From 88a4185133acb22bc76f2adaf38afa64675774e0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 1 Oct 2025 09:42:42 +0200 Subject: [PATCH] [type-declarations] Open isset check in StrictArrayParamDimFetchRectorTest, move to last position in type-declaration level --- .../Fixture/different_usage.php.inc | 2 +- .../with_array_filter_and_array_walk.php.inc | 2 +- .../try_catch_finally_same_type.php.inc | 2 +- .../Fixture/func_call_found.php.inc | 2 +- .../cover_isset_with_dim_fetch.php.inc | 43 +++++++++++++++++++ .../Fixture/include_isset_index.php.inc | 27 ++++++++++++ .../Fixture/skip_isset_index.php.inc | 11 ----- .../StrictArrayParamDimFetchRector.php | 30 ++++++------- ...wFunctionParamArrayWhereDimFetchRector.php | 2 +- src/Config/Level/TypeDeclarationLevel.php | 4 +- 10 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/cover_isset_with_dim_fetch.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/include_isset_index.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_isset_index.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc index cd882d64acf..4e77ccb4457 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc @@ -51,4 +51,4 @@ final class DifferentUsage { } } -?> \ No newline at end of file +?> diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc index 41f1f5d296e..cdd7a0e2d49 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc @@ -51,4 +51,4 @@ final class WithArrayFilterAndArrayWalk } } -?> \ No newline at end of file +?> diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc index b31edee97fe..ec8bd15ab75 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc @@ -36,4 +36,4 @@ final class TryCatchFinallySameType } } -?> \ No newline at end of file +?> diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/func_call_found.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/func_call_found.php.inc index 3b7534b4fdc..ca60150b9b7 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/func_call_found.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/func_call_found.php.inc @@ -24,4 +24,4 @@ class FuncCallFound } } -?> \ No newline at end of file +?> diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/cover_isset_with_dim_fetch.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/cover_isset_with_dim_fetch.php.inc new file mode 100644 index 00000000000..917d1b0e17e --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/cover_isset_with_dim_fetch.php.inc @@ -0,0 +1,43 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/include_isset_index.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/include_isset_index.php.inc new file mode 100644 index 00000000000..2efe88605bb --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/include_isset_index.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_isset_index.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_isset_index.php.inc deleted file mode 100644 index 78d924eb3c5..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_isset_index.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -typeComparator->isSubtype( - $variableType, - new ObjectType('ArrayAccess') - )) { + if ($this->isArrayAccess($variableType)) { $isParamAccessedArrayDimFetch = false; return NodeVisitor::STOP_TRAVERSAL; } @@ -216,18 +213,8 @@ private function shouldStop(Node $node, Param $param, string $paramName): bool { $nodeToCheck = null; - if (! $param->default instanceof Expr) { - if ($node instanceof Isset_) { - foreach ($node->vars as $var) { - if ($var instanceof ArrayDimFetch && $var->var instanceof Variable && $var->var->name === $paramName) { - return true; - } - } - } - - if ($node instanceof Empty_ && $node->expr instanceof ArrayDimFetch && $node->expr->var instanceof Variable && $node->expr->var->name === $paramName) { - return true; - } + if (! $param->default instanceof Expr && ($node instanceof Empty_ && $node->expr instanceof ArrayDimFetch && $node->expr->var instanceof Variable && $node->expr->var->name === $paramName)) { + return true; } if ($node instanceof FuncCall @@ -320,4 +307,13 @@ private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bo return false; } + + private function isArrayAccess(Type $type): bool + { + if (! $type instanceof ObjectType) { + return false; + } + + return $this->typeComparator->isSubtype($type, new ObjectType('ArrayAccess')); + } } diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 7d04f95de39..1fa3c45f010 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -33,7 +33,7 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Add function/closure param array type, if dim fetch is inside', [ + return new RuleDefinition('Add closure and arrow function param array type, if dim fetch is used inside', [ new CodeSample( <<<'CODE_SAMPLE' $array = [['name' => 'John']]; diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index f2414fc4751..88c97dfdd15 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -137,10 +137,12 @@ final class TypeDeclarationLevel AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnNeverTypeRector::class, - StrictArrayParamDimFetchRector::class, StrictStringParamConcatRector::class, TypedPropertyFromJMSSerializerAttributeTypeRector::class, + // array parameter from dim fetch assign inside + StrictArrayParamDimFetchRector::class, + // possibly based on docblocks, but also helpful, intentionally last AddArrayFunctionClosureParamTypeRector::class, TypedPropertyFromDocblockSetUpDefinedRector::class,