diff --git a/build/build-preload.php b/build/build-preload.php index de87adabbbc..54539b212c6 100755 --- a/build/build-preload.php +++ b/build/build-preload.php @@ -361,12 +361,7 @@ private function findPhpParserFilesAndSortThem(string $vendorDir): array } } - $fileInfos = array_values($fileInfos); - - $stmtsAwareInterface = new SplFileInfo(__DIR__ . '/../src/Contract/PhpParser/Node/StmtsAwareInterface.php'); - array_splice($fileInfos, 1, 0, [$stmtsAwareInterface]); - - return $fileInfos; + return array_values($fileInfos); } /** diff --git a/composer.json b/composer.json index 135e20db25f..c685549aaf7 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "doctrine/inflector": "^2.1", "illuminate/container": "^11.46", "nette/utils": "^4.0", - "nikic/php-parser": "^5.6.1", + "nikic/php-parser": "dev-tv-add-stmts-iterable as 5.4", "ondram/ci-detector": "^4.2", "phpstan/phpdoc-parser": "^2.3", "phpstan/phpstan": "^2.1.26", @@ -116,21 +116,6 @@ "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/illuminate-container-container-php.patch" ], "nikic/php-parser": [ - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-expr-closure-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-finally-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-function-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-do-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-catch-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-trycatch-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-for-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-classmethod-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-else-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-while-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-foreach-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-if-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-case-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch", - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-namespace-php.patch", "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-nodetraverser-php.patch" ] }, @@ -147,5 +132,12 @@ } }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/tomasVotruba/php-parser", + "no-api": true + } + ] } diff --git a/phpstan.neon b/phpstan.neon index b63fa6411e0..d76a3836451 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -332,7 +332,7 @@ parameters: - identifier: symplify.seeAnnotationToTest - path: utils/PHPStan/Rule/PreferDirectIsNameRule.php + path: utils/ - identifier: arrayValues.list @@ -356,3 +356,7 @@ parameters: - identifier: offsetAccess.invalidOffset path: src/CustomRules/SimpleNodeDumper.php + + - + message: '#Method Rector\\Utils\\Rector\\MakeUseOfContaintsStmtsRector\:\:refactor\(\) should return 4\|PhpParser\\Node\\Expr\\BinaryOp\\Identical\|PhpParser\\Node\\Expr\\MethodCall\|null but returns int\|null#' + path: utils/Rector/MakeUseOfContaintsStmtsRector.php diff --git a/preload-split-package.php b/preload-split-package.php index da953d5a7e5..070c40df4de 100644 --- a/preload-split-package.php +++ b/preload-split-package.php @@ -26,7 +26,7 @@ function isPHPStanTestPreloaded(): bool } require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node.php'; -require_once __DIR__ . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php'; +//require_once __DIR__ . '/src/Contract/PhpParser/Node/ContainsStmts.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr.php'; require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeVisitor.php'; diff --git a/preload.php b/preload.php index cecb0b90b4c..bd9d8303409 100644 --- a/preload.php +++ b/preload.php @@ -26,7 +26,7 @@ function isPHPStanTestPreloaded(): bool } require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node.php'; -require_once __DIR__ . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php'; +//require_once __DIR__ . '/src/Contract/PhpParser/Node/ContainsStmts.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr.php'; require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor.php'; diff --git a/rector.php b/rector.php index b24f7fe82f0..ff568be5c5d 100644 --- a/rector.php +++ b/rector.php @@ -6,6 +6,7 @@ use Rector\Config\RectorConfig; use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector; use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; +use Rector\Utils\Rector\MakeUseOfContaintsStmtsRector; return RectorConfig::configure() ->withPreparedSets( @@ -35,6 +36,7 @@ __DIR__ . '/config', __DIR__ . '/build/build-preload.php', ]) + ->withRules([MakeUseOfContaintsStmtsRector::class]) ->withRootFiles() ->withImportNames(removeUnusedImports: true) ->withSkip([ diff --git a/rules/Assert/NodeAnalyzer/ExistingAssertStaticCallResolver.php b/rules/Assert/NodeAnalyzer/ExistingAssertStaticCallResolver.php index 306bebbd8fa..bc83c472e67 100644 --- a/rules/Assert/NodeAnalyzer/ExistingAssertStaticCallResolver.php +++ b/rules/Assert/NodeAnalyzer/ExistingAssertStaticCallResolver.php @@ -18,14 +18,14 @@ final class ExistingAssertStaticCallResolver */ public function resolve(ClassMethod $classMethod): array { - if ($classMethod->stmts === null) { + if ($classMethod->getStmts() === []) { return []; } $existingAssertCallHashes = []; $standard = new Standard(); - foreach ($classMethod->stmts as $currentStmt) { + foreach ($classMethod->getStmts() as $currentStmt) { if (! $currentStmt instanceof Expression) { continue; } diff --git a/rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php b/rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php index 34d769278b4..dfa7f5386c2 100644 --- a/rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php +++ b/rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php @@ -108,7 +108,7 @@ public function refactor(Node $node): ?ClassMethod return null; } - if ($node->stmts === null || $node->isAbstract()) { + if ($node->getStmts() === [] || $node->isAbstract()) { return null; } diff --git a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php index 993a41d9913..0dbc9ee6703 100644 --- a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php @@ -26,11 +26,11 @@ public function __construct( */ public function matchAssignItemsOnlyForeachArrayVariable(Foreach_ $foreach): ?Expr { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return null; } - $onlyStatement = $foreach->stmts[0]; + $onlyStatement = $foreach->getStmts()[0]; if ($onlyStatement instanceof Expression) { $onlyStatement = $onlyStatement->expr; } diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index 90109dc2b8b..80f0cab470d 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -96,7 +96,7 @@ public function refactor(Node $node): ?Node } $isChanged = false; - $this->traverseNodesWithCallable($node->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($node->getStmts(), function (Node $node) use ( $caughtThrowableVariable, &$isChanged ): ?int { diff --git a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php index cc193e975ca..9c2f11ca19a 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node } $hasChanged = false; - $this->traverseNodesWithCallable((array) $node->stmts, static function (Node $node) use ( + $this->traverseNodesWithCallable((array) $node->getStmts(), static function (Node $node) use ( &$hasChanged ): int|null|Return_ { if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) { diff --git a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php index 04f999bcccc..42b6a87a8f2 100644 --- a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use Rector\CodeQuality\NodeAnalyzer\VariableDimFetchAssignResolver; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Exception\NotImplementedYetException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -66,11 +66,11 @@ function getPerson() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php b/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php index 92ef0bafc8c..f84f037ff22 100644 --- a/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php +++ b/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php @@ -81,7 +81,7 @@ public function refactor(Node $node): ?Node return null; } - if ($constructClassMethod->stmts === null) { + if ($constructClassMethod->getStmts() === []) { return null; } @@ -182,14 +182,14 @@ private function refactorProperty( AttributeKey::COMMENTS, array_merge( $classStmt->getComments(), - isset($constructClassMethod->stmts[$key]) - ? $constructClassMethod->stmts[$key]->getComments() + isset($constructClassMethod->getStmts()[$key]) + ? $constructClassMethod->getStmts()[$key]->getComments() : [], ) ); // remove assign - unset($constructClassMethod->stmts[$key]); + unset($constructClassMethod->getStmts()[$key]); return true; } diff --git a/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php b/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php index 7aaf9dceb6d..9132d3b25ea 100644 --- a/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php +++ b/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeVisitor; use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; @@ -75,11 +75,11 @@ public function run($items) */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php b/rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php index 8e5d9a7dd03..79910567ac2 100644 --- a/rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php +++ b/rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use PhpParser\NodeFinder; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\BinaryOpManipulator; use Rector\Php71\ValueObject\TwoNodeMatch; use Rector\PhpParser\Node\Value\ValueResolver; @@ -64,15 +64,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -162,11 +162,11 @@ private function shouldSkipForeach(Foreach_ $foreach): bool return true; } - if (count($foreach->stmts) > 1) { + if (count($foreach->getStmts()) > 1) { return true; } - if (! $foreach->stmts[0] instanceof If_) { + if (! $foreach->getStmts()[0] instanceof If_) { return true; } @@ -196,7 +196,7 @@ private function matchNodes(Equal|Identical $binaryOp, Expr $expr): ?TwoNodeMatc private function isIfBodyABoolReturnNode(If_ $if): bool { - $ifStatement = $if->stmts[0]; + $ifStatement = $if->getStmts()[0]; if (! $ifStatement instanceof Return_) { return false; } diff --git a/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php b/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php index 3e4729ca744..2e249d5c03d 100644 --- a/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php +++ b/rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -57,15 +57,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -147,11 +147,11 @@ public function provideMinPhpVersion(): int */ private function matchForeachReturnOrAssign(Foreach_ $foreach): Expression|Return_|null { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return null; } - $onlyForeachStmt = $foreach->stmts[0]; + $onlyForeachStmt = $foreach->getStmts()[0]; if (! $onlyForeachStmt instanceof If_) { return null; } @@ -161,7 +161,7 @@ private function matchForeachReturnOrAssign(Foreach_ $foreach): Expression|Retur return null; } - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return null; } @@ -174,7 +174,7 @@ private function matchForeachReturnOrAssign(Foreach_ $foreach): Expression|Retur return null; } - $innerStmt = $if->stmts[0]; + $innerStmt = $if->getStmts()[0]; if ($innerStmt instanceof Return_) { return $innerStmt; } @@ -205,7 +205,7 @@ private function processForeachNodeWithReturnInside( } /** @var If_ $ifNode */ - $ifNode = $foreach->stmts[0]; + $ifNode = $foreach->getStmts()[0]; /** @var Identical $identicalNode */ $identicalNode = $ifNode->cond; @@ -229,7 +229,7 @@ private function processForeachNodeWithReturnInside( private function processForeachNodeWithAssignInside(Foreach_ $foreach, Assign $assign): ?Assign { /** @var If_ $ifNode */ - $ifNode = $foreach->stmts[0]; + $ifNode = $foreach->getStmts()[0]; /** @var Identical $identicalNode */ $identicalNode = $ifNode->cond; diff --git a/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php b/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php index ec3f04163c8..fa147114b40 100644 --- a/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php +++ b/rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php @@ -6,11 +6,11 @@ use PhpParser\Node; use PhpParser\Node\ArrayItem; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Foreach_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; use Rector\NodeManipulator\StmtsManipulator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -71,15 +71,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - $stmts = $node->stmts; + $stmts = $node->getStmts(); if ($stmts === null) { return null; @@ -203,7 +203,7 @@ private function refactorArrayForeachValue(List_ $list, Foreach_ $foreach): ?Lis private function isVariableUsedInForeach(Variable $variable, Foreach_ $foreach): bool { return (bool) $this->betterNodeFinder->findFirst( - $foreach->stmts, + $foreach->getStmts(), fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $variable) ); } diff --git a/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php b/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php index 0f9714edbac..886edc2fe4e 100644 --- a/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php +++ b/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php @@ -6,6 +6,7 @@ use PhpParser\Comment\Doc; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\AssignOp; use PhpParser\Node\Expr\Ternary; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Type\MixedType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeAnalyzer\CallAnalyzer; use Rector\NodeAnalyzer\VariableAnalyzer; @@ -106,15 +106,15 @@ function () { */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - $stmts = $node->stmts; + $stmts = $node->getStmts(); if ($stmts === null) { return null; } @@ -152,11 +152,11 @@ public function refactor(Node $node): ?Node } private function processSimplifyUselessVariable( - StmtsAwareInterface $stmtsAware, + ContainsStmts $containsStmts, Return_ $return, Assign|AssignOp $assign, int $key - ): ?StmtsAwareInterface { + ): ?ContainsStmts { if (! $assign instanceof Assign) { $binaryClass = $this->assignAndBinaryMap->getAlternative($assign); if ($binaryClass === null) { @@ -168,8 +168,8 @@ private function processSimplifyUselessVariable( $return->expr = $assign->expr; } - unset($stmtsAware->stmts[$key - 1]); - return $stmtsAware; + unset($containsStmts->getStmts()[$key - 1]); + return $containsStmts; } private function shouldSkipStmt(Return_ $return, Stmt $previousStmt): bool diff --git a/rules/CodeQuality/Rector/If_/CombineIfRector.php b/rules/CodeQuality/Rector/If_/CombineIfRector.php index 17c81ccb391..5ff92625945 100644 --- a/rules/CodeQuality/Rector/If_/CombineIfRector.php +++ b/rules/CodeQuality/Rector/If_/CombineIfRector.php @@ -80,7 +80,7 @@ public function refactor(Node $node): ?Node } /** @var If_ $subIf */ - $subIf = $node->stmts[0]; + $subIf = $node->getStmts()[0]; if ($this->hasVarTag($subIf)) { return null; @@ -117,7 +117,7 @@ private function shouldSkip(If_ $if): bool return true; } - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return true; } @@ -125,15 +125,16 @@ private function shouldSkip(If_ $if): bool return true; } - if (! $if->stmts[0] instanceof If_) { + if (! $if->getStmts()[0] instanceof If_) { return true; } - if ($if->stmts[0]->else instanceof Else_) { + if ($if->getStmts()[0]->else instanceof Else_) { return true; } - return (bool) $if->stmts[0]->elseifs; + return (bool) $if->getStmts()[0] + ->elseifs; } private function hasVarTag(If_ $if): bool diff --git a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php index 1b950cc5ebb..2e15b480c0c 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -84,7 +84,7 @@ public function refactor(Node $node): ?Node */ private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool { - $startStmt = current($if->stmts); + $startStmt = current($if->getStmts()); if (! $startStmt instanceof Stmt) { return true; } diff --git a/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php b/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php index a4687e98a3a..ec1d7b665c3 100644 --- a/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php +++ b/rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\Throw_; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\IfManipulator; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; @@ -72,11 +72,11 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php b/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php index 75dd1b3526d..c685d5896bc 100644 --- a/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php +++ b/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php @@ -79,11 +79,11 @@ private function shortenElseIf(If_ $node): ?If_ } $else = $node->else; - if (count($else->stmts) !== 1) { + if (count($else->getStmts()) !== 1) { return null; } - $if = $else->stmts[0]; + $if = $else->getStmts()[0]; if (! $if instanceof If_) { return null; } @@ -95,17 +95,17 @@ private function shortenElseIf(If_ $node): ?If_ $if = $refactored; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { $nop = new Nop(); $nop->setAttribute(AttributeKey::COMMENTS, $if->getComments()); $if->stmts[] = $nop; } else { - $currentStmt = current($if->stmts); + $currentStmt = current($if->getStmts()); $mergedComments = array_merge($if->getComments(), $currentStmt->getComments()); $currentStmt->setAttribute(AttributeKey::COMMENTS, $mergedComments); } - $node->elseifs[] = new ElseIf_($if->cond, $if->stmts); + $node->elseifs[] = new ElseIf_($if->cond, $if->getStmts()); $node->else = $if->else; diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php index 449af2cdc22..152408681b6 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php @@ -91,12 +91,12 @@ public function refactor(Node $node): ?Node return null; } - $ifAssignVarExpr = $this->resolveOnlyStmtAssignVar($node->stmts); + $ifAssignVarExpr = $this->resolveOnlyStmtAssignVar($node->getStmts()); if (! $ifAssignVarExpr instanceof Expr) { return null; } - $elseAssignExpr = $this->resolveOnlyStmtAssignVar($node->else->stmts); + $elseAssignExpr = $this->resolveOnlyStmtAssignVar($node->else->getStmts()); if (! $elseAssignExpr instanceof Expr) { return null; } @@ -105,8 +105,8 @@ public function refactor(Node $node): ?Node return null; } - $ternaryIfExpr = $this->resolveOnlyStmtAssignExpr($node->stmts); - $expr = $this->resolveOnlyStmtAssignExpr($node->else->stmts); + $ternaryIfExpr = $this->resolveOnlyStmtAssignExpr($node->getStmts()); + $expr = $this->resolveOnlyStmtAssignExpr($node->else->getStmts()); if (! $ternaryIfExpr instanceof Expr) { return null; } diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php index 48c0b83880f..083c73f3078 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php @@ -5,11 +5,11 @@ namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\IfManipulator; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; @@ -56,15 +56,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { - foreach ((array) $node->stmts as $key => $stmt) { + foreach ((array) $node->getStmts() as $key => $stmt) { if (! $stmt instanceof If_) { continue; } @@ -77,11 +77,11 @@ public function refactor(Node $node): ?StmtsAwareInterface continue; } - if (! isset($node->stmts[$key + 1])) { + if (! isset($node->getStmts()[$key + 1])) { return null; } - $nextNode = $node->stmts[$key + 1]; + $nextNode = $node->getStmts()[$key + 1]; if (! $nextNode instanceof Return_) { continue; } @@ -91,7 +91,7 @@ public function refactor(Node $node): ?StmtsAwareInterface continue; } - $insideIfNode = $stmt->stmts[0]; + $insideIfNode = $stmt->getStmts()[0]; if (! $nextNode->expr instanceof Expr) { continue; } @@ -100,7 +100,7 @@ public function refactor(Node $node): ?StmtsAwareInterface continue; } - unset($node->stmts[$key]); + unset($node->getStmts()[$key]); $node->stmts[$key + 1] = $insideIfNode; return $node; diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php index 03767341002..115977ed8af 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BooleanNot; @@ -18,7 +19,6 @@ use PHPStan\Type\Type; use PHPStan\Type\UnionType; use Rector\CodeQuality\TypeResolver\AssignVariableTypeResolver; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; use Rector\NodeManipulator\IfManipulator; use Rector\PhpParser\Node\Value\ValueResolver; @@ -83,15 +83,15 @@ public function get(): ?stdClass { */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php index 12d21ee35f6..af02119b437 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Return_; use Rector\BetterPhpDocParser\Comment\CommentsMerger; use Rector\CodeQuality\NodeManipulator\ExprBoolCaster; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\Rector\AbstractRector; @@ -63,15 +63,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -128,7 +128,7 @@ private function shouldSkipIfAndReturn(If_ $if, Return_ $return): bool } /** @var Return_ $ifInnerNode */ - $ifInnerNode = $if->stmts[0]; + $ifInnerNode = $if->getStmts()[0]; /** @var Expr $returnedExpr */ $returnedExpr = $ifInnerNode->expr; @@ -196,7 +196,7 @@ private function processReturnFalse(If_ $if, Return_ $nextReturn): ?Return_ private function isIfWithSingleReturnExpr(If_ $if): bool { - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return false; } @@ -204,7 +204,7 @@ private function isIfWithSingleReturnExpr(If_ $if): bool return false; } - $ifInnerNode = $if->stmts[0]; + $ifInnerNode = $if->getStmts()[0]; if (! $ifInnerNode instanceof Return_) { return false; } diff --git a/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php b/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php index 939503c8447..aac0a4bc42b 100644 --- a/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php +++ b/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php @@ -88,7 +88,7 @@ public function refactor(Node $node): array|If_|null // only default → basically unwrap if (! $onlyCase->cond instanceof Expr) { // remove default clause because it cause syntax error - return array_filter($onlyCase->stmts, static fn (Stmt $stmt): bool => ! $stmt instanceof Break_); + return array_filter($onlyCase->getStmts(), static fn (Stmt $stmt): bool => ! $stmt instanceof Break_); } $if = new If_(new Identical($node->cond, $onlyCase->cond)); diff --git a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php index c143fca8cc8..2c6d6db8e73 100644 --- a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php +++ b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php @@ -94,7 +94,7 @@ public function refactor(Node $node): ?array $defaultCase = null; foreach ($node->cases as $case) { - if (! end($case->stmts) instanceof Return_) { + if (! end($case->getStmts()) instanceof Return_) { return null; } @@ -110,7 +110,7 @@ public function refactor(Node $node): ?array } if ($defaultCase instanceof Case_) { - $newStmts = array_merge($newStmts, $defaultCase->stmts); + $newStmts = array_merge($newStmts, $defaultCase->getStmts()); } if ($newStmts === []) { diff --git a/rules/CodingStyle/Application/UseImportsAdder.php b/rules/CodingStyle/Application/UseImportsAdder.php index 7396ac4f327..e9667c3802a 100644 --- a/rules/CodingStyle/Application/UseImportsAdder.php +++ b/rules/CodingStyle/Application/UseImportsAdder.php @@ -118,7 +118,7 @@ public function addImportsToNamespace( ): bool { $namespaceName = $this->getNamespaceName($namespace); - $existingUsedImports = $this->usedImportsResolver->resolveForStmts($namespace->stmts); + $existingUsedImports = $this->usedImportsResolver->resolveForStmts($namespace->getStmts()); $existingUseImportTypes = $existingUsedImports->getUseImports(); $existingConstantUseImportTypes = $existingUsedImports->getConstantImports(); $existingFunctionUseImportTypes = $existingUsedImports->getFunctionImports(); @@ -143,7 +143,7 @@ public function addImportsToNamespace( return false; } - $this->mirrorUseComments($namespace->stmts, $newUses); + $this->mirrorUseComments($namespace->getStmts(), $newUses); $namespace->stmts = array_merge($newUses, $this->resolveInsertNop($namespace), $namespace->stmts); $namespace->stmts = array_values($namespace->stmts); @@ -156,7 +156,7 @@ public function addImportsToNamespace( */ private function resolveInsertNop(FileWithoutNamespace|Namespace_ $namespace): array { - $currentStmt = $namespace->stmts[0] ?? null; + $currentStmt = $namespace->getStmts()[0] ?? null; if (! $currentStmt instanceof Stmt || $currentStmt instanceof Use_ || $currentStmt instanceof GroupUse) { return []; } diff --git a/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php b/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php index 65f5e26ae47..ebc09ea8a32 100644 --- a/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php +++ b/rules/CodingStyle/ClassNameImport/AliasUsesResolver.php @@ -38,7 +38,7 @@ public function resolveFromNode(Node $node, array $stmts): array $node = current($namespaces); } - return $this->resolveFromStmts($node->stmts); + return $this->resolveFromStmts($node->getStmts()); } /** diff --git a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php index 2829a5a0c78..353d5fc5d08 100644 --- a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php +++ b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php @@ -80,7 +80,7 @@ public function resolveShortClassLikeNames(File $file): array $namespace = current($namespaces); /** @var ClassLike[] $classLikes */ - $classLikes = $this->betterNodeFinder->findInstanceOf($namespace->stmts, ClassLike::class); + $classLikes = $this->betterNodeFinder->findInstanceOf($namespace->getStmts(), ClassLike::class); $shortClassLikeNames = []; foreach ($classLikes as $classLike) { diff --git a/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php b/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php index 679d8977192..2bdd499dd22 100644 --- a/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php +++ b/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php @@ -27,7 +27,7 @@ public function traverserStmts(array $stmts, callable $callable): void { foreach ($stmts as $stmt) { if ($stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace) { - $this->traverserStmts($stmt->stmts, $callable); + $this->traverserStmts($stmt->getStmts(), $callable); continue; } diff --git a/rules/CodingStyle/Guard/StaticGuard.php b/rules/CodingStyle/Guard/StaticGuard.php index 810d296053a..1afb853e506 100644 --- a/rules/CodingStyle/Guard/StaticGuard.php +++ b/rules/CodingStyle/Guard/StaticGuard.php @@ -28,7 +28,7 @@ public function isLegal(Closure|ArrowFunction $node): bool } $nodes = $node instanceof Closure - ? $node->stmts + ? $node->getStmts() : [$node->expr]; return ! (bool) $this->betterNodeFinder->findFirst( diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 7bce0c68f87..b4189cc0278 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -87,7 +87,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -192,7 +192,7 @@ private function renameVariableInStmts( array $stmts, ?Stmt $stmt ): void { - $this->traverseNodesWithCallable($catch->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($catch->getStmts(), function (Node $node) use ( $oldVariableName, $newVariableName ): null { diff --git a/rules/CodingStyle/Rector/ClassMethod/BinaryOpStandaloneAssignsToDirectRector.php b/rules/CodingStyle/Rector/ClassMethod/BinaryOpStandaloneAssignsToDirectRector.php index 8ab71fb74a6..7f3197abc74 100644 --- a/rules/CodingStyle/Rector/ClassMethod/BinaryOpStandaloneAssignsToDirectRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/BinaryOpStandaloneAssignsToDirectRector.php @@ -65,17 +65,17 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } - if (count($node->stmts) !== 3) { + if (count($node->getStmts()) !== 3) { return null; } - $firstStmt = $node->stmts[0]; - $secondStmt = $node->stmts[1]; - $thirdStmt = $node->stmts[2]; + $firstStmt = $node->getStmts()[0]; + $secondStmt = $node->getStmts()[1]; + $thirdStmt = $node->getStmts()[2]; if (! $thirdStmt instanceof Return_) { return null; diff --git a/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php b/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php index d470bfb0fa9..dd1594bb228 100644 --- a/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php @@ -65,7 +65,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->params !== [] || $node->stmts === null) { + if ($node->params !== [] || $node->getStmts() === []) { return null; } @@ -127,11 +127,11 @@ private function hasFunctionOrClosureInside( ClassMethod | Function_ | Closure $functionLike, Variable $variable ): bool { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return false; } - return (bool) $this->betterNodeFinder->findFirst($functionLike->stmts, function (Node $node) use ( + return (bool) $this->betterNodeFinder->findFirst($functionLike->getStmts(), function (Node $node) use ( $variable ): bool { if (! $node instanceof Closure && ! $node instanceof Function_) { diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index 186d791fce4..94d9b4a40e0 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -88,7 +88,7 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { // skip methods with no bodies (e.g interface methods) - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -203,12 +203,12 @@ private function isNewVariableThanBefore(?string $currentStmtVariableName): bool private function isPrecededByEmptyLine(ClassMethod | Function_ | Closure $node, int $key): bool { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return false; } - $previousNode = $node->stmts[$key - 1]; - $currentNode = $node->stmts[$key]; + $previousNode = $node->getStmts()[$key - 1]; + $currentNode = $node->getStmts()[$key]; return abs($currentNode->getStartLine() - $previousNode->getStartLine()) >= 2; } diff --git a/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php b/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php index e5c0abd29cb..5a627e7eabd 100644 --- a/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php +++ b/rules/CodingStyle/Rector/Foreach_/MultiDimensionalArrayToArrayDestructRector.php @@ -110,7 +110,7 @@ private function replaceValueArrayAccessorsInForeachTree(Foreach_ $foreach): arr $usedVariableNames = $this->getUsedVariableNamesInForeachTree($foreach); $createdDestructedVariables = []; - $this->traverseNodesWithCallable($foreach->stmts, function (Node $traverseNode) use ( + $this->traverseNodesWithCallable($foreach->getStmts(), function (Node $traverseNode) use ( $foreach, $usedVariableNames, &$createdDestructedVariables diff --git a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php index e8beda1aa68..f4be1bd1538 100644 --- a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php +++ b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php @@ -116,11 +116,12 @@ private function shouldSkip( private function extractCallLike(Closure|ArrowFunction $node): FuncCall|MethodCall|StaticCall|null { if ($node instanceof Closure) { - if (count($node->stmts) !== 1 || ! $node->stmts[0] instanceof Return_) { + if (count($node->getStmts()) !== 1 || ! $node->getStmts()[0] instanceof Return_) { return null; } - $callLike = $node->stmts[0]->expr; + $callLike = $node->getStmts()[0] + ->expr; } else { $callLike = $node->expr; } diff --git a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php index 857ca35b2cb..246852838cc 100644 --- a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php +++ b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php @@ -6,6 +6,7 @@ use PhpParser\Comment; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; @@ -23,7 +24,6 @@ use PhpParser\Node\Stmt\Trait_; use PhpParser\Node\Stmt\TryCatch; use PhpParser\Node\Stmt\While_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Contract\Rector\HTMLAverseRectorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; @@ -95,22 +95,22 @@ public function second() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class, ClassLike::class]; + return [ContainsStmts::class, ClassLike::class]; } /** - * @param StmtsAwareInterface|ClassLike $node + * @param ContainsStmts|ClassLike $node */ - public function refactor(Node $node): null|StmtsAwareInterface|ClassLike + public function refactor(Node $node): null|ContainsStmts|ClassLike { return $this->processAddNewLine($node, false); } private function processAddNewLine( - StmtsAwareInterface|ClassLike $node, + ContainsStmts|ClassLike $node, bool $hasChanged, int $jumpToKey = 0 - ): null|StmtsAwareInterface|ClassLike { + ): null|ContainsStmts|ClassLike { if ($node->stmts === null) { return null; } diff --git a/rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php b/rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php index c81af62584a..acbb3c8887c 100644 --- a/rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php +++ b/rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php @@ -51,7 +51,7 @@ public function getNodeTypes(): array public function refactor(Node $node): null|FileWithoutNamespace|Namespace_ { $hasChanged = false; - foreach ($node->stmts as $stmt) { + foreach ($node->getStmts() as $stmt) { if (! $stmt instanceof Use_) { continue; } diff --git a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php index f1eca7b9ef7..f7a5388363c 100644 --- a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php @@ -190,7 +190,7 @@ private function isUsedByTrait(Trait_ $trait, string $classMethodName, string $c $callMethod = null; $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - (array) $classMethod->stmts, + (array) $classMethod->getStmts(), function (Node $subNode) use ($className, $classMethodName, &$callMethod): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index c85ac41cd20..68af63d8a1e 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -5,12 +5,12 @@ namespace Rector\DeadCode\Rector\Assign; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; @@ -47,11 +47,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php index eeed814e41e..3442acbb12e 100644 --- a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php @@ -81,7 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): null|ClassMethod|Function_ { - $stmts = $node->stmts; + $stmts = $node->getStmts(); if ($stmts === null || $stmts === []) { return null; } @@ -113,7 +113,7 @@ public function refactor(Node $node): null|ClassMethod|Function_ $this->mirrorComments($newExpression, $currentStmt); $node->stmts[$stmtPosition] = $newExpression; } else { - unset($node->stmts[$stmtPosition]); + unset($node->getStmts()[$stmtPosition]); } $hasChanged = true; diff --git a/rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php b/rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php index d57a8a2f2f0..733a98379aa 100644 --- a/rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php +++ b/rules/DeadCode/Rector/Block/ReplaceBlockToItsStmtsRector.php @@ -56,10 +56,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): int|array { - if ($node->stmts === []) { + if ($node->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $node->stmts; + return $node->getStmts(); } } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveArgumentFromDefaultParentCallRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveArgumentFromDefaultParentCallRector.php index f58411325e4..11572372f24 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveArgumentFromDefaultParentCallRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveArgumentFromDefaultParentCallRector.php @@ -117,7 +117,7 @@ public function refactor(Node $node): ?Class_ continue; } - foreach ((array) $classMethod->stmts as $stmt) { + foreach ((array) $classMethod->getStmts() as $stmt) { if (! $stmt instanceof Expression) { continue; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php index d47ee0983a8..c1925e40e33 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php @@ -134,7 +134,7 @@ public function refactor(Node $node): ?Node $hasChanged = true; // is variable used? only remove property, keep param - $variable = $this->betterNodeFinder->findVariableOfName((array) $constructClassMethod->stmts, $paramName); + $variable = $this->betterNodeFinder->findVariableOfName($constructClassMethod->getStmts(), $paramName); if ($variable instanceof Variable) { $param->flags = 0; continue; diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php index e6324ee5a00..12eaa397511 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php @@ -66,7 +66,7 @@ public function refactor(Node $node): ?Node return null; } - if ($node->stmts === null || $node->stmts == []) { + if ($node->getStmts() === [] || $node->getStmts() == []) { return null; } @@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node } foreach ($removeStmtKeys as $removeStmtKey) { - unset($node->stmts[$removeStmtKey]); + unset($node->getStmts()[$removeStmtKey]); } return $node; diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php index c8b1c1a3aae..22a12024067 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php @@ -91,7 +91,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -100,7 +100,7 @@ public function refactor(Node $node): ?Node } $hasChanged = false; - $this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use ( + $this->traverseNodesWithCallable($node->getStmts(), function (Node $subNode) use ( &$hasChanged ): int|null|array|Return_ { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { diff --git a/rules/DeadCode/Rector/Closure/RemoveUnusedClosureVariableUseRector.php b/rules/DeadCode/Rector/Closure/RemoveUnusedClosureVariableUseRector.php index c6e9f0f6488..362a3bdf08a 100644 --- a/rules/DeadCode/Rector/Closure/RemoveUnusedClosureVariableUseRector.php +++ b/rules/DeadCode/Rector/Closure/RemoveUnusedClosureVariableUseRector.php @@ -75,7 +75,7 @@ public function refactor(Node $node): ?Node } $isUseUsed = (bool) $this->betterNodeFinder->findFirst( - $node->stmts, + $node->getStmts(), fn (Node $subNode): bool => $this->exprUsedInNodeAnalyzer->isUsed($subNode, $useVariable->var) ); if ($isUseUsed) { diff --git a/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php b/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php index cc8a10830ec..ed15fd9cc3a 100644 --- a/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php +++ b/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php @@ -174,11 +174,11 @@ private function refactorSmallerRight(Smaller $smaller, If_ $if): null|array|int return null; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $if->stmts; + return $if->getStmts(); } /** @@ -195,11 +195,11 @@ private function refactorGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ return null; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $if->stmts; + return $if->getStmts(); } private function refactorGreaterOrEqualRight(GreaterOrEqual $greaterOrEqual): ?int @@ -246,11 +246,11 @@ private function refactorGreaterLeft(Greater $greater, If_ $if): null|array|int return null; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $if->stmts; + return $if->getStmts(); } private function refactorGreaterRight(Greater $greater): ?int diff --git a/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php b/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php index e2d152f20ad..98bd9e9f78d 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php @@ -58,8 +58,8 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $modified = false; - while ($this->canRemoveLastStatement($node->stmts)) { - array_pop($node->stmts); + while ($this->canRemoveLastStatement($node->getStmts())) { + array_pop($node->getStmts()); $modified = true; } diff --git a/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php b/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php index da26f962428..7970716633a 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\For_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\CallLike; @@ -13,7 +14,6 @@ use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\EarlyReturn\NodeTransformer\ConditionInverter; use Rector\NodeManipulator\StmtsManipulator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -76,15 +76,15 @@ public function run($value, $differentValue) */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): Node|null { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -113,7 +113,7 @@ public function refactor(Node $node): Node|null return null; } - private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware): void + private function processIf(If_ $if, int $key, ContainsStmts $containsStmts): void { if ($if->elseifs !== []) { return; @@ -125,7 +125,7 @@ private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware): return; } - unset($stmtsAware->stmts[$key]); + unset($containsStmts->getStmts()[$key]); $this->hasChanged = true; return; } @@ -137,7 +137,7 @@ private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware): $this->hasChanged = true; } - private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInterface $stmtsAware): void + private function processForForeach(For_|Foreach_ $for, int $key, ContainsStmts $containsStmts): void { if ($for instanceof For_) { $variables = $this->betterNodeFinder->findInstanceOf( @@ -146,7 +146,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter ); foreach ($variables as $variable) { if ($this->stmtsManipulator->isVariableUsedInNextStmt( - $stmtsAware, + $containsStmts, $key + 1, (string) $this->getName($variable) )) { @@ -154,7 +154,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter } } - unset($stmtsAware->stmts[$key]); + unset($containsStmts->getStmts()[$key]); $this->hasChanged = true; return; @@ -164,7 +164,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter $variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class); foreach ($variables as $variable) { if ($this->stmtsManipulator->isVariableUsedInNextStmt( - $stmtsAware, + $containsStmts, $key + 1, (string) $this->getName($variable) )) { @@ -172,7 +172,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter } } - unset($stmtsAware->stmts[$key]); + unset($containsStmts->getStmts()[$key]); $this->hasChanged = true; } diff --git a/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php b/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php index db9e4e9d37c..1db80bbb123 100644 --- a/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php +++ b/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php @@ -69,7 +69,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?int { - if ($node->stmts !== []) { + if ($node->getStmts() !== []) { return null; } diff --git a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php index 20a1e782cc1..f771028c1f1 100644 --- a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php +++ b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeFinder; @@ -12,7 +13,6 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; use Rector\NodeManipulator\StmtsManipulator; use Rector\Rector\AbstractRector; @@ -59,15 +59,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php b/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php index 141279cb8df..89b679ed152 100644 --- a/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php +++ b/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php @@ -74,13 +74,13 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === [] || $node->stmts === null) { + if ($node->getStmts() === [] || $node->getStmts() === []) { return null; } - $lastStmtKey = array_key_last($node->stmts); + $lastStmtKey = array_key_last($node->getStmts()); - $lastStmt = $node->stmts[$lastStmtKey]; + $lastStmt = $node->getStmts()[$lastStmtKey]; if ($lastStmt instanceof If_) { if (! $this->isBareIfWithOnlyStmtEmptyReturn($lastStmt)) { @@ -99,7 +99,7 @@ public function refactor(Node $node): ?Node return null; } - unset($node->stmts[$lastStmtKey]); + unset($node->getStmts()[$lastStmtKey]); return $node; } @@ -113,11 +113,11 @@ private function isBareIfWithOnlyStmtEmptyReturn(If_ $if): bool return false; } - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return false; } - $onlyStmt = $if->stmts[0]; + $onlyStmt = $if->getStmts()[0]; if (! $onlyStmt instanceof Return_) { return false; } diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index 9f772a64473..e9aab02b8d9 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -123,17 +123,19 @@ public function refactor(Node $node): int|null|array|If_ return null; } - if ($node->stmts === []) { + if ($node->getStmts() === []) { 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); + $node->getStmts()[0] + ->setAttribute( + AttributeKey::COMMENTS, + array_merge($node->getComments(), $node->getStmts()[0] ->getComments()) + ); + $node->getStmts()[0] + ->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true); - return $node->stmts; + return $node->getStmts(); } private function shouldSkipFromVariable(Expr $expr): bool diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index c58e99ac8c1..b7f36904950 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -113,19 +113,19 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu if ($instanceof->expr instanceof Assign) { $instanceof->expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, false); $assignExpression = new Expression($instanceof->expr); - return array_merge([$assignExpression], $if->stmts); + return array_merge([$assignExpression], $if->getStmts()); } if ($if->cond !== $instanceof) { return NodeVisitor::REMOVE_NODE; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } // unwrap stmts - return $if->stmts; + return $if->getStmts(); } private function shouldSkipFromNotTypedParam(Instanceof_ $instanceof): bool diff --git a/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php index 6edae6c5282..764a0619654 100644 --- a/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php @@ -177,11 +177,11 @@ private function refactorStmtAndInstanceof(Class_ $class, If_ $if, Instanceof_ $ return NodeVisitor::REMOVE_NODE; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $if->stmts; + return $if->getStmts(); } private function isSkippedPropertyFetch(Class_ $class, PropertyFetch|StaticPropertyFetch $propertyFetch): bool diff --git a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php index d328de2e17c..10c7a390769 100644 --- a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php +++ b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PHPStan\Analyser\Scope; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeManipulator\CountManipulator; use Rector\DeadCode\UselessIfCondBeforeForeachDetector; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; @@ -83,12 +83,12 @@ public function run() */ public function getNodeTypes(): array { - return [If_::class, StmtsAwareInterface::class]; + return [If_::class, ContainsStmts::class]; } /** - * @param If_|StmtsAwareInterface $node - * @return Foreach_|StmtsAwareInterface|null + * @param If_|ContainsStmts $node + * @return Foreach_|ContainsStmts|null */ public function refactor(Node $node): Node|null { @@ -107,7 +107,7 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool } /** @var Foreach_ $foreach */ - $foreach = $if->stmts[0]; + $foreach = $if->getStmts()[0]; $foreachExpr = $foreach->expr; if ($this->shouldSkipForeachExpr($foreachExpr, $scope)) { @@ -151,24 +151,24 @@ private function isUselessBooleanAnd(BooleanAnd $booleanAnd, Expr $foreachExpr): return $this->countManipulator->isCounterHigherThanOne($booleanAnd->right, $foreachExpr); } - private function refactorStmtsAware(StmtsAwareInterface $stmtsAware): ?StmtsAwareInterface + private function refactorStmtsAware(ContainsStmts $containsStmts): ?ContainsStmts { - if ($stmtsAware->stmts === null) { + if ($containsStmts->getStmts() === []) { return null; } - foreach ($stmtsAware->stmts as $key => $stmt) { + foreach ($containsStmts->stmts as $key => $stmt) { if (! $stmt instanceof Foreach_) { continue; } - $previousStmt = $stmtsAware->stmts[$key - 1] ?? null; + $previousStmt = $containsStmts->stmts[$key - 1] ?? null; if (! $previousStmt instanceof If_) { continue; } // not followed by any stmts - $nextStmt = $stmtsAware->stmts[$key + 1] ?? null; + $nextStmt = $containsStmts->stmts[$key + 1] ?? null; if ($nextStmt instanceof Stmt) { continue; } @@ -193,8 +193,8 @@ private function refactorStmtsAware(StmtsAwareInterface $stmtsAware): ?StmtsAwar continue; } - unset($stmtsAware->stmts[$key - 1]); - return $stmtsAware; + unset($containsStmts->stmts[$key - 1]); + return $containsStmts; } return null; @@ -207,7 +207,7 @@ private function refactorIf(If_ $if, Scope $scope): ?Foreach_ } /** @var Foreach_ $stmt */ - $stmt = $if->stmts[0]; + $stmt = $if->getStmts()[0]; $ifComments = $if->getAttribute(AttributeKey::COMMENTS) ?? []; $stmtComments = $stmt->getAttribute(AttributeKey::COMMENTS) ?? []; diff --git a/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php b/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php index 829bf2405a2..d44da5ee7eb 100644 --- a/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php +++ b/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php @@ -78,20 +78,20 @@ public function refactor(Node $node): int|null|array return null; } - if ($node->stmts === []) { + if ($node->getStmts() === []) { return NodeVisitor::REMOVE_NODE; } - return $node->stmts; + return $node->getStmts(); } private function isIfWithConstantReturns(If_ $if): bool { $possibleContents = []; - $possibleContents[] = $this->betterStandardPrinter->print($if->stmts); + $possibleContents[] = $this->betterStandardPrinter->print($if->getStmts()); foreach ($if->elseifs as $elseif) { - $possibleContents[] = $this->betterStandardPrinter->print($elseif->stmts); + $possibleContents[] = $this->betterStandardPrinter->print($elseif->getStmts()); } $else = $if->else; @@ -99,7 +99,7 @@ private function isIfWithConstantReturns(If_ $if): bool throw new ShouldNotHappenException(); } - $possibleContents[] = $this->betterStandardPrinter->print($else->stmts); + $possibleContents[] = $this->betterStandardPrinter->print($else->getStmts()); $uniqueContents = array_unique($possibleContents); diff --git a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php index 2e1f70233a8..164f8ee8d38 100644 --- a/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php +++ b/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php @@ -100,7 +100,7 @@ private function refactorIsMatch(If_ $if): ?array return null; } - return $if->stmts; + return $if->getStmts(); } /** @@ -114,6 +114,6 @@ private function refactorIsNotMatch(If_ $if): array|int } // else is always used - return $if->else->stmts; + return $if->else->getStmts(); } } diff --git a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php index 6278b4977f7..399910a858f 100644 --- a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php +++ b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\Node; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; @@ -25,7 +26,6 @@ use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\StmtsManipulator; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; @@ -98,15 +98,15 @@ public function get() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -190,7 +190,7 @@ public function refactor(Node $node): ?Node /** * @param string[] $extractValues */ - private function shouldSkip(StmtsAwareInterface $stmtsAware, int $key, Stmt $stmt, array $extractValues): bool + private function shouldSkip(ContainsStmts $containsStmts, int $key, Stmt $stmt, array $extractValues): bool { if (! in_array($stmt::class, self::NODE_TYPES, true)) { return true; @@ -201,12 +201,12 @@ private function shouldSkip(StmtsAwareInterface $stmtsAware, int $key, Stmt $stm } foreach ($extractValues as $extractValue) { - if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, $extractValue)) { + if ($this->stmtsManipulator->isVariableUsedInNextStmt($containsStmts, $key + 1, $extractValue)) { return true; } } - return isset($stmtsAware->stmts[$key + 1]) && $stmtsAware->stmts[$key + 1] instanceof InlineHTML; + return isset($containsStmts->getStmts()[$key + 1]) && $containsStmts->getStmts()[$key + 1] instanceof InlineHTML; } private function hasVariableName(Stmt $stmt, string $variableName): bool diff --git a/rules/DeadCode/Rector/Return_/RemoveDeadConditionAboveReturnRector.php b/rules/DeadCode/Rector/Return_/RemoveDeadConditionAboveReturnRector.php index 22e8a820d0c..9ad533f6f99 100644 --- a/rules/DeadCode/Rector/Return_/RemoveDeadConditionAboveReturnRector.php +++ b/rules/DeadCode/Rector/Return_/RemoveDeadConditionAboveReturnRector.php @@ -5,11 +5,11 @@ namespace Rector\DeadCode\Rector\Return_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -61,20 +61,20 @@ public function go() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { - foreach ((array) $node->stmts as $key => $stmt) { + foreach ((array) $node->getStmts() as $key => $stmt) { if (! $stmt instanceof Return_) { continue; } - $previousNode = $node->stmts[$key - 1] ?? null; + $previousNode = $node->getStmts()[$key - 1] ?? null; if (! $this->isBareIf($previousNode)) { continue; } @@ -84,9 +84,9 @@ public function refactor(Node $node): ?StmtsAwareInterface continue; } - $countStmt = count($previousNode->stmts); + $countStmt = count($previousNode->getStmts()); if ($countStmt === 0) { - unset($node->stmts[$key - 1]); + unset($node->getStmts()[$key - 1]); return $node; } @@ -94,7 +94,7 @@ public function refactor(Node $node): ?StmtsAwareInterface return null; } - $previousFirstStmt = $previousNode->stmts[0]; + $previousFirstStmt = $previousNode->getStmts()[0]; if (! $previousFirstStmt instanceof Return_) { return null; } @@ -103,7 +103,7 @@ public function refactor(Node $node): ?StmtsAwareInterface return null; } - unset($node->stmts[$key - 1]); + unset($node->getStmts()[$key - 1]); return $node; } diff --git a/rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php b/rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php index c989a42642a..35ebbba78b0 100644 --- a/rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php +++ b/rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php @@ -81,7 +81,7 @@ public function refactor(Node $node): ?Node $hasChanged = false; foreach ($node->getMethods() as $classMethod) { - if ($classMethod->stmts === null) { + if ($classMethod->getStmts() === []) { continue; } diff --git a/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php b/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php index 73930f12c8e..55298b3e61b 100644 --- a/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php +++ b/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php @@ -5,12 +5,12 @@ namespace Rector\DeadCode\Rector\Stmt; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -67,15 +67,15 @@ public function __construct(array $items) */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -133,11 +133,11 @@ public function refactor(Node $node): ?Node private function matchSoleIfReturn(If_ $if): ?Return_ { - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return null; } - $soleIfStmt = $if->stmts[0]; + $soleIfStmt = $if->getStmts()[0]; if (! $soleIfStmt instanceof Return_) { return null; } diff --git a/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php b/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php index 1ce39353c00..47692bfa719 100644 --- a/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php +++ b/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php @@ -5,8 +5,8 @@ namespace Rector\DeadCode\Rector\Stmt; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Stmt; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\TerminatedNodeAnalyzer; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -56,25 +56,25 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } // at least 2 items are needed - if (count($node->stmts) < 2) { + if (count($node->getStmts()) < 2) { return null; } - $originalStmts = $node->stmts; - $cleanedStmts = $this->processCleanUpUnreachableStmts($node, $node->stmts); + $originalStmts = $node->getStmts(); + $cleanedStmts = $this->processCleanUpUnreachableStmts($node, $node->getStmts()); if ($cleanedStmts === $originalStmts) { return null; @@ -88,7 +88,7 @@ public function refactor(Node $node): ?Node * @param Stmt[] $stmts * @return Stmt[] */ - private function processCleanUpUnreachableStmts(StmtsAwareInterface $stmtsAware, array $stmts): array + private function processCleanUpUnreachableStmts(ContainsStmts $containsStmts, array $stmts): array { foreach ($stmts as $key => $stmt) { if (! isset($stmts[$key - 1])) { @@ -99,7 +99,7 @@ private function processCleanUpUnreachableStmts(StmtsAwareInterface $stmtsAware, // unset... - if ($this->terminatedNodeAnalyzer->isAlwaysTerminated($stmtsAware, $previousStmt, $stmt)) { + if ($this->terminatedNodeAnalyzer->isAlwaysTerminated($containsStmts, $previousStmt, $stmt)) { array_splice($stmts, $key); return $stmts; } diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index 2461eff9636..89263bf8a37 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -115,7 +115,7 @@ private function removeDuplicatedCases(Switch_ $switch): void $processedCasesKeys[] = $outerCaseKey; - if ($outerCase->stmts === []) { + if ($outerCase->getStmts() === []) { $result[] = $outerCase; continue; } @@ -131,7 +131,7 @@ private function removeDuplicatedCases(Switch_ $switch): void continue; } - if ($innerCase->stmts === []) { + if ($innerCase->getStmts() === []) { $casesWithoutStmts[$innerCaseKey] = $innerCase; continue; } @@ -172,15 +172,15 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex * Skip multi no stmts * @see rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/skip_multi_no_stmts.php.inc */ - if ($currentCase->stmts[0] instanceof Break_ && $nextCase->stmts[0] instanceof Break_) { + if ($currentCase->getStmts()[0] instanceof Break_ && $nextCase->getStmts()[0] instanceof Break_) { return $this->areSwitchStmtsEqualsConsideringComments($currentCase, $nextCase); } - if (! $this->nodeComparator->areNodesEqual($currentCase->stmts, $nextCase->stmts)) { + if (! $this->nodeComparator->areNodesEqual($currentCase->getStmts(), $nextCase->getStmts())) { return false; } - foreach ($currentCase->stmts as $stmt) { + foreach ($currentCase->getStmts() as $stmt) { if ($stmt instanceof Break_ || $stmt instanceof Return_) { return true; } @@ -191,8 +191,8 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex private function areSwitchStmtsEqualsConsideringComments(Case_ $currentCase, Case_ $nextCase): bool { - $currentCasePrintResult = $this->betterStandardPrinter->print($currentCase->stmts); - $nextCasePrintResult = $this->betterStandardPrinter->print($nextCase->stmts); + $currentCasePrintResult = $this->betterStandardPrinter->print($currentCase->getStmts()); + $nextCasePrintResult = $this->betterStandardPrinter->print($nextCase->getStmts()); return $currentCasePrintResult === $nextCasePrintResult; } diff --git a/rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php b/rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php index d8859548602..f53aaed763a 100644 --- a/rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php +++ b/rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php @@ -105,11 +105,11 @@ public function refactor(Node $node): ?Node private function isJustThrownSameVariable(Catch_ $catch): bool { - if ($this->isEmpty($catch->stmts)) { + if ($this->isEmpty($catch->getStmts())) { return false; } - $catchItemStmt = $catch->stmts[0]; + $catchItemStmt = $catch->getStmts()[0]; if (! ($catchItemStmt instanceof Expression && $catchItemStmt->expr instanceof Throw_)) { return false; } diff --git a/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php b/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php index de2d51d8883..e147f913122 100644 --- a/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php +++ b/rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php @@ -66,14 +66,14 @@ public function getNodeTypes(): array */ public function refactor(Node $node): array|null|int { - $isEmptyFinallyStmts = ! $node->finally instanceof Finally_ || $this->isEmpty($node->finally->stmts); + $isEmptyFinallyStmts = ! $node->finally instanceof Finally_ || $this->isEmpty($node->finally->getStmts()); // not empty stmts on finally always executed if (! $isEmptyFinallyStmts) { return null; } - if ($this->isEmpty($node->stmts)) { + if ($this->isEmpty($node->getStmts())) { return NodeVisitor::REMOVE_NODE; } @@ -82,11 +82,11 @@ public function refactor(Node $node): array|null|int } $onlyCatch = $node->catches[0]; - if ($this->isEmpty($onlyCatch->stmts)) { + if ($this->isEmpty($onlyCatch->getStmts())) { return null; } - $onlyCatchStmt = $onlyCatch->stmts[0]; + $onlyCatchStmt = $onlyCatch->getStmts()[0]; if (! ($onlyCatchStmt instanceof Expression && $onlyCatchStmt->expr instanceof Throw_)) { return null; } @@ -95,7 +95,7 @@ public function refactor(Node $node): array|null|int return null; } - return $node->stmts; + return $node->getStmts(); } /** diff --git a/rules/DeadCode/UselessIfCondBeforeForeachDetector.php b/rules/DeadCode/UselessIfCondBeforeForeachDetector.php index fba53d67cec..075131d5638 100644 --- a/rules/DeadCode/UselessIfCondBeforeForeachDetector.php +++ b/rules/DeadCode/UselessIfCondBeforeForeachDetector.php @@ -39,15 +39,15 @@ public function isMatchingEmptyAndForeachedExpr(If_ $if, Expr $foreachExpr): boo return false; } - if ($if->stmts === []) { + if ($if->getStmts() === []) { return true; } - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return false; } - $stmt = $if->stmts[0]; + $stmt = $if->getStmts()[0]; return $stmt instanceof Return_ && ! $stmt->expr instanceof Expr; } diff --git a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php index 5487e37b9da..d81f1bd5e4e 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php @@ -5,13 +5,13 @@ namespace Rector\EarlyReturn\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\IfManipulator; use Rector\NodeManipulator\StmtsManipulator; use Rector\Rector\AbstractRector; @@ -70,15 +70,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php index 47a6fb3a806..fa4e3dd5fa0 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php @@ -5,11 +5,11 @@ namespace Rector\EarlyReturn\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\EarlyReturn\NodeTransformer\ConditionInverter; use Rector\NodeManipulator\IfManipulator; use Rector\Rector\AbstractRector; @@ -73,15 +73,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php b/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php index 300d8bde87b..d84f85fffa6 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php @@ -101,7 +101,7 @@ private function processMultiIfContinue(If_ $if): ?array { $node = clone $if; /** @var Continue_ $continue */ - $continue = $if->stmts[0]; + $continue = $if->getStmts()[0]; $ifs = $this->createMultipleIfs($if->cond, $continue, []); // ensure ifs not removed by other rules diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index 44da436638c..2c04d9e43ec 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -83,7 +83,7 @@ public function refactor(Node $node): ?array } if ($node->else instanceof Else_) { - $stmts = $node->else->stmts; + $stmts = $node->else->getStmts(); $node->else = null; return [$node, ...$stmts]; @@ -143,7 +143,7 @@ private function getStatementsElseIfs(If_ $if): array { $statements = []; foreach ($if->elseifs as $key => $elseif) { - if ($this->doesLastStatementBreakFlow($elseif) && $elseif->stmts !== []) { + if ($this->doesLastStatementBreakFlow($elseif) && $elseif->getStmts() !== []) { continue; } @@ -156,7 +156,7 @@ private function getStatementsElseIfs(If_ $if): array private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool { - $lastStmt = end($node->stmts); + $lastStmt = end($node->getStmts()); if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) { if ($this->doesLastStatementBreakFlow($lastStmt) || $this->doesLastStatementBreakFlow($lastStmt->else)) { diff --git a/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php b/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php index 12d07352976..6dd222af073 100644 --- a/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php @@ -5,6 +5,7 @@ namespace Rector\EarlyReturn\Rector\Return_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\AssignOp; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\While_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\EarlyReturn\ValueObject\BareSingleAssignIf; use Rector\NodeManipulator\IfManipulator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -87,15 +87,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -185,11 +185,11 @@ public function refactor(Node $node): ?StmtsAwareInterface * @param If_[] $ifs * @return BareSingleAssignIf[] */ - private function getMatchingBareSingleAssignIfs(array $ifs, StmtsAwareInterface $stmtsAware): array + private function getMatchingBareSingleAssignIfs(array $ifs, ContainsStmts $containsStmts): array { $bareSingleAssignIfs = []; foreach ($ifs as $key => $if) { - $bareSingleAssignIf = $this->matchBareSingleAssignIf($if, $key, $stmtsAware); + $bareSingleAssignIf = $this->matchBareSingleAssignIf($if, $key, $containsStmts); if (! $bareSingleAssignIf instanceof BareSingleAssignIf) { return []; @@ -233,18 +233,18 @@ private function isVariableSharedInAssignIfsAndReturn( return true; } - private function matchBareSingleAssignIf(Stmt $stmt, int $key, StmtsAwareInterface $stmtsAware): ?BareSingleAssignIf + private function matchBareSingleAssignIf(Stmt $stmt, int $key, ContainsStmts $containsStmts): ?BareSingleAssignIf { if (! $stmt instanceof If_) { return null; } // is exactly single stmt - if (count($stmt->stmts) !== 1) { + if (count($stmt->getStmts()) !== 1) { return null; } - $onlyStmt = $stmt->stmts[0]; + $onlyStmt = $stmt->getStmts()[0]; if (! $onlyStmt instanceof Expression) { return null; } @@ -258,15 +258,15 @@ private function matchBareSingleAssignIf(Stmt $stmt, int $key, StmtsAwareInterfa return null; } - if (! isset($stmtsAware->stmts[$key + 1])) { + if (! isset($containsStmts->getStmts()[$key + 1])) { return null; } - if ($stmtsAware->stmts[$key + 1] instanceof If_) { + if ($containsStmts->getStmts()[$key + 1] instanceof If_) { return new BareSingleAssignIf($stmt, $expression->expr); } - if ($stmtsAware->stmts[$key + 1] instanceof Return_) { + if ($containsStmts->getStmts()[$key + 1] instanceof Return_) { return new BareSingleAssignIf($stmt, $expression->expr); } @@ -277,14 +277,14 @@ private function matchBareSingleAssignIf(Stmt $stmt, int $key, StmtsAwareInterfa * @param BareSingleAssignIf[] $bareSingleAssignIfs */ private function refactorToDirectReturns( - StmtsAwareInterface $stmtsAware, + ContainsStmts $containsStmts, int $initialAssignPosition, array $bareSingleAssignIfs, Assign $initialAssign, Return_ $return - ): StmtsAwareInterface { + ): ContainsStmts { // 1. remove initial assign - unset($stmtsAware->stmts[$initialAssignPosition]); + unset($containsStmts->getStmts()[$initialAssignPosition]); // 2. make ifs early return foreach ($bareSingleAssignIfs as $bareSingleAssignIf) { @@ -295,6 +295,6 @@ private function refactorToDirectReturns( // 3. make return default value $return->expr = $initialAssign->expr; - return $stmtsAware; + return $containsStmts; } } diff --git a/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php b/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php index 83ba2deaf3d..8fa64eb2353 100644 --- a/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php @@ -5,12 +5,12 @@ namespace Rector\EarlyReturn\Rector\Return_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\CallAnalyzer; use Rector\PhpParser\Node\AssignAndBinaryMap; use Rector\Rector\AbstractRector; @@ -64,11 +64,11 @@ public function accept() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php b/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php index ba4f26cf053..33e612841d0 100644 --- a/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php +++ b/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php @@ -5,6 +5,7 @@ namespace Rector\EarlyReturn\Rector\StmtsAwareInterface; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\VariableAnalyzer; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -73,15 +73,15 @@ public function run($value) */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - $stmts = (array) $node->stmts; + $stmts = (array) $node->getStmts(); foreach ($stmts as $key => $stmt) { $returnVariable = $this->matchNextStmtReturnVariable($node, $key); @@ -92,18 +92,18 @@ public function refactor(Node $node): ?Node if ($stmt instanceof If_ && ! $stmt->else instanceof Else_ && $stmt->elseifs === []) { // is single condition if $if = $stmt; - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { continue; } - $onlyIfStmt = $if->stmts[0]; + $onlyIfStmt = $if->getStmts()[0]; $assignedExpr = $this->matchOnlyIfStmtReturnExpr($onlyIfStmt, $returnVariable); if (! $assignedExpr instanceof Expr) { continue; } $if->stmts[0] = new Return_($assignedExpr); - $this->mirrorComments($if->stmts[0], $onlyIfStmt); + $this->mirrorComments($if->getStmts()[0], $onlyIfStmt); return $node; } @@ -150,9 +150,9 @@ private function matchOnlyIfStmtReturnExpr(Stmt $onlyIfStmt, Variable $returnVar return $assign->expr; } - private function matchNextStmtReturnVariable(StmtsAwareInterface $stmtsAware, int $key): Variable|null + private function matchNextStmtReturnVariable(ContainsStmts $containsStmts, int $key): Variable|null { - $nextStmt = $stmtsAware->stmts[$key + 1] ?? null; + $nextStmt = $containsStmts->getStmts()[$key + 1] ?? null; // last item → stop if (! $nextStmt instanceof Stmt) { diff --git a/rules/Naming/Guard/BreakingVariableRenameGuard.php b/rules/Naming/Guard/BreakingVariableRenameGuard.php index 4030085a807..e8ff29315b2 100644 --- a/rules/Naming/Guard/BreakingVariableRenameGuard.php +++ b/rules/Naming/Guard/BreakingVariableRenameGuard.php @@ -125,7 +125,7 @@ public function shouldSkipParam( return true; } - return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->getStmts(), function (Node $node) use ( + return (bool) $this->betterNodeFinder->findFirst($classMethod->getStmts(), function (Node $node) use ( $expectedName ): bool { if (! $node instanceof Variable) { @@ -164,7 +164,7 @@ private function hasConflictVariable( } return $this->betterNodeFinder->hasInstanceOfName( - [...(array) $functionLike->stmts, ...$functionLike->params], + [...$functionLike->getStmts(), ...$functionLike->params], Variable::class, $newName ); diff --git a/rules/Naming/Naming/ConflictingNameResolver.php b/rules/Naming/Naming/ConflictingNameResolver.php index 136294896e5..a6b097c674a 100644 --- a/rules/Naming/Naming/ConflictingNameResolver.php +++ b/rules/Naming/Naming/ConflictingNameResolver.php @@ -90,7 +90,7 @@ private function resolveForNewAssigns(ClassMethod | Function_ | Closure | ArrowF $names = []; /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->getStmts(), Assign::class); + $assigns = $this->betterNodeFinder->findInstanceOf($functionLike->getStmts(), Assign::class); foreach ($assigns as $assign) { $name = $this->expectedNameResolver->resolveForAssignNew($assign); if ($name === null) { @@ -111,7 +111,7 @@ private function resolveForNonNewAssigns(ClassMethod | Function_ | Closure | Arr $names = []; /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->getStmts(), Assign::class); + $assigns = $this->betterNodeFinder->findInstanceOf($functionLike->getStmts(), Assign::class); foreach ($assigns as $assign) { $name = $this->expectedNameResolver->resolveForAssignNonNew($assign); if ($name === null) { diff --git a/rules/Naming/Naming/OverriddenExistingNamesResolver.php b/rules/Naming/Naming/OverriddenExistingNamesResolver.php index f890b14c07d..dad9b7ad189 100644 --- a/rules/Naming/Naming/OverriddenExistingNamesResolver.php +++ b/rules/Naming/Naming/OverriddenExistingNamesResolver.php @@ -41,7 +41,7 @@ public function hasNameInFunctionLikeForParam( ClassMethod|Function_|Closure|ArrowFunction $classMethod ): bool { /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->getStmts(), Assign::class); + $assigns = $this->betterNodeFinder->findInstanceOf($classMethod->getStmts(), Assign::class); $usedVariableNames = []; foreach ($assigns as $assign) { @@ -74,7 +74,7 @@ private function resolveOverriddenNamesForNew(ClassMethod | Function_ | Closure $currentlyUsedNames = []; /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class); + $assigns = $this->betterNodeFinder->findInstanceOf($functionLike->getStmts(), Assign::class); foreach ($assigns as $assign) { /** @var Variable $assignVariable */ diff --git a/rules/Naming/Naming/UseImportsResolver.php b/rules/Naming/Naming/UseImportsResolver.php index 001c22665af..d82995ce04f 100644 --- a/rules/Naming/Naming/UseImportsResolver.php +++ b/rules/Naming/Naming/UseImportsResolver.php @@ -31,7 +31,7 @@ public function resolve(): array } return array_filter( - $namespace->stmts, + $namespace->getStmts(), static fn (Stmt $stmt): bool => $stmt instanceof Use_ || $stmt instanceof GroupUse ); } @@ -47,7 +47,7 @@ public function resolveBareUses(): array return []; } - return array_filter($namespace->stmts, static fn (Stmt $stmt): bool => $stmt instanceof Use_); + return array_filter($namespace->getStmts(), static fn (Stmt $stmt): bool => $stmt instanceof Use_); } public function resolvePrefix(Use_|GroupUse $use): string diff --git a/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php index c3fe0241608..a9d56b9812d 100644 --- a/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php @@ -104,12 +104,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } $hasChanged = false; - foreach ($node->stmts as $stmt) { + foreach ($node->getStmts() as $stmt) { if (! $stmt instanceof Expression) { continue; } diff --git a/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php b/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php index 562a9cdba2a..c0ec669f4f4 100644 --- a/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php +++ b/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node private function getAssignsOfNew(ClassMethod $classMethod): array { /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class); + $assigns = $this->betterNodeFinder->findInstanceOf($classMethod->getStmts(), Assign::class); return array_filter($assigns, static fn (Assign $assign): bool => $assign->expr instanceof New_); } diff --git a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php index 460faad02ad..9043550cf29 100644 --- a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php +++ b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php @@ -5,9 +5,9 @@ namespace Rector\Naming\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Foreach_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeManipulator\StmtsManipulator; @@ -69,15 +69,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -147,7 +147,7 @@ public function refactor(Node $node): ?Node private function processRename(Foreach_ $foreach, string $valueVarName, string $singularValueVarName): void { $foreach->valueVar = new Variable($singularValueVarName); - $this->traverseNodesWithCallable($foreach->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($foreach->getStmts(), function (Node $node) use ( $singularValueVarName, $valueVarName ): ?Variable { diff --git a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php index 824d41a8cc7..6d623882b59 100644 --- a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php @@ -90,13 +90,13 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } $hasRenamed = false; $this->traverseNodesWithCallable( - $node->stmts, + $node->getStmts(), function (Node $subNode) use ($node, &$hasRenamed): ?int { if ($subNode instanceof Class_ || $subNode instanceof Closure || $subNode instanceof Function_) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; diff --git a/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php b/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php index 8b21423f11e..ce601ad946a 100644 --- a/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php +++ b/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php @@ -5,6 +5,7 @@ namespace Rector\Php52\Rector\Switch_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Variable; @@ -21,7 +22,6 @@ use PhpParser\Node\Stmt\While_; use PhpParser\NodeVisitor; use PHPStan\Type\Constant\ConstantIntegerType; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -107,7 +107,7 @@ public function refactor(Node $node): ?Switch_ return $node; } - private function processContinueStatement(Stmt|StmtsAwareInterface $stmt): void + private function processContinueStatement(Stmt|ContainsStmts $stmt): void { $this->traverseNodesWithCallable( $stmt, diff --git a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php index 2cfd85ba834..c3e2f584413 100644 --- a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php +++ b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php @@ -113,7 +113,7 @@ public function refactor(Node $node): Class_|null $psr4ConstructorMethod->name = new Identifier(MethodName::CONSTRUCT); } - $classMethodStmts = $psr4ConstructorMethod->stmts; + $classMethodStmts = $psr4ConstructorMethod->getStmts(); if ($classMethodStmts === null) { return null; } @@ -149,11 +149,11 @@ public function refactor(Node $node): Class_|null private function processClassMethodStatementsForParentConstructorCalls(ClassMethod $classMethod, Scope $scope): void { - if (! is_iterable($classMethod->stmts)) { + if (! is_iterable($classMethod->getStmts())) { return; } - foreach ($classMethod->stmts as $methodStmt) { + foreach ($classMethod->getStmts() as $methodStmt) { if (! $methodStmt instanceof Expression) { continue; } diff --git a/rules/Php70/Rector/If_/IfToSpaceshipRector.php b/rules/Php70/Rector/If_/IfToSpaceshipRector.php index 6cde006b02c..af3c3d7d9eb 100644 --- a/rules/Php70/Rector/If_/IfToSpaceshipRector.php +++ b/rules/Php70/Rector/If_/IfToSpaceshipRector.php @@ -5,6 +5,7 @@ namespace Rector\Php70\Rector\If_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -13,7 +14,6 @@ use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Php70\Enum\BattleshipCompareOrder; use Rector\Php70\NodeAnalyzer\BattleshipTernaryAnalyzer; use Rector\Php70\ValueObject\ComparedExprs; @@ -65,11 +65,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class, If_::class]; + return [ContainsStmts::class, If_::class]; } /** - * @param StmtsAwareInterface|If_ $node + * @param ContainsStmts|If_ $node */ public function refactor(Node $node): ?Node { @@ -77,7 +77,7 @@ public function refactor(Node $node): ?Node return $this->refactorIf($node); } - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -167,11 +167,11 @@ private function matchExprComparedExprsReturnZero(If_ $if): ?ComparedExprs } $binaryOp = $if->cond; - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return null; } - $onlyStmt = $if->stmts[0]; + $onlyStmt = $if->getStmts()[0]; if (! $onlyStmt instanceof Return_) { return null; } @@ -207,11 +207,11 @@ private function createReturnSpaceship(?string $battleshipCompareOrder, Compared private function matchElseOnlyStmtTernary(Else_ $else): Ternary|null { - if (count($else->stmts) !== 1) { + if (count($else->getStmts()) !== 1) { return null; } - $onlyElseStmt = $else->stmts[0]; + $onlyElseStmt = $else->getStmts()[0]; if (! $onlyElseStmt instanceof Return_) { return null; } diff --git a/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php index f0bea02d07d..827505632a2 100644 --- a/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php +++ b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php @@ -5,6 +5,7 @@ namespace Rector\Php70\Rector\StmtsAwareInterface; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\Isset_; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -65,15 +65,15 @@ public function resolve($key) */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -135,10 +135,10 @@ private function matchBareIfOnlyStmt(If_ $if): ?Stmt return null; } - if (count($if->stmts) !== 1) { + if (count($if->getStmts()) !== 1) { return null; } - return $if->stmts[0]; + return $if->getStmts()[0]; } } diff --git a/rules/Php71/Rector/Assign/AssignArrayToStringRector.php b/rules/Php71/Rector/Assign/AssignArrayToStringRector.php index 532c30c0e55..84a005677d8 100644 --- a/rules/Php71/Rector/Assign/AssignArrayToStringRector.php +++ b/rules/Php71/Rector/Assign/AssignArrayToStringRector.php @@ -84,13 +84,13 @@ public function refactor(Node $node): ?Node return $this->refactorClass($node); } - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } $hasChanged = false; $this->traverseNodesWithCallable( - $node->stmts, + $node->getStmts(), function (Node $subNode) use (&$hasChanged, $node): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; @@ -173,7 +173,7 @@ private function findSameNamedVariableAssigns( Variable $variable, Namespace_|FileWithoutNamespace|ClassMethod|Function_|Closure $node ): array { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return []; } @@ -184,7 +184,7 @@ private function findSameNamedVariableAssigns( $assignedArrayDimFetches = []; - $this->traverseNodesWithCallable($node->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($node->getStmts(), function (Node $node) use ( $variable, $variableName, &$assignedArrayDimFetches diff --git a/rules/Php71/Rector/TryCatch/MultiExceptionCatchRector.php b/rules/Php71/Rector/TryCatch/MultiExceptionCatchRector.php index 0fb994cd3e2..1eff4a75565 100644 --- a/rules/Php71/Rector/TryCatch/MultiExceptionCatchRector.php +++ b/rules/Php71/Rector/TryCatch/MultiExceptionCatchRector.php @@ -75,8 +75,8 @@ public function refactor(Node $node): ?Node break; } - $currentPrintedCatch = $this->betterStandardPrinter->print($catch->stmts); - $nextPrintedCatch = $this->betterStandardPrinter->print($node->catches[$key + 1]->stmts); + $currentPrintedCatch = $this->betterStandardPrinter->print($catch->getStmts()); + $nextPrintedCatch = $this->betterStandardPrinter->print($node->catches[$key + 1]->getStmts()); // already duplicated catch → remove it and join the type if ($currentPrintedCatch === $nextPrintedCatch) { diff --git a/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php b/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php index aa59bec5a58..f1b133a7be2 100644 --- a/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php +++ b/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php @@ -6,11 +6,11 @@ use PhpParser\Node; use PhpParser\Node\Arg; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -52,33 +52,33 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { return $this->processStrWithResult($node, false); } private function processStrWithResult( - StmtsAwareInterface $stmtsAware, + ContainsStmts $containsStmts, bool $hasChanged, int $jumpToKey = 0 - ): null|StmtsAwareInterface { - if ($stmtsAware->stmts === null) { + ): null|ContainsStmts { + if ($containsStmts->getStmts() === []) { return null; } - $totalKeys = array_key_last($stmtsAware->stmts); + $totalKeys = array_key_last($containsStmts->getStmts()); for ($key = $jumpToKey; $key < $totalKeys; ++$key) { - if (! isset($stmtsAware->stmts[$key], $stmtsAware->stmts[$key + 1])) { + if (! isset($containsStmts->getStmts()[$key], $containsStmts->getStmts()[$key + 1])) { break; } - $stmt = $stmtsAware->stmts[$key]; + $stmt = $containsStmts->getStmts()[$key]; if ($this->shouldSkip($stmt)) { continue; } @@ -89,7 +89,7 @@ private function processStrWithResult( $resultVariable = new Variable('result'); $expr->args[1] = new Arg($resultVariable); - $nextExpression = $stmtsAware->stmts[$key + 1]; + $nextExpression = $containsStmts->getStmts()[$key + 1]; $this->traverseNodesWithCallable($nextExpression, function (Node $node) use ( $resultVariable, &$hasChanged @@ -106,11 +106,11 @@ private function processStrWithResult( return $resultVariable; }); - return $this->processStrWithResult($stmtsAware, $hasChanged, $key + 2); + return $this->processStrWithResult($containsStmts, $hasChanged, $key + 2); } if ($hasChanged) { - return $stmtsAware; + return $containsStmts; } return null; diff --git a/rules/Php72/Rector/While_/WhileEachToForeachRector.php b/rules/Php72/Rector/While_/WhileEachToForeachRector.php index ef400d36b9a..4d8fab2a528 100644 --- a/rules/Php72/Rector/While_/WhileEachToForeachRector.php +++ b/rules/Php72/Rector/While_/WhileEachToForeachRector.php @@ -115,7 +115,7 @@ public function refactor(Node $node): ?Node } $foreach = new Foreach_($foreachedExpr, $arrayItem->value, [ - 'stmts' => $node->stmts, + 'stmts' => $node->getStmts(), ]); $this->mirrorComments($foreach, $node); diff --git a/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php b/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php index 63a65592cf8..793a46aaed5 100644 --- a/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php +++ b/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php @@ -5,12 +5,12 @@ namespace Rector\Php73\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PHPStan\Reflection\ReflectionProvider; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; @@ -87,13 +87,13 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ - public function refactor(Node $node): ?StmtsAwareInterface + public function refactor(Node $node): ?ContainsStmts { return $this->processArrayKeyFirstLast($node); } @@ -108,32 +108,30 @@ public function providePolyfillPackage(): string return PolyfillPackage::PHP_73; } - private function processArrayKeyFirstLast( - StmtsAwareInterface $stmtsAware, - int $jumpToKey = 0 - ): ?StmtsAwareInterface { - if ($stmtsAware->stmts === null) { + private function processArrayKeyFirstLast(ContainsStmts $containsStmts, int $jumpToKey = 0): ?ContainsStmts + { + if ($containsStmts->getStmts() === []) { return null; } /** @var int $totalKeys */ - $totalKeys = array_key_last($stmtsAware->stmts); + $totalKeys = array_key_last($containsStmts->getStmts()); for ($key = $jumpToKey; $key < $totalKeys; ++$key) { - if (! isset($stmtsAware->stmts[$key], $stmtsAware->stmts[$key + 1])) { + if (! isset($containsStmts->getStmts()[$key], $containsStmts->getStmts()[$key + 1])) { break; } - if (! $stmtsAware->stmts[$key] instanceof Expression) { + if (! $containsStmts->getStmts()[$key] instanceof Expression) { continue; } /** @var Expression $stmt */ - $stmt = $stmtsAware->stmts[$key]; + $stmt = $containsStmts->getStmts()[$key]; if ($this->shouldSkip($stmt)) { continue; } - $nextStmt = $stmtsAware->stmts[$key + 1]; + $nextStmt = $containsStmts->getStmts()[$key + 1]; /** @var FuncCall $resetOrEndFuncCall */ $resetOrEndFuncCall = $stmt->expr; @@ -143,7 +141,7 @@ private function processArrayKeyFirstLast( continue; } - if ($this->hasInternalPointerChangeNext($stmtsAware, $key + 1, $totalKeys, $keyFuncCall)) { + if ($this->hasInternalPointerChangeNext($containsStmts, $key + 1, $totalKeys, $keyFuncCall)) { continue; } @@ -154,36 +152,36 @@ private function processArrayKeyFirstLast( $newName = self::PREVIOUS_TO_NEW_FUNCTIONS[$this->getName($stmt->expr)]; $keyFuncCall->name = new Name($newName); - $this->changeNextKeyCall($stmtsAware, $key + 2, $resetOrEndFuncCall, $keyFuncCall->name); + $this->changeNextKeyCall($containsStmts, $key + 2, $resetOrEndFuncCall, $keyFuncCall->name); - unset($stmtsAware->stmts[$key]); + unset($containsStmts->getStmts()[$key]); - return $stmtsAware; + return $containsStmts; } return null; } private function changeNextKeyCall( - StmtsAwareInterface $stmtsAware, + ContainsStmts $containsStmts, int $key, FuncCall $resetOrEndFuncCall, Name $newName ): void { - $counter = count($stmtsAware->stmts); + $counter = count($containsStmts->getStmts()); for ($nextKey = $key; $nextKey < $counter; ++$nextKey) { - if (! isset($stmtsAware->stmts[$nextKey])) { + if (! isset($containsStmts->getStmts()[$nextKey])) { break; } - if ($stmtsAware->stmts[$nextKey] instanceof Expression && ! $this->shouldSkip( - $stmtsAware->stmts[$nextKey] + if ($containsStmts->getStmts()[$nextKey] instanceof Expression && ! $this->shouldSkip( + $containsStmts->getStmts()[$nextKey] )) { - $this->processArrayKeyFirstLast($stmtsAware, $nextKey); + $this->processArrayKeyFirstLast($containsStmts, $nextKey); break; } - $keyFuncCall = $this->resolveKeyFuncCall($stmtsAware->stmts[$nextKey], $resetOrEndFuncCall); + $keyFuncCall = $this->resolveKeyFuncCall($containsStmts->getStmts()[$nextKey], $resetOrEndFuncCall); if (! $keyFuncCall instanceof FuncCall) { continue; } @@ -219,18 +217,18 @@ private function resolveKeyFuncCall(Stmt $nextStmt, FuncCall $resetOrEndFuncCall } private function hasInternalPointerChangeNext( - StmtsAwareInterface $stmtsAware, + ContainsStmts $containsStmts, int $nextKey, int $totalKeys, FuncCall $funcCall ): bool { for ($key = $nextKey; $key <= $totalKeys; ++$key) { - if (! isset($stmtsAware->stmts[$key])) { + if (! isset($containsStmts->getStmts()[$key])) { continue; } $hasPrevCallNext = (bool) $this->betterNodeFinder->findFirst( - $stmtsAware->stmts[$key], + $containsStmts->getStmts()[$key], function (Node $subNode) use ($funcCall): bool { if (! $subNode instanceof FuncCall) { return false; diff --git a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php index 8d2c18b7cab..88a9ce903d1 100644 --- a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php +++ b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php @@ -6,12 +6,12 @@ use PhpParser\Node; use PhpParser\Node\Arg; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Scalar\Int_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; @@ -58,11 +58,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php b/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php index d6b4ea2c86c..4f955b1fd18 100644 --- a/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php +++ b/rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php @@ -35,11 +35,11 @@ public function __construct( public function matchArrowFunctionExpr(Closure $closure): ?Expr { - if (count($closure->stmts) !== 1) { + if (count($closure->getStmts()) !== 1) { return null; } - $onlyStmt = $closure->stmts[0]; + $onlyStmt = $closure->getStmts()[0]; if (! $onlyStmt instanceof Return_) { return null; } diff --git a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php index 3e396541461..8b78c185939 100644 --- a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php +++ b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php @@ -86,9 +86,10 @@ public function refactor(Node $node): ?Node $arrowFunction->static = true; } - $comments = $node->stmts[0]->getAttribute(AttributeKey::COMMENTS) ?? []; + $comments = $node->getStmts()[0] + ->getAttribute(AttributeKey::COMMENTS) ?? []; if ($comments !== []) { - $this->mirrorComments($arrowFunction->expr, $node->stmts[0]); + $this->mirrorComments($arrowFunction->expr, $node->getStmts()[0]); $arrowFunction->setAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED, true); } diff --git a/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php b/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php index 3f84b83fd5c..23ee87a7b8f 100644 --- a/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php +++ b/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php @@ -164,7 +164,7 @@ private function isNextStmtReturnWithExpr(Switch_ $switch, ?Stmt $nextStmt): boo foreach ($switch->cases as $case) { /** @var Expression[] $expressions */ - $expressions = array_filter($case->stmts, static fn (Node $node): bool => $node instanceof Expression); + $expressions = array_filter($case->getStmts(), static fn (Node $node): bool => $node instanceof Expression); foreach ($expressions as $expression) { if (! $expression->expr instanceof Assign) { continue; diff --git a/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php b/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php index bad8c0b0dd4..03669cb4c0f 100644 --- a/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php +++ b/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php @@ -92,7 +92,7 @@ private function matchPropertyPromotionCandidate( $firstParamAsVariable = $this->resolveFirstParamUses($constructClassMethod); // match property name to assign in constructor - foreach ((array) $constructClassMethod->stmts as $stmt) { + foreach ($constructClassMethod->getStmts() as $stmt) { if (! $stmt instanceof Expression) { continue; } @@ -142,7 +142,7 @@ private function resolveFirstParamUses(ClassMethod $classMethod): array foreach ($classMethod->params as $param) { $paramName = $this->nodeNameResolver->getName($param); - $firstParamVariable = $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use ( + $firstParamVariable = $this->betterNodeFinder->findFirst($classMethod->getStmts(), function (Node $node) use ( $paramName ): bool { if (! $node instanceof Variable) { diff --git a/rules/Php80/NodeAnalyzer/SwitchAnalyzer.php b/rules/Php80/NodeAnalyzer/SwitchAnalyzer.php index db7ed01771d..57bbec1f06c 100644 --- a/rules/Php80/NodeAnalyzer/SwitchAnalyzer.php +++ b/rules/Php80/NodeAnalyzer/SwitchAnalyzer.php @@ -80,7 +80,10 @@ public function hasEachCaseSingleStmt(Switch_ $switch): bool continue; } - $stmtsWithoutBreak = array_filter($case->stmts, static fn (Node $node): bool => ! $node instanceof Break_); + $stmtsWithoutBreak = array_filter( + $case->getStmts(), + static fn (Node $node): bool => ! $node instanceof Break_ + ); if (count($stmtsWithoutBreak) !== 1) { return false; @@ -95,7 +98,7 @@ public function hasDefaultSingleStmt(Switch_ $switch): bool foreach ($switch->cases as $case) { if (! $case->cond instanceof Expr) { $stmtsWithoutBreak = array_filter( - $case->stmts, + $case->getStmts(), static fn (Node $node): bool => ! $node instanceof Break_ ); return count($stmtsWithoutBreak) === 1; @@ -107,11 +110,11 @@ public function hasDefaultSingleStmt(Switch_ $switch): bool private function hasBreakOrReturnOrEmpty(Case_ $case): bool { - if ($case->stmts === []) { + if ($case->getStmts() === []) { return true; } - foreach ($case->stmts as $caseStmt) { + foreach ($case->getStmts() as $caseStmt) { if ($caseStmt instanceof Break_) { return true; } @@ -126,7 +129,7 @@ private function hasBreakOrReturnOrEmpty(Case_ $case): bool private function containsCaseReturn(Case_ $case): bool { - foreach ($case->stmts as $stmt) { + foreach ($case->getStmts() as $stmt) { if ($stmt instanceof Return_) { return true; } diff --git a/rules/Php80/NodeResolver/SwitchExprsResolver.php b/rules/Php80/NodeResolver/SwitchExprsResolver.php index af6178fb640..1de92cb9dcd 100644 --- a/rules/Php80/NodeResolver/SwitchExprsResolver.php +++ b/rules/Php80/NodeResolver/SwitchExprsResolver.php @@ -34,7 +34,7 @@ public function resolve(Switch_ $switch): array $this->moveDefaultCaseToLast($newSwitch); foreach ($newSwitch->cases as $key => $case) { - if ($case->stmts !== []) { + if ($case->getStmts() !== []) { continue; } @@ -46,11 +46,11 @@ public function resolve(Switch_ $switch): array } foreach ($newSwitch->cases as $key => $case) { - if ($case->stmts === []) { + if ($case->getStmts() === []) { continue; } - $expr = $case->stmts[0]; + $expr = $case->getStmts()[0]; $comments = $expr->getComments(); if ($expr instanceof Expression) { $expr = $expr->expr; @@ -108,12 +108,12 @@ private function moveDefaultCaseToLast(Switch_ $switch): void } // current default has no stmt? keep as is as rely to next case - if ($case->stmts === []) { + if ($case->getStmts() === []) { return; } for ($loop = $key - 1; $loop >= 0; --$loop) { - if ($switch->cases[$loop]->stmts !== []) { + if ($switch->cases[$loop]->getStmts() !== []) { break; } @@ -131,26 +131,26 @@ private function moveDefaultCaseToLast(Switch_ $switch): void private function isValidCase(Case_ $case): bool { // prepend to previous one - if ($case->stmts === []) { + if ($case->getStmts() === []) { return true; } - if (count($case->stmts) === 2 && $case->stmts[1] instanceof Break_) { + if (count($case->getStmts()) === 2 && $case->getStmts()[1] instanceof Break_) { return true; } // default throws stmts - if (count($case->stmts) !== 1) { + if (count($case->getStmts()) !== 1) { return false; } // throws expression - if ($case->stmts[0] instanceof Throw_) { + if ($case->getStmts()[0] instanceof Throw_) { return true; } // instant return - if ($case->stmts[0] instanceof Return_) { + if ($case->getStmts()[0] instanceof Return_) { return true; } diff --git a/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php b/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php index 537e9ab1d9b..39817800ff1 100644 --- a/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php +++ b/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php @@ -5,10 +5,10 @@ namespace Rector\Php80\Rector\Catch_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Finally_; use PhpParser\Node\Stmt\TryCatch; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; use Rector\NodeManipulator\StmtsManipulator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -66,15 +66,15 @@ public function run() */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index ed37f50d75d..cfbb19277f2 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -224,7 +224,7 @@ public function refactor(Node $node): ?Node // remove assign in constructor $assignStmtPosition = $promotionCandidate->getStmtPosition(); - unset($constructClassMethod->stmts[$assignStmtPosition]); + unset($constructClassMethod->getStmts()[$assignStmtPosition]); $oldParamName = $this->getName($param); $this->variableRenamer->renameVariableInFunctionLike($constructClassMethod, $oldParamName, $propertyName); @@ -265,7 +265,7 @@ public function refactor(Node $node): ?Node ); // update variable to property fetch references - $this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($constructClassMethod->getStmts(), function (Node $node) use ( $promotionCandidate, $propertyName ): null|int|PropertyFetch { diff --git a/rules/Php80/Rector/Class_/StringableForToStringRector.php b/rules/Php80/Rector/Class_/StringableForToStringRector.php index 0835b228806..daf01e58724 100644 --- a/rules/Php80/Rector/Class_/StringableForToStringRector.php +++ b/rules/Php80/Rector/Class_/StringableForToStringRector.php @@ -151,7 +151,7 @@ private function processNotStringType(ClassMethod $toStringClassMethod): void return; } - $this->traverseNodesWithCallable((array) $toStringClassMethod->stmts, function (Node $subNode): ?int { + $this->traverseNodesWithCallable($toStringClassMethod->getStmts(), function (Node $subNode): ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } diff --git a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php index 271047249be..311f8721e35 100644 --- a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php +++ b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php @@ -5,6 +5,7 @@ namespace Rector\Php80\Rector\Switch_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Cast; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Switch_; use PhpParser\NodeVisitor; use PHPStan\Type\ObjectType; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer; use Rector\Php80\NodeFactory\MatchFactory; @@ -78,11 +78,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node * @return null|Node|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN */ public function refactor(Node $node): null|Node|int diff --git a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php index a482b57038b..bbc0c4f62b5 100644 --- a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php +++ b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php @@ -81,7 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null || $node->stmts === []) { + if ($node->getStmts() === [] || $node->stmts === []) { return null; } @@ -100,7 +100,7 @@ public function refactor(Node $node): ?Node } $hasChanged = false; - foreach ((array) $constructClassMethod->stmts as $key => $stmt) { + foreach ($constructClassMethod->getStmts() as $key => $stmt) { foreach ($params as $param) { $paramName = $this->getName($param); @@ -122,7 +122,7 @@ public function refactor(Node $node): ?Node $param->type = $currentParamType->type; $param->default = $coalesce->right; - unset($constructClassMethod->stmts[$key]); + unset($constructClassMethod->getStmts()[$key]); $this->processPropertyPromotion($node, $param, $paramName); $hasChanged = true; @@ -194,7 +194,7 @@ private function processPropertyPromotion(Class_ $class, Param $param, string $p private function matchConstructorParams(ClassMethod $classMethod): array { // skip empty constructor assigns, as we need those here - if ($classMethod->stmts === null || $classMethod->stmts === []) { + if ($classMethod->getStmts() === [] || $classMethod->getStmts() === []) { return []; } diff --git a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php index 6855933ca0c..e827d53af32 100644 --- a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php +++ b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php @@ -267,13 +267,13 @@ private function shouldSkipParentClassMethod(ClassReflection $parentClassReflect } // has any stmts? - if ($parentClassMethod->stmts === null || $parentClassMethod->stmts === []) { + if ($parentClassMethod->getStmts() === [] || $parentClassMethod->getStmts() === []) { return true; } - if (count($parentClassMethod->stmts) === 1) { + if (count($parentClassMethod->getStmts()) === 1) { /** @var Stmt $soleStmt */ - $soleStmt = $parentClassMethod->stmts[0]; + $soleStmt = $parentClassMethod->getStmts()[0]; // most likely, return null; is interface to be designed to override if ($soleStmt instanceof Return_ && $soleStmt->expr instanceof Expr && $this->valueResolver->isNull( $soleStmt->expr diff --git a/rules/Php84/NodeFactory/PropertyHookFactory.php b/rules/Php84/NodeFactory/PropertyHookFactory.php index 634574d6aaf..f6165a73805 100644 --- a/rules/Php84/NodeFactory/PropertyHookFactory.php +++ b/rules/Php84/NodeFactory/PropertyHookFactory.php @@ -24,9 +24,9 @@ public function create(ClassMethod $classMethod, string $propertyName): ?Propert return null; } - Assert::notNull($classMethod->stmts); + Assert::notNull($classMethod->getStmts()); - $soleStmt = $classMethod->stmts[0]; + $soleStmt = $classMethod->getStmts()[0]; // use sole Expr if (($soleStmt instanceof Expression || $soleStmt instanceof Return_) && $methodName !== 'set') { diff --git a/rules/Php84/Rector/Class_/PropertyHookRector.php b/rules/Php84/Rector/Class_/PropertyHookRector.php index 5cdd9f103eb..f40e2293433 100644 --- a/rules/Php84/Rector/Class_/PropertyHookRector.php +++ b/rules/Php84/Rector/Class_/PropertyHookRector.php @@ -111,7 +111,7 @@ public function refactor(Node $node): ?Node ); foreach ($candidateClassMethods as $candidateClassMethod) { - if (count((array) $candidateClassMethod->stmts) !== 1) { + if (count((array) $candidateClassMethod->getStmts()) !== 1) { continue; } diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php index a3a977e2dad..6f902b135b9 100644 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php @@ -5,6 +5,7 @@ namespace Rector\Php84\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; @@ -16,7 +17,6 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\StmtsManipulator; use Rector\Php84\NodeAnalyzer\ForeachKeyUsedInConditionalAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -81,15 +81,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -102,14 +102,14 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::ARRAY_ALL; } - private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node + private function refactorBooleanAssignmentPattern(ContainsStmts $containsStmts): ?Node { - foreach ($stmtsAware->stmts as $key => $stmt) { + foreach ($containsStmts->stmts as $key => $stmt) { if (! $stmt instanceof Foreach_) { continue; } - $prevStmt = $stmtsAware->stmts[$key - 1] ?? null; + $prevStmt = $containsStmts->stmts[$key - 1] ?? null; if (! $prevStmt instanceof Expression) { continue; } @@ -136,7 +136,7 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar } if ($this->stmtsManipulator->isVariableUsedInNextStmt( - $stmtsAware, + $containsStmts, $key + 1, (string) $this->getName($foreach->valueVar) )) { @@ -174,26 +174,26 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar $newAssign = new Assign($assignedVariable, $funcCall); $newExpression = new Expression($newAssign); - unset($stmtsAware->stmts[$key - 1]); - $stmtsAware->stmts[$key] = $newExpression; + unset($containsStmts->stmts[$key - 1]); + $containsStmts->stmts[$key] = $newExpression; - $stmtsAware->stmts = array_values($stmtsAware->stmts); + $containsStmts->stmts = array_values($containsStmts->stmts); - return $stmtsAware; + return $containsStmts; } return null; } - private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node + private function refactorEarlyReturnPattern(ContainsStmts $containsStmts): ?Node { - foreach ($stmtsAware->stmts as $key => $stmt) { + foreach ($containsStmts->stmts as $key => $stmt) { if (! $stmt instanceof Foreach_) { continue; } $foreach = $stmt; - $nextStmt = $stmtsAware->stmts[$key + 1] ?? null; + $nextStmt = $containsStmts->stmts[$key + 1] ?? null; if (! $nextStmt instanceof Return_) { continue; @@ -236,11 +236,11 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?N $funcCall = $this->nodeFactory->createFuncCall('array_all', [$foreach->expr, $arrowFunction]); - $stmtsAware->stmts[$key] = new Return_($funcCall); - unset($stmtsAware->stmts[$key + 1]); - $stmtsAware->stmts = array_values($stmtsAware->stmts); + $containsStmts->stmts[$key] = new Return_($funcCall); + unset($containsStmts->stmts[$key + 1]); + $containsStmts->stmts = array_values($containsStmts->stmts); - return $stmtsAware; + return $containsStmts; } return null; @@ -248,25 +248,25 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?N private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return false; } - if (! $foreach->stmts[0] instanceof If_) { + if (! $foreach->getStmts()[0] instanceof If_) { return false; } - $ifStmt = $foreach->stmts[0]; + $ifStmt = $foreach->getStmts()[0]; - if (count($ifStmt->stmts) !== 1) { + if (count($ifStmt->getStmts()) !== 1) { return false; } - if (! $ifStmt->stmts[0] instanceof Return_) { + if (! $ifStmt->getStmts()[0] instanceof Return_) { return false; } - $returnStmt = $ifStmt->stmts[0]; + $returnStmt = $ifStmt->getStmts()[0]; if (! $returnStmt->expr instanceof Expr) { return false; @@ -284,20 +284,20 @@ private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool private function isValidBooleanAssignmentForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return false; } - $firstStmt = $foreach->stmts[0]; + $firstStmt = $foreach->getStmts()[0]; if ( ! $firstStmt instanceof If_ || - count($firstStmt->stmts) !== 2 + count($firstStmt->getStmts()) !== 2 ) { return false; } - $assignmentStmt = $firstStmt->stmts[0]; - $breakStmt = $firstStmt->stmts[1]; + $assignmentStmt = $firstStmt->getStmts()[0]; + $breakStmt = $firstStmt->getStmts()[1]; if ( ! $assignmentStmt instanceof Expression || diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php index 9ddea5d90c2..a1ff2e1e745 100644 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php @@ -5,6 +5,7 @@ namespace Rector\Php84\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; @@ -15,7 +16,6 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\StmtsManipulator; use Rector\Php84\NodeAnalyzer\ForeachKeyUsedInConditionalAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -80,15 +80,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -101,14 +101,14 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::ARRAY_ANY; } - private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node + private function refactorBooleanAssignmentPattern(ContainsStmts $containsStmts): ?Node { - foreach ($stmtsAware->stmts as $key => $stmt) { + foreach ($containsStmts->stmts as $key => $stmt) { if (! $stmt instanceof Foreach_) { continue; } - $prevStmt = $stmtsAware->stmts[$key - 1] ?? null; + $prevStmt = $containsStmts->stmts[$key - 1] ?? null; if (! $prevStmt instanceof Expression) { continue; } @@ -135,7 +135,7 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar } if ($this->stmtsManipulator->isVariableUsedInNextStmt( - $stmtsAware, + $containsStmts, $key + 1, (string) $this->getName($foreach->valueVar) )) { @@ -171,26 +171,26 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar $newAssign = new Assign($assignedVariable, $funcCall); $newExpression = new Expression($newAssign); - unset($stmtsAware->stmts[$key - 1]); - $stmtsAware->stmts[$key] = $newExpression; + unset($containsStmts->stmts[$key - 1]); + $containsStmts->stmts[$key] = $newExpression; - $stmtsAware->stmts = array_values($stmtsAware->stmts); + $containsStmts->stmts = array_values($containsStmts->stmts); - return $stmtsAware; + return $containsStmts; } return null; } - private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node + private function refactorEarlyReturnPattern(ContainsStmts $containsStmts): ?Node { - foreach ($stmtsAware->stmts as $key => $stmt) { + foreach ($containsStmts->stmts as $key => $stmt) { if (! $stmt instanceof Foreach_) { continue; } $foreach = $stmt; - $nextStmt = $stmtsAware->stmts[$key + 1] ?? null; + $nextStmt = $containsStmts->stmts[$key + 1] ?? null; if (! $nextStmt instanceof Return_) { continue; @@ -232,11 +232,11 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?N $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); - $stmtsAware->stmts[$key] = new Return_($funcCall); - unset($stmtsAware->stmts[$key + 1]); - $stmtsAware->stmts = array_values($stmtsAware->stmts); + $containsStmts->stmts[$key] = new Return_($funcCall); + unset($containsStmts->stmts[$key + 1]); + $containsStmts->stmts = array_values($containsStmts->stmts); - return $stmtsAware; + return $containsStmts; } return null; @@ -244,20 +244,20 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?N private function isValidBooleanAssignmentForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return false; } - $firstStmt = $foreach->stmts[0]; + $firstStmt = $foreach->getStmts()[0]; if ( ! $firstStmt instanceof If_ || - count($firstStmt->stmts) !== 2 + count($firstStmt->getStmts()) !== 2 ) { return false; } - $assignmentStmt = $firstStmt->stmts[0]; - $breakStmt = $firstStmt->stmts[1]; + $assignmentStmt = $firstStmt->getStmts()[0]; + $breakStmt = $firstStmt->getStmts()[1]; if ( ! $assignmentStmt instanceof Expression || @@ -284,25 +284,25 @@ private function isValidBooleanAssignmentForeachStructure(Foreach_ $foreach, Var private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return false; } - if (! $foreach->stmts[0] instanceof If_) { + if (! $foreach->getStmts()[0] instanceof If_) { return false; } - $ifStmt = $foreach->stmts[0]; + $ifStmt = $foreach->getStmts()[0]; - if (count($ifStmt->stmts) !== 1) { + if (count($ifStmt->getStmts()) !== 1) { return false; } - if (! $ifStmt->stmts[0] instanceof Return_) { + if (! $ifStmt->getStmts()[0] instanceof Return_) { return false; } - $returnStmt = $ifStmt->stmts[0]; + $returnStmt = $ifStmt->getStmts()[0]; if (! $returnStmt->expr instanceof Expr) { return false; diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php index 61f74fd1856..01980ad52ad 100644 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php @@ -5,6 +5,7 @@ namespace Rector\Php84\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; @@ -14,7 +15,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\StmtsManipulator; use Rector\Php84\NodeAnalyzer\ForeachKeyUsedInConditionalAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -69,15 +69,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -169,21 +169,21 @@ private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVa { if ( ! $foreach->keyVar instanceof Expr || - count($foreach->stmts) !== 1 + count($foreach->getStmts()) !== 1 ) { return false; } - $firstStmt = $foreach->stmts[0]; + $firstStmt = $foreach->getStmts()[0]; if ( ! $firstStmt instanceof If_ || - count($firstStmt->stmts) !== 2 + count($firstStmt->getStmts()) !== 2 ) { return false; } - $assignmentStmt = $firstStmt->stmts[0]; - $breakStmt = $firstStmt->stmts[1]; + $assignmentStmt = $firstStmt->getStmts()[0]; + $breakStmt = $firstStmt->getStmts()[1]; if ( ! $assignmentStmt instanceof Expression || diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php index 8ce48ae1252..57baabce67b 100644 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php @@ -5,6 +5,7 @@ namespace Rector\Php84\Rector\Foreach_; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; @@ -13,7 +14,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeManipulator\StmtsManipulator; use Rector\Php84\NodeAnalyzer\ForeachKeyUsedInConditionalAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -64,15 +64,15 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -162,20 +162,20 @@ public function provideMinPhpVersion(): int private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool { - if (count($foreach->stmts) !== 1) { + if (count($foreach->getStmts()) !== 1) { return false; } - $firstStmt = $foreach->stmts[0]; + $firstStmt = $foreach->getStmts()[0]; if ( ! $firstStmt instanceof If_ || - count($firstStmt->stmts) !== 2 + count($firstStmt->getStmts()) !== 2 ) { return false; } - $assignmentStmt = $firstStmt->stmts[0]; - $breakStmt = $firstStmt->stmts[1]; + $assignmentStmt = $firstStmt->getStmts()[0]; + $breakStmt = $firstStmt->getStmts()[1]; if ( ! $assignmentStmt instanceof Expression || diff --git a/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php b/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php index 7f42e4d59d8..34c590d7dba 100644 --- a/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php +++ b/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php @@ -76,7 +76,7 @@ public function refactor(Node $node): ?Node $hasChanged = \false; - $this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable((array) $node->getStmts(), function (Node $node) use ( &$hasChanged ): int|Return_|null { if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) { diff --git a/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php b/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php index 910f961640e..963fe5d781d 100644 --- a/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php +++ b/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php @@ -65,12 +65,13 @@ public function refactor(Node $node): ?Node continue; } - if (count($case->stmts) === 0) { + if (count($case->getStmts()) === 0) { $startCaseStmtsPos = isset($node->cases[$key + 1]) ? $node->cases[$key + 1]->getStartTokenPos() : $node->getEndTokenPos(); } else { - $startCaseStmtsPos = $case->stmts[0]->getStartTokenPos(); + $startCaseStmtsPos = $case->getStmts()[0] + ->getStartTokenPos(); } if ($startCaseStmtsPos < 0) { diff --git a/rules/Privatization/Guard/ParentPropertyLookupGuard.php b/rules/Privatization/Guard/ParentPropertyLookupGuard.php index d45a508d8ca..2b8ee08e531 100644 --- a/rules/Privatization/Guard/ParentPropertyLookupGuard.php +++ b/rules/Privatization/Guard/ParentPropertyLookupGuard.php @@ -77,7 +77,7 @@ private function isFoundInParentClassMethods( $methods = $classLike->getMethods(); foreach ($methods as $method) { - $isFound = $this->isFoundInMethodStmts((array) $method->stmts, $propertyName, $className); + $isFound = $this->isFoundInMethodStmts((array) $method->getStmts(), $propertyName, $className); if ($isFound) { return true; } diff --git a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php index 9ebbac2df15..6dd771654b0 100644 --- a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php +++ b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php @@ -143,7 +143,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool // if has parent call, its probably overriding parent one → skip it $hasParentCall = (bool) $this->betterNodeFinder->findFirst( - (array) $classMethod->stmts, + $classMethod->getStmts(), function (Node $node): bool { if (! $node instanceof StaticCall) { return false; diff --git a/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php b/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php index 65ac151438a..74d7e17e3fc 100644 --- a/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php +++ b/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php @@ -138,7 +138,7 @@ private function matchLocalPropertyFetchInGetterMethod(ClassMethod $classMethod) return null; } - $stmts = (array) $classMethod->stmts; + $stmts = $classMethod->getStmts(); if (count($stmts) !== 1) { return null; } diff --git a/rules/Transform/Rector/ClassMethod/WrapReturnRector.php b/rules/Transform/Rector/ClassMethod/WrapReturnRector.php index 26e5ba610ac..dbe0acb5180 100644 --- a/rules/Transform/Rector/ClassMethod/WrapReturnRector.php +++ b/rules/Transform/Rector/ClassMethod/WrapReturnRector.php @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node continue; } - if ($node->stmts === null) { + if ($node->getStmts() === []) { continue; } @@ -110,12 +110,12 @@ public function configure(array $configuration): void private function wrap(ClassMethod $classMethod): bool { - if (! is_iterable($classMethod->stmts)) { + if (! is_iterable($classMethod->getStmts())) { return false; } $hasChanged = false; - foreach ($classMethod->stmts as $stmt) { + foreach ($classMethod->getStmts() as $stmt) { if ($stmt instanceof Return_ && $stmt->expr instanceof Expr && ! $stmt->expr instanceof Array_) { $stmt->expr = new Array_([new ArrayItem($stmt->expr)]); diff --git a/rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.php b/rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.php index 694a7da036e..b535882eda3 100644 --- a/rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.php +++ b/rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\Smaller; @@ -17,7 +18,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PhpParser\NodeVisitor; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Transform\ValueObject\WrapFuncCallWithPhpVersionIdChecker; @@ -63,16 +63,16 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node * @return null|Node|NodeVisitor::DONT_TRAVERSE_CHILDREN */ public function refactor(Node $node): null|Node|int { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -126,22 +126,22 @@ public function configure(array $configuration): void $this->wrapFuncCallWithPhpVersionIdCheckers = $configuration; } - private function isWrappedFuncCall(StmtsAwareInterface $stmtsAware): bool + private function isWrappedFuncCall(ContainsStmts $containsStmts): bool { - if (! $stmtsAware instanceof If_) { + if (! $containsStmts instanceof If_) { return false; } - $phpVersionId = $this->getPhpVersionId($stmtsAware->cond); + $phpVersionId = $this->getPhpVersionId($containsStmts->cond); if (! $phpVersionId instanceof Int_) { return false; } - if (count($stmtsAware->stmts) !== 1) { + if (count($containsStmts->getStmts()) !== 1) { return false; } - $childStmt = $stmtsAware->stmts[0]; + $childStmt = $containsStmts->getStmts()[0]; if (! $childStmt instanceof Expression || ! $childStmt->expr instanceof FuncCall) { return false; diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index 2061e95a62b..2d09264d21f 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -58,7 +58,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b StatementDepthAttributeDecorator::decorateClassMethods($initializeClassMethods); foreach ($initializeClassMethods as $initializeClassMethod) { - $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $initializeClassMethod->stmts, function ( + $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $initializeClassMethod->getStmts(), function ( Node $node ) use ($propertyName, &$isAssignedInConstructor, $allowConditional): ?int { if ($this->isIfElseAssign($node, $propertyName)) { @@ -132,8 +132,8 @@ private function isIfElseAssign(Node $node, string $propertyName): bool return false; } - return $this->isAssignedInStmts($node->stmts, $propertyName) && $this->isAssignedInStmts( - $node->else->stmts, + return $this->isAssignedInStmts($node->getStmts(), $propertyName) && $this->isAssignedInStmts( + $node->else->getStmts(), $propertyName ); } diff --git a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php index 06634f6a12e..167177adda7 100644 --- a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php +++ b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php @@ -32,7 +32,7 @@ public function isLegal(Param $param, ClassMethod $classMethod): bool $isLegal = true; $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - (array) $classMethod->stmts, + $classMethod->getStmts(), function (Node $subNode) use (&$isLegal, $paramName): ?int { if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->nodeNameResolver->isName( $subNode->var, diff --git a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php index f9676efad1b..c444ba88179 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php @@ -20,7 +20,7 @@ public function __construct( public function hasPropertyFetchReturn(ClassMethod $classMethod, string $propertyName): bool { - $stmts = (array) $classMethod->stmts; + $stmts = $classMethod->getStmts(); if (count($stmts) !== 1) { return false; } @@ -42,7 +42,7 @@ public function hasPropertyFetchReturn(ClassMethod $classMethod, string $propert public function hasOnlyPropertyAssign(ClassMethod $classMethod, string $propertyName): bool { - $stmts = (array) $classMethod->stmts; + $stmts = $classMethod->getStmts(); if (count($stmts) !== 1) { return false; } diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index 0ca20bab991..7e409fd0852 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php @@ -21,7 +21,7 @@ public function __construct( public function hasNeverFuncCall(ClassMethod | Closure | Function_ $functionLike): bool { - foreach ((array) $functionLike->stmts as $stmt) { + foreach ($functionLike->getStmts() as $stmt) { if ($this->isWithNeverTypeExpr($stmt)) { return true; } diff --git a/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php index 246af63b648..57d985b7e8d 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php @@ -22,7 +22,7 @@ public function __construct( */ public function hasOnlyReturnWithExpr(ClassMethod|Function_ $functionLike, array $returns): bool { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return false; } diff --git a/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php index 9494de33cec..a046a03811c 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php @@ -25,7 +25,7 @@ public function __construct( */ public function matchAlwaysReturnNativeCallLikes(ClassMethod|Function_ $functionLike): ?array { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php index 1d8166cbf07..edae9d849e2 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php @@ -32,7 +32,7 @@ public function __construct( public function matchAlwaysReturnVariableNew(ClassMethod|Function_ $functionLike): ?string { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return null; } @@ -93,7 +93,7 @@ private function resolveCreatedVariablesToTypes(ClassMethod|Function_|Closure $f $createdVariablesToTypes = []; // what new is assigned to it? - foreach ((array) $functionLike->stmts as $stmt) { + foreach ($functionLike->getStmts() as $stmt) { $assignToVariable = $this->matchAssignToVariable($stmt); if (! $assignToVariable instanceof AssignToVariable) { continue; diff --git a/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php b/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php index 91f2f697a38..a770f3c3cdd 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php +++ b/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php @@ -35,11 +35,7 @@ public function matchMask( string $variableName, string $mask ): bool { - if ($functionLike instanceof ArrowFunction) { - $stmts = [$functionLike->expr]; - } else { - $stmts = (array) $functionLike->stmts; - } + $stmts = $functionLike instanceof ArrowFunction ? [$functionLike->expr] : $functionLike->getStmts(); $funcCalls = $this->betterNodeFinder->findInstancesOfScoped($stmts, FuncCall::class); $funcCalls = array_values( diff --git a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php index bce482f80cd..195f5341823 100644 --- a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php +++ b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php @@ -38,7 +38,7 @@ public function __construct( public function add(ClassMethod|Function_ $functionLike, Scope $scope): ClassMethod|Function_|null { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return null; } @@ -46,13 +46,13 @@ public function add(ClassMethod|Function_ $functionLike, Scope $scope): ClassMet return null; } - $return = $this->findCurrentScopeReturn($functionLike->stmts); + $return = $this->findCurrentScopeReturn($functionLike->getStmts()); if (! $return instanceof Return_ || ! $return->expr instanceof Expr) { return null; } $returnName = $this->nodeNameResolver->getName($return->expr); - $stmts = $functionLike->stmts; + $stmts = $functionLike->getStmts(); foreach ($functionLike->getParams() as $param) { if (! $param->type instanceof Node) { diff --git a/rules/TypeDeclaration/NodeManipulator/AddUnionReturnType.php b/rules/TypeDeclaration/NodeManipulator/AddUnionReturnType.php index 1a0014e94d3..15de5301b36 100644 --- a/rules/TypeDeclaration/NodeManipulator/AddUnionReturnType.php +++ b/rules/TypeDeclaration/NodeManipulator/AddUnionReturnType.php @@ -32,7 +32,7 @@ public function __construct( */ public function add(ClassMethod|Function_ $node, Scope $scope): ClassMethod|Function_|null { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php index 1c3dafbb847..f79ec43531c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php @@ -86,7 +86,7 @@ public function refactor(Node $node): null|ClassMethod|Function_ return null; } - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -99,7 +99,7 @@ public function refactor(Node $node): null|ClassMethod|Function_ $paramsWithType = []; $this->traverseNodesWithCallable( - $node->stmts, + $node->getStmts(), function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithType): null|int { if ($subNode instanceof Class_ || $subNode instanceof Function_) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php index d51dec28693..8246debd307 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php @@ -71,7 +71,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ClassMethod|Function_|Closure|ArrowFunction|null { - if ($node instanceof ClassMethod && $node->stmts === null) { + if ($node instanceof ClassMethod && $node->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php index f9d6e1ec866..dede4069dff 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php @@ -106,7 +106,7 @@ public function refactor(Node $node): ?Node return null; } - foreach ((array) $node->stmts as $classMethodStmt) { + foreach ((array) $node->getStmts() as $classMethodStmt) { if (! $classMethodStmt instanceof TryCatch) { continue; } @@ -159,7 +159,7 @@ public function refactor(Node $node): ?Node private function matchReturnType(TryCatch|Catch_|Finally_ $tryOrCatchOrFinally): ?Type { - foreach ($tryOrCatchOrFinally->stmts as $stmt) { + foreach ($tryOrCatchOrFinally->getStmts() as $stmt) { if (! $stmt instanceof Return_) { continue; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php index 88fc60ba5fb..df871b0eed6 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php @@ -128,11 +128,11 @@ private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool return false; } - if (count((array) $classMethod->stmts) !== 1) { + if (count($classMethod->getStmts()) !== 1) { return false; } - $onlyStmt = $classMethod->stmts[0] ?? null; + $onlyStmt = $classMethod->getStmts()[0] ?? null; return $onlyStmt instanceof Expression && $onlyStmt->expr instanceof Throw_; } @@ -142,6 +142,6 @@ private function isNotFinalAndEmpty(ClassMethod $classMethod): bool return false; } - return $classMethod->stmts === []; + return $classMethod->getStmts() === []; } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php index 89da603b8d0..45e566adcdc 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -136,7 +136,7 @@ private function shouldSkip(ClassMethod|Function_ $functionLike, Scope $scope): } // empty, nothing to find - if ($functionLike->stmts === null || $functionLike->stmts === []) { + if ($functionLike->getStmts() === [] || $functionLike->getStmts() === []) { return true; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php index af0bc81aecc..5e35a334d96 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -129,7 +129,7 @@ private function shouldSkip(ClassMethod|Function_ $functionLike, Scope $scope): } // empty, nothing to find - if ($functionLike->stmts === null || $functionLike->stmts === []) { + if ($functionLike->getStmts() === [] || $functionLike->getStmts() === []) { return true; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php index 580032d088b..69a8577ab6f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node { $scope = ScopeFetcher::fetch($node); // empty body, nothing to resolve - if ($node->stmts === null || $node->stmts === []) { + if ($node->getStmts() === [] || $node->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php index bfd15cd8eab..fbd765a9cd8 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php @@ -105,7 +105,7 @@ public function provideMinPhpVersion(): int private function hasReturnArray(ClassMethod|Function_ $functionLike): bool { - $stmts = $functionLike->stmts; + $stmts = $functionLike->getStmts(); if (! is_array($stmts)) { return false; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index d0372b24e59..f145841e2f0 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -102,7 +102,7 @@ public function refactor(Node $node): ?Node } // 1. is variable instantiated with array - $stmts = $node->stmts; + $stmts = $node->getStmts(); if ($stmts === null) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php index 5d0c483d83f..90565f5242f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php @@ -112,7 +112,7 @@ public function refactor(Node $node): ?Node return null; } - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php index ac9505488ad..a31bc2631f7 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php @@ -81,7 +81,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } @@ -94,16 +94,17 @@ public function refactor(Node $node): ?Node return null; } - if (count($node->stmts) !== 1) { + if (count($node->getStmts()) !== 1) { return null; } - if (! $node->stmts[0] instanceof Return_ || ! $node->stmts[0]->expr instanceof MethodCall) { + if (! $node->getStmts()[0] instanceof Return_ || ! $node->getStmts()[0]->expr instanceof MethodCall) { return null; } /** @var MethodCall $returnExpr */ - $returnExpr = $node->stmts[0]->expr; + $returnExpr = $node->getStmts()[0] + ->expr; if (! $this->isName($returnExpr->name, 'deserialize')) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index 9f48e7fdffa..e9cbc6ebf4c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -124,14 +124,14 @@ public function refactor(Node $node): ?Node private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function_|Closure $functionLike): bool { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return false; } $paramName = $this->getName($param); $isParamAccessedArrayDimFetch = false; - $this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($functionLike->getStmts(), function (Node $node) use ( $paramName, &$isParamAccessedArrayDimFetch, ): int|null { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php index d83c6be72ef..2282780a669 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php @@ -130,7 +130,7 @@ private function resolveVariableConcattedFromParam( Param $param, ClassMethod|Function_|Closure $functionLike ): ?Variable { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return null; } @@ -141,7 +141,7 @@ private function resolveVariableConcattedFromParam( $paramName = $this->getName($param); $variableConcatted = null; - $this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use ( + $this->traverseNodesWithCallable($functionLike->getStmts(), function (Node $node) use ( $paramName, &$variableConcatted, ): int|null { diff --git a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php index 3ec0cb6d1ad..11ba32871ad 100644 --- a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php @@ -179,7 +179,7 @@ private function resolveEntityClassnameFromPhpDoc(Class_ $class): ?string private function containsMethodCallNamed(ClassMethod $classMethod, string $desiredMethodName): bool { - return (bool) $this->nodeFinder->findFirst((array) $classMethod->stmts, static function (Node $node) use ( + return (bool) $this->nodeFinder->findFirst($classMethod->getStmts(), static function (Node $node) use ( $desiredMethodName ): bool { if (! $node instanceof MethodCall) { diff --git a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php index 86d6c9ddb50..186917bee2d 100644 --- a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php @@ -86,7 +86,7 @@ public function refactor(Node $node): ?Node return null; } - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 1fa3c45f010..ce6c15e267a 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -137,7 +137,7 @@ public function provideMinPhpVersion(): int */ private function resolveDimFetchVariableNames(Closure|ArrowFunction $closureExpr): array { - $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->stmts; + $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->getStmts(); /** @var ArrayDimFetch[] $arrayDimFetches */ $arrayDimFetches = $this->betterNodeFinder->findInstancesOfScoped($closureNodes, ArrayDimFetch::class); @@ -164,7 +164,7 @@ private function resolveDimFetchVariableNames(Closure|ArrowFunction $closureExpr */ private function resolveIsArrayVariables(Closure|ArrowFunction $closureExpr): array { - $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->stmts; + $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->getStmts(); /** @var FuncCall[] $funcCalls */ $funcCalls = $this->betterNodeFinder->findInstancesOfScoped($closureNodes, FuncCall::class); @@ -193,7 +193,7 @@ private function resolveIsArrayVariables(Closure|ArrowFunction $closureExpr): ar */ private function resolveInstanceofVariables(Closure|ArrowFunction $closureExpr): array { - $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->stmts; + $closureNodes = $closureExpr instanceof ArrowFunction ? [$closureExpr->expr] : $closureExpr->getStmts(); /** @var Instanceof_[] $instanceOfs */ $instanceOfs = $this->betterNodeFinder->findInstancesOfScoped($closureNodes, Instanceof_::class); diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index 51969277d0f..54fedc534e7 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\DeclareItem; use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\Int_; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Contract\Rector\HTMLAverseRectorInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Rector\AbstractRector; @@ -78,13 +78,13 @@ public function beforeTraverse(array $nodes): ?array $stmt = $rootStmt; if ($rootStmt instanceof FileWithoutNamespace) { - $currentStmt = current($rootStmt->stmts); + $currentStmt = current($rootStmt->getStmts()); if (! $currentStmt instanceof Stmt) { return null; } - $nodes = $rootStmt->stmts; + $nodes = $rootStmt->getStmts(); $stmt = $currentStmt; } @@ -114,11 +114,11 @@ public function beforeTraverse(array $nodes): ?array */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php index 23a79200535..25139f4d7cb 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\DeclareItem; use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\Int_; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Rector\AbstractRector; @@ -109,11 +109,11 @@ public function beforeTraverse(array $nodes): ?array */ public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node): ?Node { diff --git a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php index 717b947a5b0..780248ca4cf 100644 --- a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/TrustedClassMethodPropertyTypeInferer.php @@ -162,7 +162,7 @@ private function getResolveParamStaticTypeAsPHPStanType(ClassMethod $classMethod $paramStaticType = new ArrayType(new MixedType(), new MixedType()); $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - (array) $classMethod->stmts, + $classMethod->getStmts(), function (Node $node) use ($propertyName, &$paramStaticType): ?int { if (! $node instanceof Variable) { return null; diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index ca18286b132..1474a2cbc14 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -119,7 +119,7 @@ private function isFoundLoopControl(Do_|While_ $node): bool { $isFoundLoopControl = false; $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - $node->stmts, + $node->getStmts(), static function (Node $subNode) use (&$isFoundLoopControl) { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; @@ -145,7 +145,7 @@ private function isDoOrWhileWithAlwaysReturnOrExit(Stmt $stmt, bool $withNativeN return ! $this->isFoundLoopControl($stmt); } - if (! $this->hasStmtsAlwaysReturnOrExit($stmt->stmts, $withNativeNeverType)) { + if (! $this->hasStmtsAlwaysReturnOrExit($stmt->getStmts(), $withNativeNeverType)) { return false; } @@ -159,7 +159,7 @@ private function isIfReturn(Stmt|Expr $stmt, bool $withNativeNeverType): bool } foreach ($stmt->elseifs as $elseIf) { - if (! $this->hasStmtsAlwaysReturnOrExit($elseIf->stmts, $withNativeNeverType)) { + if (! $this->hasStmtsAlwaysReturnOrExit($elseIf->getStmts(), $withNativeNeverType)) { return false; } } @@ -168,11 +168,11 @@ private function isIfReturn(Stmt|Expr $stmt, bool $withNativeNeverType): bool return false; } - if (! $this->hasStmtsAlwaysReturnOrExit($stmt->stmts, $withNativeNeverType)) { + if (! $this->hasStmtsAlwaysReturnOrExit($stmt->getStmts(), $withNativeNeverType)) { return false; } - return $this->hasStmtsAlwaysReturnOrExit($stmt->else->stmts, $withNativeNeverType); + return $this->hasStmtsAlwaysReturnOrExit($stmt->else->getStmts(), $withNativeNeverType); } private function isStopped(Stmt $stmt): bool @@ -194,7 +194,7 @@ private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch, bool $withNativ foreach ($switch->cases as $case) { if (! $case->cond instanceof Expr) { - $hasDefault = $case->stmts !== []; + $hasDefault = $case->getStmts() !== []; break; } } @@ -205,7 +205,7 @@ private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch, bool $withNativ $casesWithReturnOrExitCount = $this->resolveReturnOrExitCount($switch, $withNativeNeverType); - $cases = array_filter($switch->cases, static fn (Case_ $case): bool => $case->stmts !== []); + $cases = array_filter($switch->cases, static fn (Case_ $case): bool => $case->getStmts() !== []); // has same amount of first return or exit nodes as switches return count($cases) === $casesWithReturnOrExitCount; @@ -214,16 +214,16 @@ private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch, bool $withNativ private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch, bool $withNativeNeverType): bool { $hasReturnOrExitInFinally = $tryCatch->finally instanceof Finally_ && $this->hasStmtsAlwaysReturnOrExit( - $tryCatch->finally->stmts, + $tryCatch->finally->getStmts(), $withNativeNeverType ); - if (! $this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts, $withNativeNeverType)) { + if (! $this->hasStmtsAlwaysReturnOrExit($tryCatch->getStmts(), $withNativeNeverType)) { return $hasReturnOrExitInFinally; } foreach ($tryCatch->catches as $catch) { - if ($this->hasStmtsAlwaysReturnOrExit($catch->stmts, $withNativeNeverType)) { + if ($this->hasStmtsAlwaysReturnOrExit($catch->getStmts(), $withNativeNeverType)) { continue; } @@ -242,7 +242,7 @@ private function resolveReturnOrExitCount(Switch_ $switch, bool $withNativeNever $casesWithReturnCount = 0; foreach ($switch->cases as $case) { - if ($this->hasStmtsAlwaysReturnOrExit($case->stmts, $withNativeNeverType)) { + if ($this->hasStmtsAlwaysReturnOrExit($case->getStmts(), $withNativeNeverType)) { ++$casesWithReturnCount; } } diff --git a/rules/TypeDeclarationDocblocks/NodeAnalyzer/ConstructorAssignedTypeResolver.php b/rules/TypeDeclarationDocblocks/NodeAnalyzer/ConstructorAssignedTypeResolver.php index 913af1ba7a7..d8cfd5e55b7 100644 --- a/rules/TypeDeclarationDocblocks/NodeAnalyzer/ConstructorAssignedTypeResolver.php +++ b/rules/TypeDeclarationDocblocks/NodeAnalyzer/ConstructorAssignedTypeResolver.php @@ -30,11 +30,11 @@ public function resolve(Class_ $class, string $propertyName): ?Type return null; } - if ($constructorClassMethod->stmts === null) { + if ($constructorClassMethod->getStmts() === []) { return null; } - $assigns = $this->betterNodeFinder->findInstancesOfScoped($constructorClassMethod->stmts, Assign::class); + $assigns = $this->betterNodeFinder->findInstancesOfScoped($constructorClassMethod->getStmts(), Assign::class); foreach ($assigns as $assign) { if (! $assign->var instanceof PropertyFetch) { continue; diff --git a/rules/TypeDeclarationDocblocks/NodeFinder/ArrayDimFetchFinder.php b/rules/TypeDeclarationDocblocks/NodeFinder/ArrayDimFetchFinder.php index a0481e794ce..853c5c9e58d 100644 --- a/rules/TypeDeclarationDocblocks/NodeFinder/ArrayDimFetchFinder.php +++ b/rules/TypeDeclarationDocblocks/NodeFinder/ArrayDimFetchFinder.php @@ -26,7 +26,7 @@ public function __construct( */ public function findDimFetchAssignToVariableName(ClassMethod $classMethod, string $variableName): array { - $assigns = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, Assign::class); + $assigns = $this->betterNodeFinder->findInstancesOfScoped($classMethod->getStmts(), Assign::class); $exprs = []; foreach ($assigns as $assign) { diff --git a/rules/TypeDeclarationDocblocks/NodeFinder/ArrayMapClosureExprFinder.php b/rules/TypeDeclarationDocblocks/NodeFinder/ArrayMapClosureExprFinder.php index 031f2a137f2..3b16f09c1c0 100644 --- a/rules/TypeDeclarationDocblocks/NodeFinder/ArrayMapClosureExprFinder.php +++ b/rules/TypeDeclarationDocblocks/NodeFinder/ArrayMapClosureExprFinder.php @@ -26,12 +26,12 @@ public function __construct( */ public function findByVariableName(ClassMethod|Function_ $functionLike, string $variableName): array { - if ($functionLike->stmts === null) { + if ($functionLike->getStmts() === []) { return []; } /** @var FuncCall[] $funcCalls */ - $funcCalls = $this->betterNodeFinder->findInstancesOfScoped($functionLike->stmts, FuncCall::class); + $funcCalls = $this->betterNodeFinder->findInstancesOfScoped($functionLike->getStmts(), FuncCall::class); $arrayMapClosures = []; diff --git a/rules/TypeDeclarationDocblocks/NodeFinder/GetterClassMethodPropertyFinder.php b/rules/TypeDeclarationDocblocks/NodeFinder/GetterClassMethodPropertyFinder.php index c1ddfefe86a..adf40f0ab25 100644 --- a/rules/TypeDeclarationDocblocks/NodeFinder/GetterClassMethodPropertyFinder.php +++ b/rules/TypeDeclarationDocblocks/NodeFinder/GetterClassMethodPropertyFinder.php @@ -23,11 +23,11 @@ public function __construct( public function find(ClassMethod $classMethod, Class_ $class): Property|Param|null { // we need exactly one statement of return - if ($classMethod->stmts === null || count($classMethod->stmts) !== 1) { + if ($classMethod->getStmts() === [] || count($classMethod->getStmts()) !== 1) { return null; } - $onlyStmt = $classMethod->stmts[0]; + $onlyStmt = $classMethod->getStmts()[0]; if (! $onlyStmt instanceof Return_) { return null; } diff --git a/rules/TypeDeclarationDocblocks/NodeFinder/PropertyGetterFinder.php b/rules/TypeDeclarationDocblocks/NodeFinder/PropertyGetterFinder.php index c8c55a57d43..38f0a0096e7 100644 --- a/rules/TypeDeclarationDocblocks/NodeFinder/PropertyGetterFinder.php +++ b/rules/TypeDeclarationDocblocks/NodeFinder/PropertyGetterFinder.php @@ -31,15 +31,15 @@ public function find(Property $property, Class_ $class): ?ClassMethod continue; } - if ($classMethod->stmts === null) { + if ($classMethod->getStmts() === []) { continue; } - if (count($classMethod->stmts) !== 1) { + if (count($classMethod->getStmts()) !== 1) { continue; } - $onlyStmt = $classMethod->stmts[0]; + $onlyStmt = $classMethod->getStmts()[0]; if (! $onlyStmt instanceof Return_) { continue; } diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php index 5809dc3f431..d130d600264 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php @@ -168,7 +168,7 @@ private function isVariableExclusivelyArrayDimAssigned( ): bool { $isVariableExclusivelyArrayDimAssigned = true; - $this->traverseNodesWithCallable((array) $functionLike->stmts, function ($node) use ( + $this->traverseNodesWithCallable($functionLike->getStmts(), function ($node) use ( $variableName, &$isVariableExclusivelyArrayDimAssigned ): ?int { diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php index 2ffdc107376..3a068f7e849 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php @@ -81,7 +81,7 @@ public function getItems(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { + if ($node->getStmts() === []) { return null; } diff --git a/src/Application/NodeAttributeReIndexer.php b/src/Application/NodeAttributeReIndexer.php index f2051ed47fb..12a4aa5f553 100644 --- a/src/Application/NodeAttributeReIndexer.php +++ b/src/Application/NodeAttributeReIndexer.php @@ -5,6 +5,7 @@ namespace Rector\Application; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; @@ -21,14 +22,13 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\TryCatch; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; final class NodeAttributeReIndexer { public static function reIndexStmtKeyNodeAttributes(Node $node): ?Node { - if (! $node instanceof StmtsAwareInterface && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) { + if (! $node instanceof ContainsStmts && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) { return null; } diff --git a/src/Contract/PhpParser/Node/StmtsAwareInterface.php b/src/Contract/PhpParser/Node/StmtsAwareInterface.php index 7f5cd89b238..1314066920c 100644 --- a/src/Contract/PhpParser/Node/StmtsAwareInterface.php +++ b/src/Contract/PhpParser/Node/StmtsAwareInterface.php @@ -10,6 +10,6 @@ /** * @property Stmt[]|null $stmts */ -interface StmtsAwareInterface extends Node +interface ContainsStmts extends Node { } diff --git a/src/NodeAnalyzer/TerminatedNodeAnalyzer.php b/src/NodeAnalyzer/TerminatedNodeAnalyzer.php index 1a0ec572129..b28ac58becb 100644 --- a/src/NodeAnalyzer/TerminatedNodeAnalyzer.php +++ b/src/NodeAnalyzer/TerminatedNodeAnalyzer.php @@ -5,6 +5,7 @@ namespace Rector\NodeAnalyzer; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Exit_; use PhpParser\Node\Expr\Throw_; @@ -25,7 +26,6 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\TryCatch; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; final class TerminatedNodeAnalyzer @@ -45,7 +45,7 @@ final class TerminatedNodeAnalyzer */ private const ALLOWED_CONTINUE_CURRENT_STMTS = [InlineHTML::class, Nop::class]; - public function isAlwaysTerminated(StmtsAwareInterface $stmtsAware, Stmt $node, Stmt $currentStmt): bool + public function isAlwaysTerminated(ContainsStmts $stmtsAware, Stmt $node, Stmt $currentStmt): bool { if (in_array($currentStmt::class, self::ALLOWED_CONTINUE_CURRENT_STMTS, true)) { return false; diff --git a/src/NodeManipulator/StmtsManipulator.php b/src/NodeManipulator/StmtsManipulator.php index eb018b2c1b1..99e94f04101 100644 --- a/src/NodeManipulator/StmtsManipulator.php +++ b/src/NodeManipulator/StmtsManipulator.php @@ -5,6 +5,7 @@ namespace Rector\NodeManipulator; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Finally_; use PhpParser\Node\Stmt\TryCatch; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -74,7 +74,7 @@ function (Node $node) use (&$stmts): null { } public function isVariableUsedInNextStmt( - StmtsAwareInterface $stmtsAware, + ContainsStmts $stmtsAware, int $jumpToKey, string $variableName ): bool { diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php index 1e3c096b2f3..4dac5a04861 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php @@ -5,6 +5,7 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Global_; use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -26,7 +26,7 @@ public function __construct( public function enterNode(Node $node): ?Node { - if (! $node instanceof StmtsAwareInterface) { + if (! $node instanceof ContainsStmts) { return null; } diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php index fa3556bf986..ccc4d2d1719 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php @@ -5,6 +5,7 @@ namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; use PhpParser\Node; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; @@ -12,7 +13,6 @@ use PhpParser\Node\Stmt\Static_; use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; @@ -26,7 +26,7 @@ public function __construct( public function enterNode(Node $node): ?Node { - if (! $node instanceof StmtsAwareInterface) { + if (! $node instanceof ContainsStmts) { return null; } diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 895b1efe969..cdccf7d908b 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -524,7 +524,7 @@ private function processArrayItem(ArrayItem $arrayItem, MutatingScope $mutatingS private function decorateNodeAttrGroups(Node $node, MutatingScope $mutatingScope, callable $nodeCallback): void { // better to have AttrGroupsAwareInterface for all Node definition with attrGroups property - // but because may conflict with StmtsAwareInterface patch, this needs to be here + // but because may conflict with ContainsStmts patch, this needs to be here if ( ! $node instanceof Param && ! $node instanceof ArrowFunction && diff --git a/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php b/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php index d4e68fd6392..cb8c3289c04 100644 --- a/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php +++ b/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php @@ -4,13 +4,13 @@ namespace Rector\PhpParser\Node\CustomNode; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Stmt; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; /** * Inspired by https://github.com/phpstan/phpstan-src/commit/ed81c3ad0b9877e6122c79b4afda9d10f3994092 */ -final class FileWithoutNamespace extends Stmt implements StmtsAwareInterface +final class FileWithoutNamespace extends Stmt implements ContainsStmts { /** * @param Stmt[] $stmts @@ -33,4 +33,12 @@ public function getSubNodeNames(): array { return ['stmts']; } + + /** + * @return Stmt[] + */ + public function getStmts(): array + { + return $this->stmts; + } } diff --git a/tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php b/tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php index 5b6f7271f2e..8b5b7bee06c 100644 --- a/tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php +++ b/tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php @@ -5,12 +5,12 @@ namespace Rector\Tests\BetterPhpDocParser\PhpDoc\ArrayItemNode; use Nette\Utils\FileSystem as UtilsFileSystem; +use PhpParser\Node\ContainsStmts; use PhpParser\Node\Stmt\Class_; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter; use Rector\Comments\NodeDocBlock\DocBlockUpdater; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator; use Rector\PhpParser\Parser\RectorParser; use Rector\Testing\PHPUnit\AbstractLazyTestCase; @@ -51,15 +51,11 @@ public function testUpdateNestedClassAnnotation(): void $classDocComment = null; foreach ($newStmts as $newStmt) { - if (! $newStmt instanceof StmtsAwareInterface) { + if (! $newStmt instanceof ContainsStmts) { continue; } - if ($newStmt->stmts === null) { - continue; - } - - foreach ($newStmt->stmts as $stmt) { + foreach ($newStmt->getStmts() as $stmt) { if (! $stmt instanceof Class_) { continue; } diff --git a/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php b/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php index 437b5b92630..9a5f060401c 100644 --- a/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php +++ b/tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php @@ -8,7 +8,7 @@ use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; use PhpParser\NodeVisitor; -use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use PhpParser\Node\ContainsStmts; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -22,11 +22,11 @@ public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array { - return [StmtsAwareInterface::class]; + return [ContainsStmts::class]; } /** - * @param StmtsAwareInterface $node + * @param ContainsStmts $node */ public function refactor(Node $node) { @@ -43,4 +43,4 @@ public function refactor(Node $node) return null; } -} \ No newline at end of file +} diff --git a/utils/Rector/MakeUseOfContaintsStmtsRector.php b/utils/Rector/MakeUseOfContaintsStmtsRector.php new file mode 100644 index 00000000000..e665388ece7 --- /dev/null +++ b/utils/Rector/MakeUseOfContaintsStmtsRector.php @@ -0,0 +1,167 @@ +stmts to $node->getStmts() for ContaintsStmts', []); + } + + public function getNodeTypes(): array + { + return [ + Identical::class, + PropertyFetch::class, + Function_::class, + Assign::class, + Foreach_::class, + ClassMethod::class, + ]; + } + + /** + * @param PropertyFetch|Identical|Function_|Assign|Foreach_|ClassMethod $node + * @return MethodCall|Identical|null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN + */ + public function refactor(Node $node): MethodCall|Identical|null|int + { + if ($node instanceof ClassMethod) { + if ($this->isName($node, 'getStmts')) { + // skip getter + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + return null; + } + + if ($node instanceof Foreach_) { + return $this->refactorForeach($node); + } + + if ($node instanceof Function_) { + return $this->refactorFunction($node); + } + + if ($node instanceof Assign) { + return $this->refactorAssign($node); + } + + if ($node instanceof Identical) { + return $this->refactorIdentical($node); + } + + if (! $this->isName($node->name, 'stmts')) { + return null; + } + + if (! $this->isObjectType($node->var, new ObjectType(ContainsStmts::class))) { + return null; + } + + return new MethodCall($node->var, 'getStmts'); + } + + private function isStmtsPropertyFetch(Expr $expr): bool + { + if (! $expr instanceof PropertyFetch) { + return false; + } + + return $this->isName($expr->name, 'stmts'); + } + + private function refactorIdentical(Identical $identical): ?Identical + { + if (! $this->valueResolver->isNull($identical->right)) { + return null; + } + + if ($this->isStmtsPropertyFetch($identical->left)) { + /** @var PropertyFetch $propertyFetch */ + $propertyFetch = $identical->left; + + $identical->left = new MethodCall($propertyFetch->var, 'getStmts'); + $identical->right = new Array_([]); + + return $identical; + } + + return null; + } + + /** + * @return null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN + */ + private function refactorForeach(Foreach_ $foreach): ?int + { + if (! $this->isStmtsPropertyFetch($foreach->expr)) { + return null; + } + // skip $node->stmts in foreach with key, as key is probably used on the $node->stmts + if ($foreach->keyVar instanceof Expr) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + return null; + } + + private function refactorFunction(Function_ $function): ?int + { + // keep any unset($node->stmts[x])) calls + if ($this->isName($function->name, 'unset')) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + return null; + } + + /** + * @return null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN + */ + private function refactorAssign(Assign $assign): ?int + { + if ($assign->var instanceof ArrayDimFetch) { + $arrayDimFetch = $assign->var; + if ($this->isStmtsPropertyFetch($arrayDimFetch->var)) { + // keep $node->stmts[x] = ... + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + } + + if (! $this->isStmtsPropertyFetch($assign->var)) { + return null; + } + + // keep assign to $node->stmts property + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } +}