diff --git a/composer.json b/composer.json index a3b34d60110..8fca0284522 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "rector/type-perfect": "^2.1", "shipmonk/composer-dependency-analyser": "^1.8", "symplify/phpstan-extensions": "^12.0.2", - "symplify/phpstan-rules": "^14.9.3", + "symplify/phpstan-rules": "^14.9.4", "symplify/vendor-patches": "^11.5", "tomasvotruba/class-leak": "^2.1", "tomasvotruba/unused-public": "^2.1", diff --git a/rules-tests/DeadCode/Rector/Expression/RemoveDeadStmtRector/FixtureRemovedComments/array_dim_fetch.php.inc b/rules-tests/DeadCode/Rector/Expression/RemoveDeadStmtRector/FixtureRemovedComments/array_dim_fetch.php.inc index d756d9867e6..62738d04017 100644 --- a/rules-tests/DeadCode/Rector/Expression/RemoveDeadStmtRector/FixtureRemovedComments/array_dim_fetch.php.inc +++ b/rules-tests/DeadCode/Rector/Expression/RemoveDeadStmtRector/FixtureRemovedComments/array_dim_fetch.php.inc @@ -2,19 +2,19 @@ namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch; -function wrapToPreventPhpStanCallingMethods () +function wrapToPreventPhpStanCallingMethods() { $var[0] = 1; $var[0]; - //comment + //comment 1 $var[methodCall()]; - //comment + //comment 2 ${methodCall()}[0]; - //comment + //comment 3 ${methodCall1()}[methodCall2()]; } ?> @@ -23,19 +23,18 @@ function wrapToPreventPhpStanCallingMethods () namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch; -function wrapToPreventPhpStanCallingMethods () +function wrapToPreventPhpStanCallingMethods() { $var[0] = 1; - //comment + //comment 1 methodCall(); - //comment + //comment 2 methodCall(); - //comment methodCall2(); - //comment + //comment 3 methodCall1(); } ?> diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc index 13a6a0ad306..d2e332b5d02 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc @@ -10,6 +10,11 @@ class KeepDocblockOnReturn /** @psalm-suppress UndefinedFunction */ return ff(); }; + + function() { + // @psalm-suppress UndefinedFunction + return ff(); + }; } } @@ -26,6 +31,10 @@ class KeepDocblockOnReturn fn() => /** @psalm-suppress UndefinedFunction */ ff(); + + fn() => + // @psalm-suppress UndefinedFunction + ff(); } } diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return2.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return2.php.inc deleted file mode 100644 index fc54f526443..00000000000 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return2.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ - - // @psalm-suppress UndefinedFunction - ff(); - } -} - -?> diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return3.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return3.php.inc deleted file mode 100644 index bc411c6b50c..00000000000 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return3.php.inc +++ /dev/null @@ -1,40 +0,0 @@ - ------ - - /** - * comment - */ - // something - // @psalm-suppress UndefinedFunction - ff(); - } -} - -?> diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return4.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return4.php.inc deleted file mode 100644 index 2a7f08f4e8d..00000000000 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return4.php.inc +++ /dev/null @@ -1,44 +0,0 @@ - ------ - - /** - * comment - */ - // something - // @psalm-suppress UndefinedFunction - ff(); - } - } -} - -?> diff --git a/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php b/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php index 72d895999ec..3eda0672ab3 100644 --- a/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php +++ b/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\Expression; +use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; @@ -11,10 +12,8 @@ use PhpParser\Node\Stmt\Nop; use PhpParser\NodeVisitor; use PHPStan\Reflection\Php\PhpPropertyReflection; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\NodeManipulator\LivingCodeManipulator; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -29,7 +28,6 @@ public function __construct( private readonly LivingCodeManipulator $livingCodeManipulator, private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer, private readonly ReflectionResolver $reflectionResolver, - private readonly PhpDocInfoFactory $phpDocInfoFactory ) { } @@ -111,11 +109,9 @@ private function hasGetMagic(Expression $expression): bool */ private function removeNodeAndKeepComments(Expression $expression): int|Node { - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression); - - if ($expression->getComments() !== []) { + if ($expression->getDocComment() instanceof Doc) { $nop = new Nop(); - $nop->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo); + $nop->setDocComment($expression->getDocComment()); return $nop; } diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index 9f772a64473..0fed21a7697 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -127,11 +127,13 @@ public function refactor(Node $node): int|null|array|If_ return NodeVisitor::REMOVE_NODE; } - $node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge( - $node->getComments(), - $node->stmts[0]->getComments(), - )); - $node->stmts[0]->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true); + // keep original comments + if ($node->getComments() !== []) { + $node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge( + $node->getComments(), + $node->stmts[0]->getComments(), + )); + } return $node->stmts; } diff --git a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php index e445af74074..1d76110c2c7 100644 --- a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php +++ b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php @@ -12,6 +12,7 @@ use Rector\DeadCode\ConditionEvaluator; use Rector\DeadCode\ConditionResolver; use Rector\DeadCode\Contract\ConditionInterface; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -100,7 +101,7 @@ private function refactorIsMatch(If_ $if): ?array return null; } - return $if->stmts; + return $this->keepIfLevelComments($if, $if->stmts); } /** @@ -113,7 +114,22 @@ private function refactorIsNotMatch(If_ $if): array|int return NodeVisitor::REMOVE_NODE; } - // else is always used - return $if->else->stmts; + return $this->keepIfLevelComments($if, $if->else->stmts); + } + + /** + * @param Stmt[] $stmts + * @return Stmt[] + */ + private function keepIfLevelComments(If_ $if, array $stmts): array + { + if ($stmts !== []) { + $stmts[0]->setAttribute( + AttributeKey::COMMENTS, + array_merge($if->getComments(), $stmts[0]->getComments()) + ); + } + + return $stmts; } } diff --git a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php index e1574ebb837..b4e1fd9d04d 100644 --- a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php +++ b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node } // just created func call - if ($node->getAttribute(AttributeKey::DO_NOT_CHANGE) === true) { + if ($node->getAttribute(AttributeKey::ORIGINAL_NODE) === null) { return null; } @@ -124,9 +124,6 @@ public function refactor(Node $node): ?Node private function createGetClassFuncCall(FuncCall $oldFuncCall): FuncCall { - $funcCall = new FuncCall($oldFuncCall->name, $oldFuncCall->args); - $funcCall->setAttribute(AttributeKey::DO_NOT_CHANGE, true); - - return $funcCall; + return new FuncCall($oldFuncCall->name, $oldFuncCall->args); } } diff --git a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php index 3e396541461..ae4338ddebf 100644 --- a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php +++ b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php @@ -89,7 +89,7 @@ public function refactor(Node $node): ?Node $comments = $node->stmts[0]->getAttribute(AttributeKey::COMMENTS) ?? []; if ($comments !== []) { $this->mirrorComments($arrowFunction->expr, $node->stmts[0]); - $arrowFunction->setAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED, true); + $arrowFunction->setAttribute(AttributeKey::COMMENTS, true); } return $arrowFunction; diff --git a/src/NodeTypeResolver/Node/AttributeKey.php b/src/NodeTypeResolver/Node/AttributeKey.php index 11422b1e6c4..d3c93956907 100644 --- a/src/NodeTypeResolver/Node/AttributeKey.php +++ b/src/NodeTypeResolver/Node/AttributeKey.php @@ -81,11 +81,6 @@ final class AttributeKey */ public const IS_REGULAR_PATTERN = 'is_regular_pattern'; - /** - * @var string - */ - public const DO_NOT_CHANGE = 'do_not_change'; - /** * Helps with infinite loop detection * @var string @@ -97,11 +92,6 @@ final class AttributeKey */ public const WRAPPED_IN_PARENTHESES = 'wrapped_in_parentheses'; - /** - * @var string - */ - public const COMMENT_CLOSURE_RETURN_MIRRORED = 'comment_closure_return_mirrored'; - /** * To pass PHP 8.0 attribute FQN names * @var string @@ -114,6 +104,7 @@ final class AttributeKey public const EXTRA_USE_IMPORT = 'extra_use_import'; /** + * Used internally by php-parser * @var string */ public const DOC_LABEL = 'docLabel'; @@ -266,11 +257,6 @@ final class AttributeKey */ public const IS_FIRST_LEVEL_STATEMENT = 'first_level_stmt'; - /** - * @var string - */ - public const HAS_MERGED_COMMENTS = 'has_merged_comments'; - public const IS_DEFAULT_PROPERTY_VALUE = 'is_default_property_value'; public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value'; diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index f2ae597d01b..caa9c211a4f 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -161,7 +161,7 @@ protected function p( protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $precedence, int $lhsPrecedence): string { - if (! $arrowFunction->hasAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED)) { + if (! $arrowFunction->hasAttribute(AttributeKey::COMMENTS)) { return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 6d0e317e12d..44b38d41851 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -291,12 +291,6 @@ private function postRefactorProcess( $currentScope = $node->getAttribute(AttributeKey::SCOPE); if (is_array($refactoredNode)) { - $firstNode = current($refactoredNode); - - if ($firstNode->getAttribute(AttributeKey::HAS_MERGED_COMMENTS, false) === false) { - $this->mirrorComments($firstNode, $originalNode); - } - $this->refreshScopeNodes($refactoredNode, $filePath, $currentScope); // search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown