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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rector\TypeDeclaration\PropertyTypeInferer;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
Expand All @@ -13,7 +14,17 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
/**
* @var string[]
*/
private const RELATION_ANNOTATIONS = ['Doctrine\ORM\Mapping\OneToMany', 'Doctrine\ORM\Mapping\ManyToMany'];
private const MANY_RELATIONS_ANNOTATIONS = ['Doctrine\ORM\Mapping\OneToMany', 'Doctrine\ORM\Mapping\ManyToMany'];

/**
* @var string[]
*/
private const ONE_RELATION_ANNOTATIONS = ['Doctrine\ORM\Mapping\ManyToOne', 'Doctrine\ORM\Mapping\OneToOne'];

/**
* @var string
*/
private const JOIN_COLUMN_ANNOTATION = 'Doctrine\ORM\Mapping\JoinColumn';

/**
* @var string
Expand All @@ -35,25 +46,20 @@ public function __construct(DocBlockManipulator $docBlockManipulator)
*/
public function inferProperty(Property $property): array
{
foreach (self::RELATION_ANNOTATIONS as $doctrineRelationAnnotation) {
foreach (self::MANY_RELATIONS_ANNOTATIONS as $doctrineRelationAnnotation) {
if (! $this->docBlockManipulator->hasTag($property, $doctrineRelationAnnotation)) {
continue;
}

$relationTag = $this->docBlockManipulator->getTagByName($property, $doctrineRelationAnnotation);

$types = [];
return $this->processManyRelation($property, $doctrineRelationAnnotation);
}

if ($relationTag->value instanceof GenericTagValueNode) {
$resolveTargetType = $this->resolveTargetEntity($relationTag->value);
if ($resolveTargetType) {
$types[] = $resolveTargetType . '[]';
}
foreach (self::ONE_RELATION_ANNOTATIONS as $doctrineRelationAnnotation) {
if (! $this->docBlockManipulator->hasTag($property, $doctrineRelationAnnotation)) {
continue;
}

$types[] = self::COLLECTION_TYPE;

return $types;
return $this->processOneRelation($property, $doctrineRelationAnnotation);
}

return [];
Expand All @@ -64,10 +70,76 @@ public function getPriority(): int
return 900;
}

/**
* @return string[]
*/
private function processManyRelation(Property $property, string $doctrineRelationAnnotation): array
{
$types = [];

$relationType = $this->resolveRelationType($property, $doctrineRelationAnnotation);
if ($relationType) {
$types[] = $relationType . '[]';
}

$types[] = self::COLLECTION_TYPE;

return $types;
}

/**
* @return string[]
*/
private function processOneRelation(Property $property, string $doctrineRelationAnnotation): array
{
$types = [];

$relationType = $this->resolveRelationType($property, $doctrineRelationAnnotation);

if ($relationType) {
$types[] = $relationType;
}

if ($this->isNullableOneRelation($property)) {
$types[] = 'null';
}

return $types;
}

private function resolveTargetEntity(GenericTagValueNode $genericTagValueNode): ?string
{
$match = Strings::match($genericTagValueNode->value, '#targetEntity=\"(?<targetEntity>.*?)\"#');

return $match['targetEntity'] ?? null;
}

private function resolveRelationType(Property $property, string $doctrineRelationAnnotation): ?string
{
$relationTag = $this->docBlockManipulator->getTagByName($property, $doctrineRelationAnnotation);

if ($relationTag->value instanceof GenericTagValueNode) {
$resolveTargetType = $this->resolveTargetEntity($relationTag->value);
if ($resolveTargetType) {
return $resolveTargetType;
}
}

return null;
}

private function isNullableOneRelation(Node $node): bool
{
if (! $this->docBlockManipulator->hasTag($node, self::JOIN_COLUMN_ANNOTATION)) {
return false;
}

$joinColumnTag = $this->docBlockManipulator->getTagByName($node, self::JOIN_COLUMN_ANNOTATION);

if ($joinColumnTag->value instanceof GenericTagValueNode) {
return (bool) Strings::match($joinColumnTag->value->value, '#nullable=true#');
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ class DoctrineRelation
* @ORM\ManyToMany(targetEntity="App\Car")
*/
private $cars;

/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
*/
private $company;

/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
*/
private $company2;
}

?>
Expand All @@ -38,6 +49,19 @@ class DoctrineRelation
* @var \App\Car[]|\Doctrine\Common\Collections\Collection
*/
private $cars;

/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
* @var \App\Company\Entity\Company|null
*/
private $company;

/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @var \App\Company\Entity\Company
*/
private $company2;
}

?>