Skip to content

Commit

Permalink
cover name at JoinColumn removal
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 19, 2019
1 parent e17dcbe commit d15a1b9
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ private function refactorToManyPropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo

private function refactorToOnePropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo): void
{
/** @var JoinColumnTagValueNode $joinColumnTagValueNode */
$joinColumnTagValueNode = $propertyPhpDocInfo->getByType(JoinColumnTagValueNode::class);

if ($joinColumnTagValueNode) {
$joinColumnTagValueNode->changeName('');
$joinColumnTagValueNode->changeNullable(true);
$joinColumnTagValueNode->changeReferencedColumnName('uuid');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/to_one.php.inc'];
yield [__DIR__ . '/Fixture/to_many.php.inc'];
yield [__DIR__ . '/Fixture/many_to_many_with_extra_name.php.inc'];
// skip
yield [__DIR__ . '/Fixture/skip_already_added.php.inc'];
yield [__DIR__ . '/Fixture/skip_to_many_without_target_entity_uuid.php.inc'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
* @ORM\Entity
* @ORM\Table(name="wohoo")
*/
class ManyToManyWithExtraName
{
/**
* @ORM\ManyToOne(targetEntity="Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
* @Serializer\Type("Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
*/
private $itemRole;
}

/**
* @ORM\Entity
*/
class BarEntity
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

private $uuid;
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
* @ORM\Entity
* @ORM\Table(name="wohoo")
*/
class ManyToManyWithExtraName
{
/**
* @ORM\ManyToOne(targetEntity="Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
* @Serializer\Type("Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
*/
private $itemRole;
/**
* @ORM\ManyToOne(targetEntity="Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
* @ORM\JoinColumn(referencedColumnName="uuid", nullable=true)
* @Serializer\Type("Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\BarEntity")
*/
private $itemRoleUuid;
}

/**
* @ORM\Entity
*/
class BarEntity
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

private $uuid;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Resolver\NameResolver;
use Rector\Testing\PHPUnit\PHPUnitEnvironment;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraint;
use Throwable;

final class NodeAnnotationReader
{
Expand Down Expand Up @@ -71,6 +73,9 @@ public function readClassAnnotation(Class_ $class, string $annotationClassName):
public function readPropertyAnnotation(Property $property, string $annotationClassName)
{
$propertyReflection = $this->createPropertyReflectionFromPropertyNode($property);
if ($propertyReflection === null) {
return null;
}

/** @var Annotation|null $propertyAnnotation */
$propertyAnnotation = $this->annotationReader->getPropertyAnnotation($propertyReflection, $annotationClassName);
Expand All @@ -81,7 +86,7 @@ public function readPropertyAnnotation(Property $property, string $annotationCla
return $propertyAnnotation;
}

private function createPropertyReflectionFromPropertyNode(Property $property): ReflectionProperty
private function createPropertyReflectionFromPropertyNode(Property $property): ?ReflectionProperty
{
/** @var string $propertyName */
$propertyName = $this->nameResolver->getName($property);
Expand All @@ -97,7 +102,15 @@ private function createPropertyReflectionFromPropertyNode(Property $property): R
));
}

return new ReflectionProperty($className, $propertyName);
try {
return new ReflectionProperty($className, $propertyName);
} catch (Throwable $throwable) {
if (PHPUnitEnvironment::isPHPUnitRun()) {
return null;
}

throw new $throwable();
}
}

private function createClassReflectionFromNode(Class_ $class): ReflectionClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __toString(): string
$contentItems['nullable'] = sprintf('nullable=%s', $this->nullable ? 'true' : 'false');
}

if ($this->name !== null) {
if ($this->name) {
$contentItems['name'] = sprintf('name="%s"', $this->name);
}

Expand Down Expand Up @@ -123,4 +123,9 @@ public function isNullable(): ?bool
{
return $this->nullable;
}

public function changeName(string $newName): void
{
$this->name = $newName;
}
}
8 changes: 6 additions & 2 deletions packages/Symfony/src/PhpDocParser/SymfonyPhpDocTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ private function createSymfonyRouteTagValueNode(
private function createSerializerTypeTagValueNode(
Property $property,
string $annotationContent
): SerializerTypeTagValueNode {
/** @var Type $typeAnnotation */
): ?SerializerTypeTagValueNode {
/** @var Type|null $typeAnnotation */
$typeAnnotation = $this->nodeAnnotationReader->readPropertyAnnotation(
$property,
SerializerTypeTagValueNode::CLASS_NAME
);

if ($typeAnnotation === null) {
return null;
}

return new SerializerTypeTagValueNode($typeAnnotation->name, $annotationContent);
}

Expand Down

0 comments on commit d15a1b9

Please sign in to comment.