From 520726f7135d847ee04c3df2e62f964cb763fa1a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 10:27:12 +0200 Subject: [PATCH 1/2] [Transform] Deprecate ArrayDimFetchToMethodCallRector as too niche and able to create invalid calls --- .../ArrayDimFetchToMethodCallRectorTest.php | 28 --- .../Fixture/fixture.php.inc | 23 -- .../Fixture/handle_container_get.php.inc | 17 -- .../Fixture/skip_assign_ops.php.inc | 8 - .../skip_container_set_isset_unset.php.inc | 8 - .../Fixture/skip_non_matching_object.php.inc | 8 - .../transforms_multiple_issets.php.inc | 23 -- .../transforms_multiple_unsets.php.inc | 21 -- .../Fixture/transforms_property_fetch.php.inc | 37 ---- .../Source/SomeExtendedClass.php | 10 - .../config/configured_rule.php | 16 -- rules/Transform/Enum/MagicPropertyHandler.php | 16 -- .../ArrayDimFetchToMethodCallRector.php | 198 +----------------- .../ValueObject/ArrayDimFetchToMethodCall.php | 46 ---- 14 files changed, 11 insertions(+), 448 deletions(-) delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/ArrayDimFetchToMethodCallRectorTest.php delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/handle_container_get.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_assign_ops.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_container_set_isset_unset.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_non_matching_object.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_issets.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_unsets.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_property_fetch.php.inc delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Source/SomeExtendedClass.php delete mode 100644 rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/config/configured_rule.php delete mode 100644 rules/Transform/Enum/MagicPropertyHandler.php delete mode 100644 rules/Transform/ValueObject/ArrayDimFetchToMethodCall.php diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/ArrayDimFetchToMethodCallRectorTest.php b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/ArrayDimFetchToMethodCallRectorTest.php deleted file mode 100644 index c7cbeead72f..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/ArrayDimFetchToMethodCallRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/fixture.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/fixture.php.inc deleted file mode 100644 index 918de0ed276..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,23 +0,0 @@ - ------ -get('key'); -$object->set('key', 42); -$object->has('key'); -$object->unset('key'); - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/handle_container_get.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/handle_container_get.php.inc deleted file mode 100644 index 603557ba726..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/handle_container_get.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - ------ -get('key'); - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_assign_ops.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_assign_ops.php.inc deleted file mode 100644 index 25313e4ee49..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_assign_ops.php.inc +++ /dev/null @@ -1,8 +0,0 @@ -get() - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_issets.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_issets.php.inc deleted file mode 100644 index 70525585214..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_issets.php.inc +++ /dev/null @@ -1,23 +0,0 @@ - ------ -has('key1') && $object->has('key2')) { - echo 'Keys are set'; -} - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_unsets.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_unsets.php.inc deleted file mode 100644 index 6974b05f432..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_multiple_unsets.php.inc +++ /dev/null @@ -1,21 +0,0 @@ - ------ -unset('key1'); -$object->unset('key2'); - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_property_fetch.php.inc b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_property_fetch.php.inc deleted file mode 100644 index fb7d9fd2671..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/transforms_property_fetch.php.inc +++ /dev/null @@ -1,37 +0,0 @@ -something['key']; - $this->something['key'] = 42; - isset($this->something['key']); - unset($this->something['key']); - } -} - -?> ------ -something->get('key'); - $this->something->set('key', 42); - $this->something->has('key'); - $this->something->unset('key'); - } -} - -?> diff --git a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Source/SomeExtendedClass.php b/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Source/SomeExtendedClass.php deleted file mode 100644 index a29665e194e..00000000000 --- a/rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Source/SomeExtendedClass.php +++ /dev/null @@ -1,10 +0,0 @@ -ruleWithConfiguration(ArrayDimFetchToMethodCallRector::class, [ - new ArrayDimFetchToMethodCall(new ObjectType('SomeClass'), 'get', 'set', 'has', 'unset'), - new ArrayDimFetchToMethodCall(new ObjectType('Container'), 'get'), - ]); -}; diff --git a/rules/Transform/Enum/MagicPropertyHandler.php b/rules/Transform/Enum/MagicPropertyHandler.php deleted file mode 100644 index 031c848d065..00000000000 --- a/rules/Transform/Enum/MagicPropertyHandler.php +++ /dev/null @@ -1,16 +0,0 @@ -get('key'); -$object->set('key', 'value'); -$object->has('key'); -$object->unset('key'); CODE_SAMPLE - , - [new ArrayDimFetchToMethodCall(new ObjectType('SomeClass'), 'get', 'set', 'has', 'unset')], ), ]); } @@ -63,164 +40,11 @@ public function getNodeTypes(): array return [Assign::class, Isset_::class, Unset_::class, ArrayDimFetch::class]; } - /** - * @param ArrayDimFetch|Assign|Isset_|Unset_ $node - * @return ($node is Unset_ ? Stmt[] : ($node is Isset_ ? Expr : MethodCall|null)) - */ - public function refactor(Node $node): array|Expr|null + public function refactor(Node $node): ?Node { - if ($node instanceof Unset_) { - return $this->handleUnset($node); - } - - if ($node instanceof Isset_) { - return $this->handleIsset($node); - } - - if ($node instanceof Assign) { - if (! $node->var instanceof ArrayDimFetch) { - return null; - } - - return $this->createExplicitMethodCall($node->var, MagicPropertyHandler::SET, $node->expr); - } - - // is part of assign, skip - if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) { - return null; - } - - if ($node->getAttribute(AttributeKey::IS_ASSIGN_OP_VAR)) { - return null; - } - - // should be skipped as handled above - if ($node->getAttribute(AttributeKey::IS_UNSET_VAR)) { - return null; - } - - if ($node->getAttribute(AttributeKey::IS_ISSET_VAR)) { - return null; - } - - return $this->createExplicitMethodCall($node, MagicPropertyHandler::GET); - } - - public function configure(array $configuration): void - { - Assert::allIsInstanceOf($configuration, ArrayDimFetchToMethodCall::class); - - $this->arrayDimFetchToMethodCalls = $configuration; - } - - private function handleIsset(Isset_ $isset): Expr|null - { - $issets = []; - $exprs = []; - - foreach ($isset->vars as $var) { - if ($var instanceof ArrayDimFetch) { - $methodCall = $this->createExplicitMethodCall($var, 'exists'); - - if ($methodCall instanceof MethodCall) { - $exprs[] = $methodCall; - continue; - } - } - - $issets[] = $var; - } - - if ($exprs === []) { - // nothing to handle - return null; - } - - if ($issets !== []) { - $isset->vars = $issets; - array_unshift($exprs, $isset); - } - - return array_reduce( - $exprs, - fn (?Expr $carry, Expr $expr): Isset_|MethodCall|BooleanAnd => $carry instanceof Expr ? new BooleanAnd( - $carry, - $expr - ) : $expr, - ); - } - - /** - * @return Stmt[]|null - */ - private function handleUnset(Unset_ $unset): ?array - { - $unsets = []; - $stmts = []; - - foreach ($unset->vars as $var) { - if ($var instanceof ArrayDimFetch) { - $methodCall = $this->createExplicitMethodCall($var, 'unset'); - - if ($methodCall instanceof MethodCall) { - $stmts[] = new Expression($methodCall); - continue; - } - } - - $unsets[] = $var; - } - - // nothing to change - if ($stmts === []) { - return null; - } - - if ($unsets !== []) { - $unset->vars = $unsets; - array_unshift($stmts, $unset); - } - - return $stmts; - } - - /** - * @param MagicPropertyHandler::* $magicPropertyHandler - */ - private function createExplicitMethodCall( - ArrayDimFetch $arrayDimFetch, - string $magicPropertyHandler, - ?Expr $expr = null - ): ?MethodCall { - if (! $arrayDimFetch->dim instanceof Node) { - return null; - } - - foreach ($this->arrayDimFetchToMethodCalls as $arrayDimFetchToMethodCall) { - $method = match ($magicPropertyHandler) { - MagicPropertyHandler::GET => $arrayDimFetchToMethodCall->getMethod(), - MagicPropertyHandler::SET => $arrayDimFetchToMethodCall->getSetMethod(), - MagicPropertyHandler::ISSET_ => $arrayDimFetchToMethodCall->getExistsMethod(), - MagicPropertyHandler::UNSET => $arrayDimFetchToMethodCall->getUnsetMethod(), - }; - - if ($method === null) { - continue; - } - - if (! $this->isObjectType($arrayDimFetch->var, $arrayDimFetchToMethodCall->getObjectType())) { - continue; - } - - $args = [new Arg($arrayDimFetch->dim)]; - - if ($expr instanceof Expr) { - $args[] = new Arg($expr); - } - - return new MethodCall($arrayDimFetch->var, $method, $args); - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as too niche and never used. Use a specific rule instead', + self::class + )); } } diff --git a/rules/Transform/ValueObject/ArrayDimFetchToMethodCall.php b/rules/Transform/ValueObject/ArrayDimFetchToMethodCall.php deleted file mode 100644 index 7a19f9415d6..00000000000 --- a/rules/Transform/ValueObject/ArrayDimFetchToMethodCall.php +++ /dev/null @@ -1,46 +0,0 @@ -objectType; - } - - public function getMethod(): string - { - return $this->method; - } - - public function getSetMethod(): ?string - { - return $this->setMethod; - } - - public function getExistsMethod(): ?string - { - return $this->existsMethod; - } - - public function getUnsetMethod(): ?string - { - return $this->unsetMethod; - } -} From 6202146b79876ba248bdc71c5069d2a6864221ef Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 22 Oct 2026 08:28:09 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php b/rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php index be24ac6b5a9..63175082f2a 100644 --- a/rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php +++ b/rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php @@ -40,6 +40,9 @@ public function getNodeTypes(): array return [Assign::class, Isset_::class, Unset_::class, ArrayDimFetch::class]; } + /** + * @param Assign|Isset_|Unset_|ArrayDimFetch $node + */ public function refactor(Node $node): ?Node { throw new ShouldNotHappenException(sprintf(