diff --git a/rules/Naming/Naming/ExpectedNameResolver.php b/rules/Naming/Naming/ExpectedNameResolver.php index 1409f470aaa..d8547ca012f 100644 --- a/rules/Naming/Naming/ExpectedNameResolver.php +++ b/rules/Naming/Naming/ExpectedNameResolver.php @@ -20,8 +20,8 @@ use PHPStan\Type\Type; use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; use Rector\Naming\ValueObject\ExpectedName; +use Rector\Naming\ValueObject\VariableAndCallForeach; use Rector\NodeNameResolver\NodeNameResolver; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; @@ -146,8 +146,9 @@ public function resolveForCall(MethodCall | StaticCall | FuncCall $expr): ?strin return null; } - public function resolveForForeach(MethodCall | StaticCall | FuncCall $expr): ?string + public function resolveForForeach(VariableAndCallForeach $variableAndCallForeach): ?string { + $expr = $variableAndCallForeach->getCall(); if ($this->isDynamicNameCall($expr)) { return null; } @@ -164,7 +165,7 @@ public function resolveForForeach(MethodCall | StaticCall | FuncCall $expr): ?st $innerReturnedType = null; if ($returnedType instanceof ArrayType) { - $innerReturnedType = $this->resolveReturnTypeFromArrayType($expr, $returnedType); + $innerReturnedType = $this->resolveReturnTypeFromArrayType($returnedType); if (! $innerReturnedType instanceof Type) { return null; } @@ -208,13 +209,8 @@ private function isDynamicNameCall(MethodCall | StaticCall | FuncCall $expr): bo return $expr->name instanceof FuncCall; } - private function resolveReturnTypeFromArrayType(FuncCall|MethodCall|StaticCall $expr, ArrayType $arrayType): ?Type + private function resolveReturnTypeFromArrayType(ArrayType $arrayType): ?Type { - $parentNode = $expr->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentNode instanceof Foreach_) { - return null; - } - if (! $arrayType->getItemType() instanceof ObjectType) { return null; } diff --git a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php index 7805bf1d560..4bae478fd2d 100644 --- a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php @@ -85,7 +85,7 @@ public function refactor(Node $node): ?Node return null; } - $expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach->getCall()); + $expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach); if ($expectedName === null) { return null; }