Skip to content

Commit

Permalink
[Naming] Fix generics RenamePropertyToMatchTypeRector (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 9, 2021
1 parent db63797 commit a6ce5e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;

use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository;
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Entity\SomeEntity;

final class SkipRepositoryGeneric
{
/**
* @var ObjectRepository<SomeEntity>
*/
private $timestampableRepository;

protected function setUp(): void
{
parent::setUp();

$this->timestampableRepository = $this->entityManager->getRepository(SomeEntity::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine;

/**
* @template T
*/
interface ObjectRepository
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Entity;

final class SomeEntity
{
}
6 changes: 6 additions & 0 deletions rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\Utils\Strings;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -83,6 +84,11 @@ public function getExpectedNameFromType(Type $type): ?ExpectedName

$className = $this->nodeTypeResolver->getFullyQualifiedClassName($type);

// generic types are usually mix of parent type and specific type - various way to handle it
if ($type instanceof GenericObjectType) {
return null;
}

foreach (self::EXCLUDED_CLASSES as $excludedClass) {
if (Strings::match($className, $excludedClass)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Naming\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
Expand Down

0 comments on commit a6ce5e4

Please sign in to comment.