From 5f5853d880f66c097244ff431d23fd209223a23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Thu, 21 May 2020 17:35:26 +0200 Subject: [PATCH 1/4] PhpdocToReturnTypeFixer - tests against all tokens --- src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php | 6 ++++++ .../Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php index d49e290d63c..28d5e2e0140 100644 --- a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php @@ -251,6 +251,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } + try { + Tokens::fromCode(sprintf('fixFunctionDefinition($tokens, $startIndex, $isNullable, $returnType); } } diff --git a/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php index ced56c34ec2..4d4b2fbcb6b 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php @@ -60,6 +60,12 @@ public function provideFixCases() 'invalid class 2' => [ ' [ + ' [ + ' [ ' Date: Tue, 26 May 2020 18:52:44 +0200 Subject: [PATCH 2/4] Add one more test --- tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php index 4d4b2fbcb6b..c95a4a49acb 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php @@ -66,6 +66,9 @@ public function provideFixCases() 'invalid class 4' => [ ' [ + ' [ ' Date: Fri, 5 Jun 2020 22:49:24 +0200 Subject: [PATCH 3/4] Cache reserved keyword list (#4) --- .../PhpdocToReturnTypeFixer.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php index 28d5e2e0140..dc5213de6f2 100644 --- a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php @@ -75,6 +75,11 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati */ private $classRegex = '/^\\\\?[a-zA-Z_\\x7f-\\xff](?:\\\\?[a-zA-Z0-9_\\x7f-\\xff]+)*(?\[\])*$/'; + /** + * @var array + */ + private $reservedKeywordCache = []; + /** * {@inheritdoc} */ @@ -251,9 +256,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } - try { - Tokens::fromCode(sprintf('isReservedKeyword($returnType)) { continue; } @@ -343,4 +346,27 @@ private function findReturnAnnotations(Tokens $tokens, $index) return $doc->getAnnotationsOfType('return'); } + + /** + * Test if a given return type is a reserved PHP keyword. + * + * @param string $returnType + * + * @return bool + */ + private function isReservedKeyword($returnType) + { + if (! array_key_exists($returnType, $this->reservedKeywordCache)) { + $isReserved = false; + try { + Tokens::fromCode(sprintf('reservedKeywordCache[$returnType] = $isReserved; + } + + return $this->reservedKeywordCache[$returnType]; + } } From 1893af20a4e3076667c2932bae720bc3d2f4d4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Fri, 5 Jun 2020 22:57:53 +0200 Subject: [PATCH 4/4] Update naming --- .../PhpdocToReturnTypeFixer.php | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php index dc5213de6f2..dda3fa27d69 100644 --- a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php @@ -78,7 +78,7 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati /** * @var array */ - private $reservedKeywordCache = []; + private $returnTypeCache = []; /** * {@inheritdoc} @@ -256,7 +256,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } - if ($this->isReservedKeyword($returnType)) { + if (!$this->isValidType($returnType)) { continue; } @@ -348,25 +348,21 @@ private function findReturnAnnotations(Tokens $tokens, $index) } /** - * Test if a given return type is a reserved PHP keyword. - * * @param string $returnType * * @return bool */ - private function isReservedKeyword($returnType) + private function isValidType($returnType) { - if (! array_key_exists($returnType, $this->reservedKeywordCache)) { - $isReserved = false; + if (!\array_key_exists($returnType, $this->returnTypeCache)) { try { Tokens::fromCode(sprintf('returnTypeCache[$returnType] = true; } catch (\ParseError $e) { - $isReserved = true; + $this->returnTypeCache[$returnType] = false; } - - $this->reservedKeywordCache[$returnType] = $isReserved; } - - return $this->reservedKeywordCache[$returnType]; + + return $this->returnTypeCache[$returnType]; } }