From 97a19e266005d046e6858ba598294bc96cf7063a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 3 Jul 2026 14:05:18 +0200 Subject: [PATCH] [TypeDeclaration] Skip property fetch on array dim fetch in StrictArrayParamDimFetchRector --- .../Fixture/skip_instanceof_param.php.inc | 19 +++++++++++ .../skip_property_fetch_on_dim_fetch.php.inc | 22 +++++++++++++ .../Source/Lead.php | 16 +++++++++ .../StrictArrayParamDimFetchRector.php | 33 +++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_instanceof_param.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_property_fetch_on_dim_fetch.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Source/Lead.php diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_instanceof_param.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_instanceof_param.php.inc new file mode 100644 index 00000000000..8c2d33fd1d1 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_instanceof_param.php.inc @@ -0,0 +1,19 @@ +getPrimaryCompany(); + } else { + $fields = $entity['primaryCompany']; + } + + return $fields; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_property_fetch_on_dim_fetch.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_property_fetch_on_dim_fetch.php.inc new file mode 100644 index 00000000000..8b5933b0308 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_property_fetch_on_dim_fetch.php.inc @@ -0,0 +1,22 @@ + $value) { + if (0 === $key) { + continue; + } + + $object[0]->$key = $value; + } + + return $object[0]; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Source/Lead.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Source/Lead.php new file mode 100644 index 00000000000..fce4c6660a7 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Source/Lead.php @@ -0,0 +1,16 @@ +isPropertyFetchedOnArrayDimFetch($node, $paramName)) { + return true; + } + + if ($this->isInstanceofParam($node, $paramName)) { + return true; + } + return $this->isReassignAndUseAsArg($node, $paramName); } @@ -288,6 +299,28 @@ private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool return $node instanceof Array_ && $node->expr instanceof Variable && $this->isName($node->expr, $paramName); } + private function isPropertyFetchedOnArrayDimFetch(Node $node, string $paramName): bool + { + if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) { + return false; + } + + $fetchedOn = $node instanceof PropertyFetch ? $node->var : $node->class; + if (! $fetchedOn instanceof ArrayDimFetch) { + return false; + } + + return $fetchedOn->var instanceof Variable && $this->isName($fetchedOn->var, $paramName); + } + + private function isInstanceofParam(Node $node, string $paramName): bool + { + return $node instanceof Instanceof_ && $node->expr instanceof Variable && $this->isName( + $node->expr, + $paramName + ); + } + private function isMethodCall(string $paramName, ?Node $node): bool { if ($node instanceof MethodCall) {