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,44 @@
<?php

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

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

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

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

?>
-----
<?php

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

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

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

public function getRepository(): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository
{
return $this->em->getRepository(Event::class);
}
}

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

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

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

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

public function getRepository(): EventRepository
{
return $this->em->getRepository(Event::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use Doctrine\ORM\EntityManagerInterface;

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

public function getRepository()
{
return $this->em->getRepository(Event::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

use Doctrine\ORM\EntityManagerInterface;

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

/**
* @return \App\NonExisting\MissingRepository
*/
public function getRepository()
{
return $this->em->getRepository(Event::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\Source\EventRepository;

final class SkipNotEntityManager
{
public function __construct(private \stdClass $registry)
{
}

/**
* @return EventRepository
*/
public function getRepository()
{
return $this->registry->getRepository(Event::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReturnTypeFromGetRepositoryDocblockRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

final class EventRepository
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector;

return RectorConfig::configure()
->withRules([ReturnTypeFromGetRepositoryDocblockRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector\ReturnTypeFromGetRepositoryDocblockRectorTest
*/
final class ReturnTypeFromGetRepositoryDocblockRector extends AbstractRector
{
private const string ENTITY_MANAGER_INTERFACE = 'Doctrine\ORM\EntityManagerInterface';

public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly ReflectionProvider $reflectionProvider,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add repository return type declaration based on @return docblock, when a method returns entity manager getRepository() fetch',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class EventModel
{
public function __construct(private \Doctrine\ORM\EntityManagerInterface $em)
{
}

/**
* @return \App\Repository\EventRepository
*/
public function getRepository()
{
return $this->em->getRepository(Event::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class EventModel
{
public function __construct(private \Doctrine\ORM\EntityManagerInterface $em)
{
}

public function getRepository(): \App\Repository\EventRepository
{
return $this->em->getRepository(Event::class);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
// return type is already set
if ($node->returnType instanceof Node) {
return null;
}

if (! $this->isGetRepositoryReturnOnly($node)) {
return null;
}

$scope = ScopeFetcher::fetch($node);
if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}

$returnType = $phpDocInfo->getReturnType();
if (! $returnType instanceof ObjectType) {
return null;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Name) {
return null;
}

// must be an existing repository class
if (! $this->reflectionProvider->hasClass($returnTypeNode->toString())) {
return null;
}

$node->returnType = $returnTypeNode;
$this->removeReturnTag($phpDocInfo, $node);

return $node;
}

private function isGetRepositoryReturnOnly(ClassMethod $classMethod): bool
{
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
if (count($returns) !== 1) {
return false;
}

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

if (! $this->isName($methodCall->name, 'getRepository')) {
return false;
}

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

private function isEntityManagerType(MethodCall $methodCall): bool
{
$callerType = $this->nodeTypeResolver->getType($methodCall->var);
if (! $callerType instanceof ObjectType) {
return false;
}

if ($callerType->getClassName() === self::ENTITY_MANAGER_INTERFACE) {
return true;
}

return $callerType->isInstanceOf(self::ENTITY_MANAGER_INTERFACE)
->yes();
}

private function removeReturnTag(PhpDocInfo $phpDocInfo, ClassMethod $classMethod): void
{
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
return;
}

$phpDocInfo->removeByType(ReturnTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}
}
4 changes: 4 additions & 0 deletions src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNullableTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector;
Expand Down Expand Up @@ -188,5 +189,8 @@ final class TypeDeclarationLevel
NarrowArrayAnyAllNullableParamTypeRector::class,
AddArrayAnyAllClosureParamTypeRector::class,
ClosureReturnTypeFromAssertInstanceOfRector::class,

// docblock @return into native return type, intentionally last
ReturnTypeFromGetRepositoryDocblockRector::class,
];
}
Loading