Skip to content

Commit

Permalink
Merge branch 'master' into 3.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
  • Loading branch information
keradus committed May 3, 2021
2 parents eb681b4 + c5d6614 commit 114bb0e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 106 deletions.
29 changes: 13 additions & 16 deletions src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

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 All @@ -173,25 +173,22 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}
}

private function findCorrectVariable(Tokens $tokens, int $index, Annotation $paramTypeAnnotation): ?int
private function findCorrectVariable(Tokens $tokens, int $startIndex, Annotation $paramTypeAnnotation): ?int
{
$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
20 changes: 6 additions & 14 deletions src/Fixer/Import/GroupImportFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,10 @@ private function removeSingleUseStatements(array $statements, Tokens $tokens): v
private function addGroupUseStatements(array $statements, Tokens $tokens): void
{
$currentUseDeclaration = null;
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex();

while ($tokens[$insertIndex]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
++$insertIndex;
}
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex() + 1;

foreach ($statements as $index => $useDeclaration) {
if ($this->areDeclarationsDifferent($currentUseDeclaration, $useDeclaration)) {
if ($index > 1) {
++$insertIndex;
}

$currentUseDeclaration = $useDeclaration;
$insertIndex += $this->createNewGroup(
$tokens,
Expand All @@ -173,11 +165,11 @@ private function addGroupUseStatements(array $statements, Tokens $tokens): void
];

if ($useDeclaration->isAliased()) {
$tokens->insertAt($insertIndex + 1, $newTokens);
$tokens->insertAt($insertIndex, $newTokens);
$insertIndex += \count($newTokens);
$newTokens = [];

$insertIndex += $this->insertToGroupUseWithAlias($tokens, $insertIndex + 1, $useDeclaration);
$insertIndex += $this->insertToGroupUseWithAlias($tokens, $insertIndex, $useDeclaration);
}

$newTokens[] = new Token([T_STRING, $useDeclaration->getShortName()]);
Expand All @@ -188,7 +180,7 @@ private function addGroupUseStatements(array $statements, Tokens $tokens): void
$newTokens[] = new Token([T_WHITESPACE, "\n"]);
}

$tokens->insertAt($insertIndex + 1, $newTokens);
$tokens->insertAt($insertIndex, $newTokens);
$insertIndex += \count($newTokens);
}
}
Expand Down Expand Up @@ -218,7 +210,7 @@ private function insertToGroupUseWithAlias(Tokens $tokens, int $insertIndex, Nam

$tokens->insertAt($insertIndex, $newTokens);

return \count($newTokens);
return \count($newTokens) + 1;
}

/**
Expand Down Expand Up @@ -267,7 +259,7 @@ private function createNewGroup(Tokens $tokens, int $insertIndex, NamespaceUseAn
$insertIndex += $inserted;
}

$tokens->insertAt($insertIndex + 1, new Token([T_STRING, $useDeclaration->getShortName()]));
$tokens->insertAt($insertIndex, new Token([T_STRING, $useDeclaration->getShortName()]));
++$insertedTokens;

return $insertedTokens;
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,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);
}
',
],
];
}
}
Loading

0 comments on commit 114bb0e

Please sign in to comment.