Skip to content

Commit

Permalink
[TypeDeclaration] Add init assign method call support on ReturnTypeFr…
Browse files Browse the repository at this point in the history
…omStrictNewArrayRector (#4929)
  • Loading branch information
samsonasik committed Sep 7, 2023
1 parent 2a697f7 commit e47f631
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
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;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Source\OrderRepository;

final class InitAssignMethodCall
{
public function fetchAllForBuyer(OrderRepository $orderRepository)
{
$orders = $orderRepository->fetchAllForBuyer();

foreach ($orders as $order) {

}

return $orders;
}
}

?>
-----
<?php

declare(strict_types=1);

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Source\OrderRepository;

final class InitAssignMethodCall
{
/**
* @return mixed[]
*/
public function fetchAllForBuyer(OrderRepository $orderRepository): array
{
$orders = $orderRepository->fetchAllForBuyer();

foreach ($orders as $order) {

}

return $orders;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

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

final class OrderRepository
{
public function fetchAllForBuyer(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,10 @@ private function matchArrayAssignedVariable(array $stmts): Variable|null
continue;
}

if (! $assign->expr instanceof Array_) {
continue;
$nativeType = $this->nodeTypeResolver->getNativeType($assign->expr);
if ($nativeType->isArray()->yes()) {
return $assign->var;
}

return $assign->var;
}

return null;
Expand Down

0 comments on commit e47f631

Please sign in to comment.