From e4d375134a9ff87bc43313952283ef57497564c3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 4 Sep 2023 12:05:53 +0200 Subject: [PATCH 1/3] StrictArrayParamDimFetchRector: Skip objects --- .../skip_possible_array_access_object.php.inc | 13 +++++++++++++ .../ClassMethod/StrictArrayParamDimFetchRector.php | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_possible_array_access_object.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_possible_array_access_object.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_possible_array_access_object.php.inc new file mode 100644 index 00000000000..e46f41646ba --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_possible_array_access_object.php.inc @@ -0,0 +1,13 @@ +doSomething(); + + return $item['name']; + } +} diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index 576a590b5fe..aad3ec633ab 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -159,6 +159,10 @@ private function shouldStop(Node $node, string $paramName): bool $nodeToCheck = $firstArg->value; } + if ($node instanceof Node\Stmt\Expression) { + $nodeToCheck = $node->expr; + } + if ($node instanceof Coalesce) { $nodeToCheck = $node->left; } @@ -167,6 +171,10 @@ private function shouldStop(Node $node, string $paramName): bool $nodeToCheck = $node->var; } + if ($nodeToCheck instanceof Node\Expr\MethodCall && $this->isName($nodeToCheck->var, $paramName)) { + return true; + } + if ($nodeToCheck instanceof ArrayDimFetch) { return $nodeToCheck->var instanceof Variable && $this->isName($nodeToCheck->var, $paramName); } From 8b5faa54a051a5fc2e0084d11012e9301320acd9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 4 Sep 2023 12:24:26 +0200 Subject: [PATCH 2/3] feedback --- .../Rector/ClassMethod/StrictArrayParamDimFetchRector.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index aad3ec633ab..26f5e83e93c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -171,7 +171,9 @@ private function shouldStop(Node $node, string $paramName): bool $nodeToCheck = $node->var; } - if ($nodeToCheck instanceof Node\Expr\MethodCall && $this->isName($nodeToCheck->var, $paramName)) { + if ($nodeToCheck instanceof Node\Expr\MethodCall + && $nodeToCheck->var instanceof Variable + && $this->isName($nodeToCheck->var, $paramName)) { return true; } From fde89cd98077cd548eebe41b6fa025c4e78c2daa Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 4 Sep 2023 12:25:56 +0200 Subject: [PATCH 3/3] simplify --- .../Rector/ClassMethod/StrictArrayParamDimFetchRector.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index 26f5e83e93c..bdb4ebfbdbc 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -171,10 +171,8 @@ private function shouldStop(Node $node, string $paramName): bool $nodeToCheck = $node->var; } - if ($nodeToCheck instanceof Node\Expr\MethodCall - && $nodeToCheck->var instanceof Variable - && $this->isName($nodeToCheck->var, $paramName)) { - return true; + if ($nodeToCheck instanceof Node\Expr\MethodCall) { + return $nodeToCheck->var instanceof Variable && $this->isName($nodeToCheck->var, $paramName); } if ($nodeToCheck instanceof ArrayDimFetch) {