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
47 changes: 47 additions & 0 deletions packages/phpstan-static-type-mapper/src/DoctrineTypeAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\PHPStanStaticTypeMapper;

use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;

final class DoctrineTypeAnalyzer
{
public function isDoctrineCollectionWithIterableUnionType(Type $type): bool
{
if (! $type instanceof UnionType) {
return false;
}

$arrayType = null;
$hasDoctrineCollectionType = false;
foreach ($type->getTypes() as $unionedType) {
if ($this->isCollectionObjectType($unionedType)) {
$hasDoctrineCollectionType = true;
}

if ($unionedType instanceof ArrayType) {
$arrayType = $unionedType;
}
}

if (! $hasDoctrineCollectionType) {
return false;
}

return $arrayType !== null;
}

private function isCollectionObjectType(Type $type): bool
{
if (! $type instanceof TypeWithClassName) {
return false;
}

return $type->getClassName() === 'Doctrine\Common\Collections\Collection';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;

Expand All @@ -41,10 +42,19 @@ final class UnionTypeMapper implements TypeMapperInterface
*/
private $unionTypeAnalyzer;

public function __construct(PhpVersionProvider $phpVersionProvider, UnionTypeAnalyzer $unionTypeAnalyzer)
{
/**
* @var DoctrineTypeAnalyzer
*/
private $doctrineTypeAnalyzer;

public function __construct(
PhpVersionProvider $phpVersionProvider,
UnionTypeAnalyzer $unionTypeAnalyzer,
DoctrineTypeAnalyzer $doctrineTypeAnalyzer
) {
$this->phpVersionProvider = $phpVersionProvider;
$this->unionTypeAnalyzer = $unionTypeAnalyzer;
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
}

/**
Expand Down Expand Up @@ -210,16 +220,18 @@ private function matchPhpParserUnionType(UnionType $unionType): ?PhpParserUnionT

private function resolveCompatibleObjectCandidate(UnionType $unionType): ?string
{
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
return null;
}
if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($unionType)) {
return 'Doctrine\Common\Collections\Collection';
}

foreach ($unionType->getTypes() as $nestedUnionedType) {
if (! $nestedUnionedType instanceof TypeWithClassName) {
return null;
}
if (! $this->isUnionTypeWithTypeClassNameOnly($unionType)) {
return null;
}

/** @var TypeWithClassName $unionedType */
foreach ($unionType->getTypes() as $unionedType) {
/** @var TypeWithClassName $nestedUnionedType */
foreach ($unionType->getTypes() as $nestedUnionedType) {
if (! $this->areTypeWithClassNamesRelated($unionedType, $nestedUnionedType)) {
continue 2;
}
Expand Down Expand Up @@ -249,4 +261,15 @@ private function shouldSkipIterable(UnionType $unionType): bool

return $unionTypeAnalysis->hasIterable() && $unionTypeAnalysis->hasArray();
}

private function isUnionTypeWithTypeClassNameOnly(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
return false;
}
}

return true;
}
}
25 changes: 21 additions & 4 deletions rules/php-74/src/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
use Rector\VendorLocker\VendorLockResolver;
Expand All @@ -36,10 +38,19 @@ final class TypedPropertyRector extends AbstractRector
*/
private $vendorLockResolver;

public function __construct(PropertyTypeInferer $propertyTypeInferer, VendorLockResolver $vendorLockResolver)
{
/**
* @var DoctrineTypeAnalyzer
*/
private $doctrineTypeAnalyzer;

public function __construct(
PropertyTypeInferer $propertyTypeInferer,
VendorLockResolver $vendorLockResolver,
DoctrineTypeAnalyzer $doctrineTypeAnalyzer
) {
$this->propertyTypeInferer = $propertyTypeInferer;
$this->vendorLockResolver = $vendorLockResolver;
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -100,6 +111,7 @@ public function refactor(Node $node): ?Node
$varType,
PHPStanStaticTypeMapper::KIND_PROPERTY
);

if ($propertyTypeNode === null) {
return null;
}
Expand All @@ -108,15 +120,20 @@ public function refactor(Node $node): ?Node
return null;
}

$this->removeVarPhpTagValueNodeIfNotComment($node);
$this->removeVarPhpTagValueNodeIfNotComment($node, $varType);

$node->type = $propertyTypeNode;

return $node;
}

private function removeVarPhpTagValueNodeIfNotComment(Property $property): void
private function removeVarPhpTagValueNodeIfNotComment(Property $property, Type $type): void
{
// keep doctrine collection narrow type
if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($type)) {
return;
}

$propertyPhpDocInfo = $property->getAttribute(AttributeKey::PHP_DOC_INFO);
// nothing to remove
if ($propertyPhpDocInfo === null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php74\Rector\Property\TypedPropertyRector;

final class DoctrineTypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureDoctrine');
}

protected function getRectorClass(): string
{
return TypedPropertyRector::class;
}

protected function getPhpVersion(): string
{
// before union type
return '7.4';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Pehapkari\Training\Entity\TrainingTerm;

/**
* @ORM\Entity
*/
class DoctrineCollection
{
/**
* @ORM\OneToMany(targetEntity="Pehapkari\Training\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]|Collection
*/
private $trainingTerms;
}

?>
-----
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Pehapkari\Training\Entity\TrainingTerm;

/**
* @ORM\Entity
*/
class DoctrineCollection
{
/**
* @ORM\OneToMany(targetEntity="Pehapkari\Training\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]|Collection
*/
private \Doctrine\Common\Collections\Collection $trainingTerms;
}

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

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Pehapkari\Training\Entity\TrainingTerm;

/**
* @ORM\Entity
*/
class DoctrineIntersectCollection
{
/**
* @ORM\OneToMany(targetEntity="Pehapkari\Training\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]&Collection
*/
private $training;
}

?>
-----
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Pehapkari\Training\Entity\TrainingTerm;

/**
* @ORM\Entity
*/
class DoctrineIntersectCollection
{
/**
* @ORM\OneToMany(targetEntity="Pehapkari\Training\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]&Collection
*/
private \Doctrine\Common\Collections\Collection $training;
}

?>