Skip to content

Commit 04315c8

Browse files
curry684ondrejmirtes
authored andcommitted
Add support for Doctrine generated fields
Closes #378
1 parent 2d1ddae commit 04315c8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Rules/Doctrine/ORM/PropertiesExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function isAlwaysWritten(PropertyReflection $property, string $propertyNa
4444
return false;
4545
}
4646

47+
$mapping = $metadata->getFieldMapping($propertyName);
48+
if (array_key_exists('generated', $mapping) && $mapping['generated'] !== ClassMetadataInfo::GENERATED_NEVER) {
49+
return true;
50+
}
51+
4752
if ($metadata->isReadOnly && !$declaringClass->hasConstructor()) {
4853
return true;
4954
}

tests/Rules/DeadCode/data/unused-private-property.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,57 @@ class ReadOnlyEntityWithConstructor
5555
public function __construct()
5656
{
5757
}
58+
}
59+
60+
/**
61+
* @ORM\Entity
62+
*/
63+
class EntityWithGeneratedField
64+
{
65+
/**
66+
* @ORM\Id
67+
* @ORM\Column
68+
*/
69+
public int $id;
5870

71+
/**
72+
* @ORM\Column(type="int", insertable=false, updatable=false, generated="ALWAYS",
73+
* columnDefinition="int GENERATED ALWAYS AS (1 + 2)")
74+
*/
75+
private int $generated;
76+
77+
public function __construct()
78+
{
79+
}
5980
}
81+
82+
/**
83+
* @ORM\Entity
84+
*/
85+
class EntityWithGeneratedFieldWithGetter
86+
{
87+
/**
88+
* @ORM\Id
89+
* @ORM\Column
90+
*/
91+
public int $id;
92+
93+
/**
94+
* @ORM\Column(type="int", insertable=false, updatable=false, generated="ALWAYS",
95+
* columnDefinition="int GENERATED ALWAYS AS (1 + 2)")
96+
*/
97+
private int $generated;
98+
99+
public function __construct()
100+
{
101+
}
102+
103+
/**
104+
* @return int
105+
*/
106+
public function getGenerated(): int
107+
{
108+
return $this->generated;
109+
}
110+
}
111+

0 commit comments

Comments
 (0)