forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PhpdocToParamTypeFixer - fix for breaking PHP syntax for type having …
…reserved name
- Loading branch information
1 parent
a4e94a9
commit 76dde6f
Showing
4 changed files
with
56 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* 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<string, bool> | ||
*/ | ||
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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]+)*(?<array>\[\])*$/'; | ||
|
@@ -263,6 +263,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
continue; | ||
} | ||
|
||
if (!$this->isValidSyntax(sprintf('<?php function f(%s $x) {}', $paramType))) { | ||
continue; | ||
} | ||
|
||
$this->fixFunctionDefinition( | ||
$paramType, | ||
$tokens, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
final class PhpdocToReturnTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
final class PhpdocToReturnTypeFixer extends AbstractPhpdocToTypeDeclarationFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
/** | ||
* @var array<int, array<int, int|string>> | ||
|
@@ -75,11 +75,6 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati | |
*/ | ||
private $classRegex = '/^\\\\?[a-zA-Z_\\x7f-\\xff](?:\\\\?[a-zA-Z0-9_\\x7f-\\xff]+)*(?<array>\[\])*$/'; | ||
|
||
/** | ||
* @var array<string, bool> | ||
*/ | ||
private $returnTypeCache = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -256,7 +251,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
continue; | ||
} | ||
|
||
if (!$this->isValidType($returnType)) { | ||
if (!$this->isValidSyntax(sprintf('<?php function f():%s {}', $returnType))) { | ||
continue; | ||
} | ||
|
||
|
@@ -346,23 +341,4 @@ private function findReturnAnnotations(Tokens $tokens, $index) | |
|
||
return $doc->getAnnotationsOfType('return'); | ||
} | ||
|
||
/** | ||
* @param string $returnType | ||
* | ||
* @return bool | ||
*/ | ||
private function isValidType($returnType) | ||
{ | ||
if (!\array_key_exists($returnType, $this->returnTypeCache)) { | ||
try { | ||
Tokens::fromCode(sprintf('<?php function f():%s {}', $returnType)); | ||
$this->returnTypeCache[$returnType] = true; | ||
} catch (\ParseError $e) { | ||
$this->returnTypeCache[$returnType] = false; | ||
} | ||
} | ||
|
||
return $this->returnTypeCache[$returnType]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters