fix nullable for xToOne annotation by default

This commit is contained in:
Tomas Votruba 2019-08-04 13:38:42 +02:00
parent 1a085ce7e1
commit f3235553dd
3 changed files with 17 additions and 53 deletions

View File

@ -14,12 +14,13 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
/**
* @var string[]
*/
private const MANY_RELATIONS_ANNOTATIONS = ['Doctrine\ORM\Mapping\OneToMany', 'Doctrine\ORM\Mapping\ManyToMany'];
private const TO_MANY_ANNOTATIONS = ['Doctrine\ORM\Mapping\OneToMany', 'Doctrine\ORM\Mapping\ManyToMany'];
/**
* Nullable by default, @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#joincolumn - "JoinColumn" and nullable=true
* @var string[]
*/
private const ONE_RELATION_ANNOTATIONS = ['Doctrine\ORM\Mapping\ManyToOne', 'Doctrine\ORM\Mapping\OneToOne'];
private const TO_ONE_ANNOTATIONS = ['Doctrine\ORM\Mapping\ManyToOne', 'Doctrine\ORM\Mapping\OneToOne'];
/**
* @var string
@ -46,20 +47,20 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
*/
public function inferProperty(Property $property): array
{
foreach (self::MANY_RELATIONS_ANNOTATIONS as $doctrineRelationAnnotation) {
foreach (self::TO_MANY_ANNOTATIONS as $doctrineRelationAnnotation) {
if (! $this->docBlockManipulator->hasTag($property, $doctrineRelationAnnotation)) {
continue;
}
return $this->processManyRelation($property, $doctrineRelationAnnotation);
return $this->processToManyRelation($property, $doctrineRelationAnnotation);
}
foreach (self::ONE_RELATION_ANNOTATIONS as $doctrineRelationAnnotation) {
foreach (self::TO_ONE_ANNOTATIONS as $doctrineRelationAnnotation) {
if (! $this->docBlockManipulator->hasTag($property, $doctrineRelationAnnotation)) {
continue;
}
return $this->processOneRelation($property, $doctrineRelationAnnotation);
return $this->processToOneRelation($property, $doctrineRelationAnnotation);
}
return [];
@ -73,7 +74,7 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
/**
* @return string[]
*/
private function processManyRelation(Property $property, string $doctrineRelationAnnotation): array
private function processToManyRelation(Property $property, string $doctrineRelationAnnotation): array
{
$types = [];
@ -90,7 +91,7 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
/**
* @return string[]
*/
private function processOneRelation(Property $property, string $doctrineRelationAnnotation): array
private function processToOneRelation(Property $property, string $doctrineRelationAnnotation): array
{
$types = [];
@ -131,7 +132,7 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
private function isNullableOneRelation(Node $node): bool
{
if (! $this->docBlockManipulator->hasTag($node, self::JOIN_COLUMN_ANNOTATION)) {
return false;
return true;
}
$joinColumnTag = $this->docBlockManipulator->getTagByName($node, self::JOIN_COLUMN_ANNOTATION);

View File

@ -7,13 +7,13 @@ use Doctrine\ORM\Mapping as ORM;
class DoctrineRelationToOne
{
/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
* @ORM\JoinColumn(name="company_id", nullable=true, referencedColumnName="id")
*/
private $company2;
@ -22,23 +22,6 @@ class DoctrineRelationToOne
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=false)
*/
private $company3;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
*/
private $company4;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @ORM\JoinColumn(nullable=true)
*/
private $company5;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @ORM\JoinColumn(nullable=false)
*/
private $company6;
}
?>
@ -52,14 +35,14 @@ use Doctrine\ORM\Mapping as ORM;
class DoctrineRelationToOne
{
/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @var \App\Company\Entity\Company|null
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity="App\Company\Entity\Company", inversedBy="userBuildings")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
* @ORM\JoinColumn(name="company_id", nullable=true, referencedColumnName="id")
* @var \App\Company\Entity\Company|null
*/
private $company2;
@ -70,26 +53,6 @@ class DoctrineRelationToOne
* @var \App\Company\Entity\Company
*/
private $company3;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @var \App\Company\Entity\Company|null
*/
private $company4;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @ORM\JoinColumn(nullable=true)
* @var \App\Company\Entity\Company|null
*/
private $company5;
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @ORM\JoinColumn(nullable=false)
* @var \App\Company\Entity\Company
*/
private $company6;
}
?>

View File

@ -12,15 +12,15 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
$this->doTestFiles([
__DIR__ . '/Fixture/constructor_param.php.inc',
__DIR__ . '/Fixture/constructor_param_with_nullable.php.inc',
// __DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
// __DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc',
__DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
__DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc',
__DIR__ . '/Fixture/constructor_assign.php.inc',
__DIR__ . '/Fixture/phpunit_setup.php.inc',
__DIR__ . '/Fixture/default_value.php.inc',
__DIR__ . '/Fixture/doctrine_column.php.inc',
__DIR__ . '/Fixture/doctrine_relation_to_many.php.inc',
__DIR__ . '/Fixture/doctrine_relation_to_one.php.inc',
__DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
// __DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
// get and set
__DIR__ . '/Fixture/complex.php.inc',
__DIR__ . '/Fixture/single_nullable_return.php.inc',