Skip to content

Commit

Permalink
bug #4605 PhpdocToParamTypeFixer - cover more cases (keradus, julienf…
Browse files Browse the repository at this point in the history
…alque)

This PR was merged into the 2.16 branch.

Discussion
----------

PhpdocToParamTypeFixer - cover more cases

ref #4583

@jg-development, @SpacePossum ,
can you pick it up, please?

Commits
-------

1ef71ae fixup! PhpdocToParamTypeFixer - add failing cases
4105c5f PhpdocToParamTypeFixer - add failing cases
  • Loading branch information
keradus committed Nov 25, 2019
2 parents e72ce13 + 1ef71ae commit bfbf1b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
Expand Up @@ -252,6 +252,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

$byRefIndex = $tokens->getPrevMeaningfulToken($variableIndex);
if ($tokens[$byRefIndex]->equals('&')) {
$variableIndex = $byRefIndex;
}

if (!('(' === $tokens[$variableIndex - 1]->getContent()) && $this->hasParamTypeHint($tokens, $variableIndex - 2)) {
continue;
}
Expand Down Expand Up @@ -374,44 +379,32 @@ private function fixFunctionDefinition(
$hasCallable,
$hasObject
) {
if (true === $hasNull) {
$newTokens[] = new Token([CT::T_NULLABLE_TYPE, '?']);
}
$newTokens = [];

if (true === $hasVoid) {
$newTokens[] = new Token('void');
}

if (true === $hasIterable && true === $hasArray) {
} elseif (true === $hasIterable && true === $hasArray) {
$newTokens[] = new Token([CT::T_ARRAY_TYPEHINT, 'array']);
} elseif (true === $hasIterable) {
$newTokens[] = new Token([T_STRING, 'iterable']);
} elseif (true === $hasArray) {
$newTokens[] = new Token([CT::T_ARRAY_TYPEHINT, 'array']);
}

if (true === $hasString) {
} elseif (true === $hasString) {
$newTokens[] = new Token([T_STRING, 'string']);
}

if (true === $hasInt) {
} elseif (true === $hasInt) {
$newTokens[] = new Token([T_STRING, 'int']);
}

if (true === $hasFloat) {
} elseif (true === $hasFloat) {
$newTokens[] = new Token([T_STRING, 'float']);
}

if (true === $hasBool) {
} elseif (true === $hasBool) {
$newTokens[] = new Token([T_STRING, 'bool']);
}

if (true === $hasCallable) {
} elseif (true === $hasCallable) {
$newTokens[] = new Token([T_CALLABLE, 'callable']);
} elseif (true === $hasObject) {
$newTokens[] = new Token([T_STRING, 'object']);
}

if (true === $hasObject) {
$newTokens[] = new Token([T_STRING, 'object']);
if ('' !== $paramType && [] !== $newTokens) {
return;
}

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

if (true === $hasNull) {
array_unshift($newTokens, new Token([CT::T_NULLABLE_TYPE, '?']));
}

$newTokens[] = new Token([T_WHITESPACE, ' ']);
$tokens->insertAt($index, $newTokens);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Expand Up @@ -233,6 +233,9 @@ class Foo {
'skip mixed types' => [
'<?php /** @param Foo|Bar $bar */ function my_foo($bar) {}',
],
'skip mixed types including array' => [
'<?php /** @param array|Foo $expected */ function testResolveIntersectionOfPaths($expected) {}',
],
'array of types' => [
'<?php /** @param Foo[] $foo */ function my_foo(array $foo) {}',
'<?php /** @param Foo[] $foo */ function my_foo($foo) {}',
Expand Down Expand Up @@ -316,6 +319,15 @@ class Foo {
'stop searching last token' => [
'<?php class Foo { /** @param Bar $bar */ public function foo($tab) { } }',
],
'param by reference' => [
'<?php /** @param array $data */ function foo(array &$data) {}',
'<?php /** @param array $data */ function foo(&$data) {}',
],
'optional param by reference' => [
'<?php /** @param null|string[] $matches */ function matchAll(?array &$matches) {}',
'<?php /** @param null|string[] $matches */ function matchAll(&$matches) {}',
70100,
],
];
}
}

0 comments on commit bfbf1b2

Please sign in to comment.