Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Naming] Skip Doctrine collection with @var Collection<int, Checkbox> on RenamePropertyToMatchTypeRector #3209

Merged
merged 3 commits into from
Dec 16, 2022
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,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