Skip to content

Commit

Permalink
fixup! PhpdocToParamTypeFixer - add failing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed Nov 24, 2019
1 parent 4105c5f commit 1ef71ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
43 changes: 20 additions & 23 deletions src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ class Foo {
'<?php class Foo { /** @param Bar $bar */ public function foo($tab) { } }',
],
'param by reference' => [
'<?php /** @param array $data */ function sort(array &$data) {}',
'<?php /** @param array $data */ function sort(&$data) {}',
'<?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) {}',
70200,
70100,
],
];
}
Expand Down

0 comments on commit 1ef71ae

Please sign in to comment.