From 4105c5f1abf2bd80df7d4e8839c871820aaf03bd Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Mon, 21 Oct 2019 06:12:58 +0200 Subject: [PATCH 1/2] PhpdocToParamTypeFixer - add failing cases --- .../FunctionNotation/PhpdocToParamTypeFixerTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php index b337a48bba8..ceffe200ed7 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php @@ -233,6 +233,9 @@ class Foo { 'skip mixed types' => [ ' [ + ' [ ' [ ' [ + ' [ + ' Date: Sun, 24 Nov 2019 14:22:17 +0100 Subject: [PATCH 2/2] fixup! PhpdocToParamTypeFixer - add failing cases --- .../PhpdocToParamTypeFixer.php | 43 +++++++++---------- .../PhpdocToParamTypeFixerTest.php | 6 +-- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php index 16254d5570c..6f0e2c2370d 100644 --- a/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php @@ -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; } @@ -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) { @@ -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); } diff --git a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php index ceffe200ed7..de0db950586 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php @@ -320,13 +320,13 @@ class Foo { ' [ - ' [ '