Skip to content

Commit

Permalink
[Naming] Skip Doctrine collection with @var Collection<int, Checkbox>…
Browse files Browse the repository at this point in the history
… on RenamePropertyToMatchTypeRector (#3209)

* skip

* Fixed 🎉

* prefer object type
  • Loading branch information
samsonasik committed Dec 16, 2022
1 parent 5a4111e commit f2e4da5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

final class SkipDoctrineCollectionPrivateProperty
{
/**
* @var Collection<int, Checkbox>
*/
private Collection $checkboxes;

public function __construct()
{
$this->checkboxes = new ArrayCollection();
}

public function getCheckboxes(): Collection
{
return $this->checkboxes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Rector\Naming\ExpectedNameResolver;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\NodeManipulator\PropertyManipulator;
Expand Down Expand Up @@ -61,15 +63,26 @@ public function resolve(Property $property, ClassLike $classLike): ?string

private function resolveExpectedName(Property $property): ?ExpectedName
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
$isPhpDocInfo = $phpDocInfo instanceof PhpDocInfo;

// property type first
if ($property->type instanceof \PhpParser\Node) {
if ($property->type instanceof Node) {
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
return $this->propertyNaming->getExpectedNameFromType($propertyType);
// not has docblock, use property type
if (! $isPhpDocInfo) {
return $this->propertyNaming->getExpectedNameFromType($propertyType);
}

// @var type is ObjectType, use property type
$varType = $phpDocInfo->getVarType();
if ($varType instanceof ObjectType) {
return $this->propertyNaming->getExpectedNameFromType($propertyType);
}
}

// fallback to docblock
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
if ($phpDocInfo instanceof PhpDocInfo) {
if ($isPhpDocInfo) {
return $this->propertyNaming->getExpectedNameFromType($phpDocInfo->getVarType());
}

Expand Down

0 comments on commit f2e4da5

Please sign in to comment.