Skip to content

Commit 114bb0e

Browse files
committed
Merge branch 'master' into 3.0
# Conflicts: # src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
2 parents eb681b4 + c5d6614 commit 114bb0e

File tree

4 files changed

+142
-106
lines changed

4 files changed

+142
-106
lines changed

src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
145145

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

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

151151
if (null === $variableIndex) {
152152
continue;
@@ -173,25 +173,22 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
173173
}
174174
}
175175

176-
private function findCorrectVariable(Tokens $tokens, int $index, Annotation $paramTypeAnnotation): ?int
176+
private function findCorrectVariable(Tokens $tokens, int $startIndex, Annotation $paramTypeAnnotation): ?int
177177
{
178-
$nextFunction = $tokens->getNextTokenOfKind($index, [[T_FUNCTION]]);
179-
$variableIndex = $tokens->getNextTokenOfKind($index, [[T_VARIABLE]]);
178+
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
180179

181-
if (\is_int($nextFunction) && $variableIndex > $nextFunction) {
182-
return null;
183-
}
184-
185-
if (!isset($tokens[$variableIndex])) {
186-
return null;
187-
}
180+
for ($index = $startIndex + 1; $index < $endIndex; ++$index) {
181+
if (!$tokens[$index]->isGivenKind(T_VARIABLE)) {
182+
continue;
183+
}
188184

189-
$variableToken = $tokens[$variableIndex]->getContent();
190-
if ($paramTypeAnnotation->getVariableName() === $variableToken) {
191-
return $variableIndex;
185+
$variableName = $tokens[$index]->getContent();
186+
if ($paramTypeAnnotation->getVariableName() === $variableName) {
187+
return $index;
188+
}
192189
}
193190

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

197194
/**

src/Fixer/Import/GroupImportFixer.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,10 @@ private function removeSingleUseStatements(array $statements, Tokens $tokens): v
147147
private function addGroupUseStatements(array $statements, Tokens $tokens): void
148148
{
149149
$currentUseDeclaration = null;
150-
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex();
151-
152-
while ($tokens[$insertIndex]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
153-
++$insertIndex;
154-
}
150+
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex() + 1;
155151

156152
foreach ($statements as $index => $useDeclaration) {
157153
if ($this->areDeclarationsDifferent($currentUseDeclaration, $useDeclaration)) {
158-
if ($index > 1) {
159-
++$insertIndex;
160-
}
161-
162154
$currentUseDeclaration = $useDeclaration;
163155
$insertIndex += $this->createNewGroup(
164156
$tokens,
@@ -173,11 +165,11 @@ private function addGroupUseStatements(array $statements, Tokens $tokens): void
173165
];
174166

175167
if ($useDeclaration->isAliased()) {
176-
$tokens->insertAt($insertIndex + 1, $newTokens);
168+
$tokens->insertAt($insertIndex, $newTokens);
177169
$insertIndex += \count($newTokens);
178170
$newTokens = [];
179171

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

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

191-
$tokens->insertAt($insertIndex + 1, $newTokens);
183+
$tokens->insertAt($insertIndex, $newTokens);
192184
$insertIndex += \count($newTokens);
193185
}
194186
}
@@ -218,7 +210,7 @@ private function insertToGroupUseWithAlias(Tokens $tokens, int $insertIndex, Nam
218210

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

221-
return \count($newTokens);
213+
return \count($newTokens) + 1;
222214
}
223215

224216
/**
@@ -267,7 +259,7 @@ private function createNewGroup(Tokens $tokens, int $insertIndex, NamespaceUseAn
267259
$insertIndex += $inserted;
268260
}
269261

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

273265
return $insertedTokens;

tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,22 @@ function my_foo($foo) {}
420420
null,
421421
['scalar_types' => false],
422422
],
423+
'do not fix function call' => [
424+
'<?php
425+
/** @param string $foo */
426+
function bar($notFoo) {
427+
return baz($foo);
428+
}
429+
',
430+
],
431+
'do not fix function call when no parameter' => [
432+
'<?php
433+
/** @param string $foo */
434+
function bar() {
435+
return baz($foo);
436+
}
437+
',
438+
],
423439
];
424440
}
425441
}

0 commit comments

Comments
 (0)