These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\ORM\Functional\Ticket; |
||
4 | |||
5 | use Doctrine\ORM\Proxy\Proxy; |
||
6 | |||
7 | /** |
||
8 | * @group DDC-1163 |
||
9 | */ |
||
10 | class DDC1163Test extends \Doctrine\Tests\OrmFunctionalTestCase |
||
11 | { |
||
12 | protected function setUp() |
||
13 | { |
||
14 | parent::setUp(); |
||
15 | //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
||
0 ignored issues
–
show
|
|||
16 | $this->_schemaTool->createSchema( |
||
17 | [ |
||
18 | $this->_em->getClassMetadata(DDC1163Product::class), |
||
19 | $this->_em->getClassMetadata(DDC1163SpecialProduct::class), |
||
20 | $this->_em->getClassMetadata(DDC1163ProxyHolder::class), |
||
21 | $this->_em->getClassMetadata(DDC1163Tag::class), |
||
22 | ] |
||
23 | ); |
||
24 | } |
||
25 | |||
26 | public function testIssue() |
||
27 | { |
||
28 | $this->createSpecialProductAndProxyHolderReferencingIt(); |
||
29 | $this->_em->clear(); |
||
30 | |||
31 | $this->createProxyForSpecialProduct(); |
||
32 | |||
33 | $this->setPropertyAndAssignTagToSpecialProduct(); |
||
34 | |||
35 | // fails |
||
36 | $this->_em->flush(); |
||
37 | } |
||
38 | |||
39 | private function createSpecialProductAndProxyHolderReferencingIt() |
||
40 | { |
||
41 | $specialProduct = new DDC1163SpecialProduct(); |
||
42 | $this->_em->persist($specialProduct); |
||
43 | |||
44 | $proxyHolder = new DDC1163ProxyHolder(); |
||
45 | $this->_em->persist($proxyHolder); |
||
46 | |||
47 | $proxyHolder->setSpecialProduct($specialProduct); |
||
48 | |||
49 | $this->_em->flush(); |
||
50 | |||
51 | $this->productId = $specialProduct->getId(); |
||
52 | $this->proxyHolderId = $proxyHolder->getId(); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * We want Doctrine to instantiate a lazy-load proxy for the previously created |
||
57 | * 'SpecialProduct' and register it. |
||
58 | * |
||
59 | * When Doctrine loads the 'ProxyHolder', it will do just that because the 'ProxyHolder' |
||
60 | * references the 'SpecialProduct'. |
||
61 | */ |
||
62 | private function createProxyForSpecialProduct() |
||
63 | { |
||
64 | /* @var $proxyHolder DDC1163ProxyHolder */ |
||
65 | $proxyHolder = $this->_em->find(DDC1163ProxyHolder::class, $this->proxyHolderId); |
||
66 | |||
67 | $this->assertInstanceOf(DDC1163SpecialProduct::class, $proxyHolder->getSpecialProduct()); |
||
68 | } |
||
69 | |||
70 | private function setPropertyAndAssignTagToSpecialProduct() |
||
71 | { |
||
72 | /* @var $specialProduct DDC1163SpecialProduct */ |
||
73 | $specialProduct = $this->_em->find(DDC1163SpecialProduct::class, $this->productId); |
||
74 | |||
75 | $this->assertInstanceOf(DDC1163SpecialProduct::class, $specialProduct); |
||
76 | $this->assertInstanceOf(Proxy::class, $specialProduct); |
||
77 | |||
78 | $specialProduct->setSubclassProperty('foobar'); |
||
79 | |||
80 | // this screams violation of law of demeter ;) |
||
81 | $this->assertEquals( |
||
82 | DDC1163SpecialProduct::class, |
||
83 | $this->_em->getUnitOfWork()->getEntityPersister(get_class($specialProduct))->getClassMetadata()->name |
||
84 | ); |
||
85 | |||
86 | $tag = new DDC1163Tag('Foo'); |
||
87 | $this->_em->persist($tag); |
||
88 | $tag->setProduct($specialProduct); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @Entity |
||
94 | */ |
||
95 | class DDC1163ProxyHolder |
||
96 | { |
||
97 | |||
98 | /** |
||
99 | * @var int |
||
100 | * @Column(name="id", type="integer") |
||
101 | * @Id |
||
102 | * @GeneratedValue(strategy="AUTO") |
||
103 | */ |
||
104 | private $id; |
||
105 | /** |
||
106 | * @var SpecialProduct |
||
107 | * @OneToOne(targetEntity="DDC1163SpecialProduct") |
||
108 | */ |
||
109 | private $specialProduct; |
||
110 | |||
111 | public function getId() |
||
112 | { |
||
113 | return $this->id; |
||
114 | } |
||
115 | |||
116 | public function setSpecialProduct(DDC1163SpecialProduct $specialProduct) |
||
117 | { |
||
118 | $this->specialProduct = $specialProduct; |
||
119 | } |
||
120 | |||
121 | public function getSpecialProduct() |
||
122 | { |
||
123 | return $this->specialProduct; |
||
124 | } |
||
125 | |||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @Entity |
||
130 | * @InheritanceType("JOINED") |
||
131 | * @DiscriminatorColumn(name="type", type="string") |
||
132 | * @DiscriminatorMap({"special" = "DDC1163SpecialProduct"}) |
||
133 | */ |
||
134 | abstract class DDC1163Product |
||
135 | { |
||
136 | |||
137 | /** |
||
138 | * @var int |
||
139 | * @Column(name="id", type="integer") |
||
140 | * @Id |
||
141 | * @GeneratedValue(strategy="AUTO") |
||
142 | */ |
||
143 | protected $id; |
||
144 | |||
145 | public function getId() |
||
146 | { |
||
147 | return $this->id; |
||
148 | } |
||
149 | |||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @Entity |
||
154 | */ |
||
155 | class DDC1163SpecialProduct extends DDC1163Product |
||
156 | { |
||
157 | |||
158 | /** |
||
159 | * @var string |
||
160 | * @Column(name="subclass_property", type="string", nullable=true) |
||
161 | */ |
||
162 | private $subclassProperty; |
||
163 | |||
164 | /** |
||
165 | * @param string $value |
||
166 | */ |
||
167 | public function setSubclassProperty($value) |
||
168 | { |
||
169 | $this->subclassProperty = $value; |
||
170 | } |
||
171 | |||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @Entity |
||
176 | */ |
||
177 | class DDC1163Tag |
||
178 | { |
||
179 | |||
180 | /** |
||
181 | * @var int |
||
182 | * @Column(name="id", type="integer") |
||
183 | * @Id |
||
184 | * @GeneratedValue(strategy="AUTO") |
||
185 | */ |
||
186 | private $id; |
||
187 | /** |
||
188 | * @var string |
||
189 | * @Column(name="name", type="string") |
||
190 | */ |
||
191 | private $name; |
||
192 | /** |
||
193 | * @var Product |
||
194 | * @ManyToOne(targetEntity="DDC1163Product", inversedBy="tags") |
||
195 | * @JoinColumns({ |
||
196 | * @JoinColumn(name="product_id", referencedColumnName="id") |
||
197 | * }) |
||
198 | */ |
||
199 | private $product; |
||
200 | |||
201 | /** |
||
202 | * @param string $name |
||
203 | */ |
||
204 | public function __construct($name) |
||
205 | { |
||
206 | $this->name = $name; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param Product $product |
||
211 | */ |
||
212 | public function setProduct(DDC1163Product $product) |
||
213 | { |
||
214 | $this->product = $product; |
||
215 | } |
||
216 | |||
217 | } |
||
218 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.