Skip to content

Commit 1ef71ae

Browse files
committed
fixup! PhpdocToParamTypeFixer - add failing cases
1 parent 4105c5f commit 1ef71ae

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
252252
continue;
253253
}
254254

255+
$byRefIndex = $tokens->getPrevMeaningfulToken($variableIndex);
256+
if ($tokens[$byRefIndex]->equals('&')) {
257+
$variableIndex = $byRefIndex;
258+
}
259+
255260
if (!('(' === $tokens[$variableIndex - 1]->getContent()) && $this->hasParamTypeHint($tokens, $variableIndex - 2)) {
256261
continue;
257262
}
@@ -374,44 +379,32 @@ private function fixFunctionDefinition(
374379
$hasCallable,
375380
$hasObject
376381
) {
377-
if (true === $hasNull) {
378-
$newTokens[] = new Token([CT::T_NULLABLE_TYPE, '?']);
379-
}
382+
$newTokens = [];
380383

381384
if (true === $hasVoid) {
382385
$newTokens[] = new Token('void');
383-
}
384-
385-
if (true === $hasIterable && true === $hasArray) {
386+
} elseif (true === $hasIterable && true === $hasArray) {
386387
$newTokens[] = new Token([CT::T_ARRAY_TYPEHINT, 'array']);
387388
} elseif (true === $hasIterable) {
388389
$newTokens[] = new Token([T_STRING, 'iterable']);
389390
} elseif (true === $hasArray) {
390391
$newTokens[] = new Token([CT::T_ARRAY_TYPEHINT, 'array']);
391-
}
392-
393-
if (true === $hasString) {
392+
} elseif (true === $hasString) {
394393
$newTokens[] = new Token([T_STRING, 'string']);
395-
}
396-
397-
if (true === $hasInt) {
394+
} elseif (true === $hasInt) {
398395
$newTokens[] = new Token([T_STRING, 'int']);
399-
}
400-
401-
if (true === $hasFloat) {
396+
} elseif (true === $hasFloat) {
402397
$newTokens[] = new Token([T_STRING, 'float']);
403-
}
404-
405-
if (true === $hasBool) {
398+
} elseif (true === $hasBool) {
406399
$newTokens[] = new Token([T_STRING, 'bool']);
407-
}
408-
409-
if (true === $hasCallable) {
400+
} elseif (true === $hasCallable) {
410401
$newTokens[] = new Token([T_CALLABLE, 'callable']);
402+
} elseif (true === $hasObject) {
403+
$newTokens[] = new Token([T_STRING, 'object']);
411404
}
412405

413-
if (true === $hasObject) {
414-
$newTokens[] = new Token([T_STRING, 'object']);
406+
if ('' !== $paramType && [] !== $newTokens) {
407+
return;
415408
}
416409

417410
foreach (explode('\\', $paramType) as $nsIndex => $value) {
@@ -425,6 +418,10 @@ private function fixFunctionDefinition(
425418
$newTokens[] = new Token([T_STRING, $value]);
426419
}
427420

421+
if (true === $hasNull) {
422+
array_unshift($newTokens, new Token([CT::T_NULLABLE_TYPE, '?']));
423+
}
424+
428425
$newTokens[] = new Token([T_WHITESPACE, ' ']);
429426
$tokens->insertAt($index, $newTokens);
430427
}

tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ class Foo {
320320
'<?php class Foo { /** @param Bar $bar */ public function foo($tab) { } }',
321321
],
322322
'param by reference' => [
323-
'<?php /** @param array $data */ function sort(array &$data) {}',
324-
'<?php /** @param array $data */ function sort(&$data) {}',
323+
'<?php /** @param array $data */ function foo(array &$data) {}',
324+
'<?php /** @param array $data */ function foo(&$data) {}',
325325
],
326326
'optional param by reference' => [
327327
'<?php /** @param null|string[] $matches */ function matchAll(?array &$matches) {}',
328328
'<?php /** @param null|string[] $matches */ function matchAll(&$matches) {}',
329-
70200,
329+
70100,
330330
],
331331
];
332332
}

0 commit comments

Comments
 (0)