From 798b3794ebfb9f3cd452a340b124d98fcd9f895e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 13 Oct 2025 15:50:19 +0700 Subject: [PATCH] [TypeDeclaration] Skip with is_array() on AddParamFromDimFetchKeyUseRector --- .../Fixture/skip_with_is_array.php.inc | 22 +++++++++++++++++++ .../Source/SomeObjectValidation.php | 14 ++++++++++++ .../AddParamFromDimFetchKeyUseRector.php | 9 ++++++++ 3 files changed, 45 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_with_is_array.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Source/SomeObjectValidation.php diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_with_is_array.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_with_is_array.php.inc new file mode 100644 index 00000000000..1f15a958dfb --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_with_is_array.php.inc @@ -0,0 +1,22 @@ +|string $key + */ + public function __construct($object, $key) + { + if (\is_array($key) && (string) $object->{$key[1]} !== '') { + } + + if (isset($object->validation_errors[$key])) { + } + } + +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Source/SomeObjectValidation.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Source/SomeObjectValidation.php new file mode 100644 index 00000000000..e713051ac26 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Source/SomeObjectValidation.php @@ -0,0 +1,14 @@ + + */ + public $validation_errors = []; +} \ No newline at end of file diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php index f5890898da5..ad4e9e8b183 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php @@ -5,9 +5,11 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; +use PHPStan\Type\UnionType; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -113,6 +115,13 @@ public function refactor(Node $node): ?Node continue; } + if ($dimFetch->dim instanceof Variable) { + $type = $this->nodeTypeResolver->getType($dimFetch->dim); + if ($type instanceof UnionType) { + continue; + } + } + $paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( $dimFetchType->getKeyType(), TypeKind::PARAM