From 120e0ccfc40bc696d23a9e1d93b6662ea7dfa682 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Dec 2023 16:53:06 +0100 Subject: [PATCH] Bump to php parser 4.18 (#5362) * bump to php parser 4.18 * Update rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php --------- Co-authored-by: Abdul Malik Ikhsan --- composer.json | 2 +- .../config/configured_rule_keep_pre_slash.php | 2 +- .../RemoveNullTagValueNodeRector.php | 96 +++++++++---------- .../StringClassNameToClassConstantRector.php | 16 ++-- 4 files changed, 59 insertions(+), 57 deletions(-) diff --git a/composer.json b/composer.json index 9109aac478a..66b6ec33667 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "fidry/cpu-core-counter": "^0.5.1", "illuminate/container": "^10.20", "nette/utils": "^3.2", - "nikic/php-parser": "^4.17.1", + "nikic/php-parser": "^4.18.0", "ondram/ci-detector": "^4.1", "phpstan/phpdoc-parser": "^1.23", "phpstan/phpstan": "^1.10.35", diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/config/configured_rule_keep_pre_slash.php b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/config/configured_rule_keep_pre_slash.php index 3c72ddf0de0..7b59c61d15d 100644 --- a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/config/configured_rule_keep_pre_slash.php +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/config/configured_rule_keep_pre_slash.php @@ -13,6 +13,6 @@ 'Exception', // keep '\\' prefix string on string '\Foo\Bar' - StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true + StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true, ]); }; diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php index 242a68f5a5b..2e4db004443 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php @@ -75,6 +75,50 @@ public function getNodeTypes(): array return [ClassMethod::class, Function_::class, Expression::class, Property::class]; } + /** + * @param ClassMethod|Function_|Expression|Property $node + */ + public function refactor(Node $node): ?Node + { + if ($node instanceof Expression || $node instanceof Property) { + return $this->processVarTagNull($node); + } + + $phpdocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + $removedParamNames = []; + + foreach ($node->params as $param) { + $paramName = $this->getName($param); + $paramTagValueNode = $phpdocInfo->getParamTagValueByName($paramName); + + if ($paramTagValueNode instanceof ParamTagValueNode && $this->isNull($paramTagValueNode)) { + $removedParamNames[] = $paramTagValueNode->parameterName; + } + } + + $hasRemoved = false; + if ($removedParamNames !== []) { + $this->removeParamNullTag($phpdocInfo, $removedParamNames); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); + + $hasRemoved = true; + } + + $returnTagValueNode = $phpdocInfo->getReturnTagValue(); + if ($returnTagValueNode instanceof ReturnTagValueNode && $this->isNull($returnTagValueNode)) { + $phpdocInfo->removeByType(ReturnTagValueNode::class); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); + + $hasRemoved = true; + } + + if (! $hasRemoved) { + return null; + } + + return $node; + } + private function isNull(VarTagValueNode|ParamTagValueNode|ReturnTagValueNode $tag): bool { return $tag->type instanceof IdentifierTypeNode @@ -91,7 +135,7 @@ private function removeParamNullTag(PhpDocInfo $phpDocInfo, array $paramNames): $phpDocNodeTraverser->traverseWithCallable( $phpDocInfo->getPhpDocNode(), '', - static function (AstNode $astNode) use ($paramNames) : ?int { + static function (AstNode $astNode) use ($paramNames): ?int { if (! $astNode instanceof PhpDocTagNode) { return null; } @@ -100,12 +144,13 @@ static function (AstNode $astNode) use ($paramNames) : ?int { return null; } - if (in_array($astNode->value->parameterName , $paramNames, true)) { + if (in_array($astNode->value->parameterName, $paramNames, true)) { return PhpDocNodeTraverser::NODE_REMOVE; } return null; - }); + } + ); } private function processVarTagNull(Expression|Property $node): ?Node @@ -114,7 +159,6 @@ private function processVarTagNull(Expression|Property $node): ?Node $varTagValueNode = $phpdocInfo->getVarTagValueNode(); if ($varTagValueNode instanceof VarTagValueNode && $this->isNull($varTagValueNode)) { - $phpdocInfo->removeByType(VarTagValueNode::class); $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); @@ -123,48 +167,4 @@ private function processVarTagNull(Expression|Property $node): ?Node return null; } - - /** - * @param ClassMethod|Function_|Expression|Property $node - */ - public function refactor(Node $node): ?Node - { - if ($node instanceof Expression || $node instanceof Property) { - return $this->processVarTagNull($node); - } - - $phpdocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $removedParamNames = []; - - foreach ($node->params as $param) { - $paramName = $this->getName($param); - $paramTagValueNode = $phpdocInfo->getParamTagValueByName($paramName); - - if ($paramTagValueNode instanceof ParamTagValueNode && $this->isNull($paramTagValueNode)) { - $removedParamNames[] = $paramTagValueNode->parameterName; - } - } - - $hasRemoved = false; - if ($removedParamNames !== []) { - $this->removeParamNullTag($phpdocInfo, $removedParamNames); - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); - - $hasRemoved = true; - } - - $returnTagValueNode = $phpdocInfo->getReturnTagValue(); - if ($returnTagValueNode instanceof ReturnTagValueNode && $this->isNull($returnTagValueNode)) { - $phpdocInfo->removeByType(ReturnTagValueNode::class); - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); - - $hasRemoved = true; - } - - if (! $hasRemoved) { - return null; - } - - return $node; - } } diff --git a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php index db43c8e8c7c..d9b20960e55 100644 --- a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php +++ b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php @@ -28,6 +28,11 @@ */ final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface { + /** + * @var string + */ + public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash'; + /** * @var string */ @@ -40,11 +45,6 @@ final class StringClassNameToClassConstantRector extends AbstractRector implemen private bool $shouldKeepPreslash = false; - /** - * @var string - */ - public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash'; - public function __construct( private readonly ReflectionProvider $reflectionProvider, ) { @@ -85,7 +85,7 @@ public function run() [ 'ClassName', 'AnotherClassName', - StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => false, +StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => false, ], ), ]); @@ -159,7 +159,9 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int */ public function configure(array $configuration): void { - if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool($configuration[self::SHOULD_KEEP_PRE_SLASH])) { + if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool( + $configuration[self::SHOULD_KEEP_PRE_SLASH] + )) { $this->shouldKeepPreslash = $configuration[self::SHOULD_KEEP_PRE_SLASH]; unset($configuration[self::SHOULD_KEEP_PRE_SLASH]); }