Skip to content

Commit

Permalink
Fix ChildDoctrineRepositoryClassTypeRector for generic type (#5746)
Browse files Browse the repository at this point in the history
* add generics fixture

* Skip generic type in ChildDoctrineRepositoryClassTypeRector
  • Loading branch information
TomasVotruba committed Mar 21, 2024
1 parent 2689095 commit f4070bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture;

use Doctrine\ORM\EntityRepository;

/**
* @template T of object
* @extends EntityRepository<T>
*/
final class ExtendedEntityRepository extends EntityRepository
{
/**
* Get an object or throws an exception
*
* @param array<string, mixed> $conditions
*
* @return T
*/
public function findOneByOrThrow(array $conditions) {
$entity = $this->findOneBy($conditions);
if ($entity === null) {
throw new \RuntimeException();
}
return $entity;
}
}
Expand Up @@ -43,7 +43,9 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO

$originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME);
$originalNameToAttribute = null;
if ($originalName instanceof Name && ! $originalName instanceof FullyQualified && $originalName->hasAttribute(AttributeKey::PHP_ATTRIBUTE_NAME)) {
if ($originalName instanceof Name && ! $originalName instanceof FullyQualified && $originalName->hasAttribute(
AttributeKey::PHP_ATTRIBUTE_NAME
)) {
$originalNameToAttribute = $originalName->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
}

Expand Down
Expand Up @@ -165,11 +165,15 @@ private function resolveEntityClassnameFromPhpDoc(Class_ $class): ?string
}

$entityGenericType = $genericTypeNode->genericTypes[0];

if (! $entityGenericType instanceof IdentifierTypeNode) {
return null;
}

// skip if value is used in generics
if (in_array($entityGenericType->name, $classPhpDocInfo->getTemplateNames(), true)) {
return null;
}

return $entityGenericType->name;
}

Expand Down
3 changes: 2 additions & 1 deletion scoper.php
Expand Up @@ -117,7 +117,8 @@ static function (string $filePath, string $prefix, string $content): string {
}
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit, 0, $flags]);
CODE_REPLACE,
CODE_REPLACE
,
$content
);
},
Expand Down
13 changes: 13 additions & 0 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Expand Up @@ -363,6 +363,19 @@ public function getParamTagValueByName(string $name): ?ParamTagValueNode
return null;
}

/**
* @return string[]
*/
public function getTemplateNames(): array
{
$templateNames = [];
foreach ($this->phpDocNode->getTemplateTagValues() as $templateTagValueNode) {
$templateNames[] = $templateTagValueNode->name;
}

return $templateNames;
}

/**
* @return TemplateTagValueNode[]
*/
Expand Down

0 comments on commit f4070bb

Please sign in to comment.