diff --git a/src/AbstractPhpdocToTypeDeclarationFixer.php b/src/AbstractPhpdocToTypeDeclarationFixer.php new file mode 100644 index 00000000000..7a4ddb990ef --- /dev/null +++ b/src/AbstractPhpdocToTypeDeclarationFixer.php @@ -0,0 +1,40 @@ + + * Dariusz RumiƄski + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer; + +use PhpCsFixer\Tokenizer\Tokens; + +/** + * @internal + */ +abstract class AbstractPhpdocToTypeDeclarationFixer extends AbstractFixer +{ + /** + * @var array + */ + private static $syntaxValidationCache = []; + + final protected function isValidSyntax($code) + { + if (!isset(self::$syntaxValidationCache[$code])) { + try { + Tokens::fromCode($code); + self::$syntaxValidationCache[$code] = true; + } catch (\ParseError $e) { + self::$syntaxValidationCache[$code] = false; + } + } + + return self::$syntaxValidationCache[$code]; + } +} diff --git a/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php index 07b5d5d46f8..8daf196bcca 100644 --- a/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php @@ -12,7 +12,7 @@ namespace PhpCsFixer\Fixer\FunctionNotation; -use PhpCsFixer\AbstractFixer; +use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer; use PhpCsFixer\DocBlock\Annotation; use PhpCsFixer\DocBlock\DocBlock; use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; @@ -29,7 +29,7 @@ /** * @author Jan Gantzert */ -final class PhpdocToParamTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface +final class PhpdocToParamTypeFixer extends AbstractPhpdocToTypeDeclarationFixer implements ConfigurationDefinitionFixerInterface { /** @internal */ const CLASS_REGEX = '/^\\\\?[a-zA-Z_\\x7f-\\xff](?:\\\\?[a-zA-Z0-9_\\x7f-\\xff]+)*(?\[\])*$/'; @@ -263,6 +263,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } + if (!$this->isValidSyntax(sprintf('fixFunctionDefinition( $paramType, $tokens, diff --git a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php index dda3fa27d69..625c7e5392f 100644 --- a/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php +++ b/src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php @@ -12,7 +12,7 @@ namespace PhpCsFixer\Fixer\FunctionNotation; -use PhpCsFixer\AbstractFixer; +use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer; use PhpCsFixer\DocBlock\Annotation; use PhpCsFixer\DocBlock\DocBlock; use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; @@ -29,7 +29,7 @@ /** * @author Filippo Tessarotto */ -final class PhpdocToReturnTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface +final class PhpdocToReturnTypeFixer extends AbstractPhpdocToTypeDeclarationFixer implements ConfigurationDefinitionFixerInterface { /** * @var array> @@ -75,11 +75,6 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati */ private $classRegex = '/^\\\\?[a-zA-Z_\\x7f-\\xff](?:\\\\?[a-zA-Z0-9_\\x7f-\\xff]+)*(?\[\])*$/'; - /** - * @var array - */ - private $returnTypeCache = []; - /** * {@inheritdoc} */ @@ -256,7 +251,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } - if (!$this->isValidType($returnType)) { + if (!$this->isValidSyntax(sprintf('getAnnotationsOfType('return'); } - - /** - * @param string $returnType - * - * @return bool - */ - private function isValidType($returnType) - { - if (!\array_key_exists($returnType, $this->returnTypeCache)) { - try { - Tokens::fromCode(sprintf('returnTypeCache[$returnType] = true; - } catch (\ParseError $e) { - $this->returnTypeCache[$returnType] = false; - } - } - - return $this->returnTypeCache[$returnType]; - } } diff --git a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php index de0db950586..00a779db7b0 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php @@ -80,6 +80,13 @@ function my_foo() {} function my_foo2($bar) {} ', ], + 'invalid - phpdoc param with keyword' => [ + ' [ '