Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository;

final class GetRepositoryViaVariable
{
public function __construct(private EntityManagerInterface $em)
{
}

/**
* @return EventRepository
*/
public function getRepository()
{
/** @var EventRepository $repo */
$repo = $this->em->getRepository(Event::class);

$repo->setDispatcher($this->dispatcher);

return $repo;
}
}

?>
-----
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository;

final class GetRepositoryViaVariable
{
public function __construct(private EntityManagerInterface $em)
{
}

public function getRepository(): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository
{
/** @var EventRepository $repo */
$repo = $this->em->getRepository(Event::class);

$repo->setDispatcher($this->dispatcher);

return $repo;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository;

final class SkipReassignedVariable
{
public function __construct(private EntityManagerInterface $em)
{
}

/**
* @return EventRepository
*/
public function getRepository()
{
$repo = $this->em->getRepository(Event::class);
$repo = someOther();

return $repo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
Expand Down Expand Up @@ -141,18 +143,61 @@ private function isGetRepositoryReturnOnly(ClassMethod $classMethod): bool
return false;
}

$methodCall = $returns[0]->expr;
if (! $methodCall instanceof MethodCall) {
return false;
$returnedExpr = $returns[0]->expr;

// direct return: return $this->em->getRepository(...);
if ($returnedExpr instanceof MethodCall) {
return $this->isGetRepositoryMethodCall($returnedExpr);
}

// indirect return: $repo = $this->em->getRepository(...); ...; return $repo;
if ($returnedExpr instanceof Variable) {
$assignedMethodCall = $this->findVariableAssignMethodCall($classMethod, $returnedExpr);
if (! $assignedMethodCall instanceof MethodCall) {
return false;
}

return $this->isGetRepositoryMethodCall($assignedMethodCall);
}

return false;
}

private function isGetRepositoryMethodCall(MethodCall $methodCall): bool
{
if (! $this->isName($methodCall->name, 'getRepository')) {
return false;
}

return $this->isEntityManagerType($methodCall);
}

private function findVariableAssignMethodCall(ClassMethod $classMethod, Variable $variable): ?MethodCall
{
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf($classMethod, Assign::class);

$methodCall = null;
foreach ($assigns as $assign) {
if (! $assign->var instanceof Variable) {
continue;
}

if (! $this->nodeComparator->areNodesEqual($assign->var, $variable)) {
continue;
}

// reassigned to something else, cannot be sure of the type
if (! $assign->expr instanceof MethodCall) {
return null;
}

$methodCall = $assign->expr;
}

return $methodCall;
}

private function isEntityManagerType(MethodCall $methodCall): bool
{
$callerType = $this->nodeTypeResolver->getType($methodCall->var);
Expand Down
Loading