Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpdocToParamTypeFixer - do not change function call #5671

Merged
merged 1 commit into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
',
],
];
}
}