Skip to content

Commit

Permalink
bug #5671 PhpdocToParamTypeFixer - do not change function call (kubaw…
Browse files Browse the repository at this point in the history
…erlos)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

PhpdocToParamTypeFixer - do not change function call

Commits
-------

a746cb7 PhpdocToParamTypeFixer - do not change function call
  • Loading branch information
keradus committed May 3, 2021
2 parents c8472bb + a746cb7 commit 3fadaee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
Expand Up @@ -135,8 +135,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)

list($paramType, $isNullable) = $typeInfo;

$startIndex = $tokens->getNextTokenOfKind($index, ['(']) + 1;
$variableIndex = $this->findCorrectVariable($tokens, $startIndex - 1, $paramTypeAnnotation);
$startIndex = $tokens->getNextTokenOfKind($index, ['(']);
$variableIndex = $this->findCorrectVariable($tokens, $startIndex, $paramTypeAnnotation);

if (null === $variableIndex) {
continue;
Expand Down Expand Up @@ -164,30 +164,27 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
}

/**
* @param int $index
* @param int $startIndex
* @param Annotation $paramTypeAnnotation
*
* @return null|int
*/
private function findCorrectVariable(Tokens $tokens, $index, $paramTypeAnnotation)
private function findCorrectVariable(Tokens $tokens, $startIndex, $paramTypeAnnotation)
{
$nextFunction = $tokens->getNextTokenOfKind($index, [[T_FUNCTION]]);
$variableIndex = $tokens->getNextTokenOfKind($index, [[T_VARIABLE]]);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);

if (\is_int($nextFunction) && $variableIndex > $nextFunction) {
return null;
}

if (!isset($tokens[$variableIndex])) {
return null;
}
for ($index = $startIndex + 1; $index < $endIndex; ++$index) {
if (!$tokens[$index]->isGivenKind(T_VARIABLE)) {
continue;
}

$variableToken = $tokens[$variableIndex]->getContent();
if ($paramTypeAnnotation->getVariableName() === $variableToken) {
return $variableIndex;
$variableName = $tokens[$index]->getContent();
if ($paramTypeAnnotation->getVariableName() === $variableName) {
return $index;
}
}

return $this->findCorrectVariable($tokens, $index + 1, $paramTypeAnnotation);
return null;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Expand Up @@ -423,6 +423,22 @@ function my_foo($foo) {}
null,
['scalar_types' => false],
],
'do not fix function call' => [
'<?php
/** @param string $foo */
function bar($notFoo) {
return baz($foo);
}
',
],
'do not fix function call when no parameter' => [
'<?php
/** @param string $foo */
function bar() {
return baz($foo);
}
',
],
];
}
}

0 comments on commit 3fadaee

Please sign in to comment.