Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Add append in loop support on ReturnTypeFromStrictNewArrayRector #4922

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class AppendInLoop
{
public function groupOffers($offers)
{
$groupOffers = [];
$groupedOffers = [];

foreach ($offers as $offer) {
$groupedOffers[$offer->groupid.'--'.$offer->tarifid] = $offer;
$groupedOffers[$offer->groupid.'--'.$offer->tarifid]->groupedOffers = $groupOffers;
}

return $groupedOffers;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class AppendInLoop
{
/**
* @return mixed[]
*/
public function groupOffers($offers): array
{
$groupOffers = [];
$groupedOffers = [];

foreach ($offers as $offer) {
$groupedOffers[$offer->groupid.'--'.$offer->tarifid] = $offer;
$groupedOffers[$offer->groupid.'--'.$offer->tarifid]->groupedOffers = $groupOffers;
}

return $groupedOffers;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,11 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

$returnType = $this->nodeTypeResolver->getType($onlyReturn->expr);
$returnType = $this->nodeTypeResolver->getNativeType($onlyReturn->expr);
if (! $returnType instanceof ArrayType) {
return null;
}

if (! $this->nodeNameResolver->areNamesEqual($onlyReturn->expr, $variable)) {
return null;
}

// 3. always returns array
$node->returnType = new Identifier('array');

Expand Down