Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
18 | class PostLoadEventTest extends OrmFunctionalTestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var integer |
||
22 | */ |
||
23 | private $userId; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | protected function setUp() |
||
36 | |||
37 | public function testLoadedEntityUsingFindShouldTriggerEvent() |
||
53 | |||
54 | View Code Duplication | public function testLoadedEntityUsingQueryShouldTriggerEvent() |
|
55 | { |
||
56 | $mockListener = $this->createMock(PostLoadListener::class); |
||
57 | |||
58 | // CmsUser and CmsAddres, because it's a ToOne inverse side on CmsUser |
||
59 | $mockListener |
||
60 | ->expects($this->exactly(2)) |
||
61 | ->method('postLoad') |
||
62 | ->will($this->returnValue(true)); |
||
63 | |||
64 | $eventManager = $this->_em->getEventManager(); |
||
65 | |||
66 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
67 | |||
68 | $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :id'); |
||
69 | |||
70 | $query->setParameter('id', $this->userId); |
||
71 | $query->getResult(); |
||
72 | } |
||
73 | |||
74 | View Code Duplication | public function testLoadedAssociationToOneShouldTriggerEvent() |
|
75 | { |
||
76 | $mockListener = $this->createMock(PostLoadListener::class); |
||
77 | |||
78 | // CmsUser (root), CmsAddress (ToOne inverse side), CmsEmail (joined association) |
||
79 | $mockListener |
||
80 | ->expects($this->exactly(3)) |
||
81 | ->method('postLoad') |
||
82 | ->will($this->returnValue(true)); |
||
83 | |||
84 | $eventManager = $this->_em->getEventManager(); |
||
85 | |||
86 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
87 | |||
88 | $query = $this->_em->createQuery('SELECT u, e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e WHERE u.id = :id'); |
||
89 | |||
90 | $query->setParameter('id', $this->userId); |
||
91 | $query->getResult(); |
||
92 | } |
||
93 | |||
94 | View Code Duplication | public function testLoadedAssociationToManyShouldTriggerEvent() |
|
95 | { |
||
96 | $mockListener = $this->createMock(PostLoadListener::class); |
||
97 | |||
98 | // CmsUser (root), CmsAddress (ToOne inverse side), 2 CmsPhonenumber (joined association) |
||
99 | $mockListener |
||
100 | ->expects($this->exactly(4)) |
||
101 | ->method('postLoad') |
||
102 | ->will($this->returnValue(true)); |
||
103 | |||
104 | $eventManager = $this->_em->getEventManager(); |
||
105 | |||
106 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
107 | |||
108 | $query = $this->_em->createQuery('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p WHERE u.id = :id'); |
||
109 | |||
110 | $query->setParameter('id', $this->userId); |
||
111 | $query->getResult(); |
||
112 | } |
||
113 | |||
114 | public function testLoadedProxyEntityShouldTriggerEvent() |
||
144 | |||
145 | View Code Duplication | public function testLoadedProxyPartialShouldTriggerEvent() |
|
146 | { |
||
147 | $eventManager = $this->_em->getEventManager(); |
||
148 | |||
149 | // Should not be invoked during getReference call |
||
150 | $mockListener = $this->createMock(PostLoadListener::class); |
||
151 | |||
152 | // CmsUser (partially loaded), CmsAddress (inverse ToOne), 2 CmsPhonenumber |
||
153 | $mockListener |
||
154 | ->expects($this->exactly(4)) |
||
155 | ->method('postLoad') |
||
156 | ->will($this->returnValue(true)); |
||
157 | |||
158 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
159 | |||
160 | $query = $this->_em->createQuery('SELECT PARTIAL u.{id, name}, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p WHERE u.id = :id'); |
||
161 | |||
162 | $query->setParameter('id', $this->userId); |
||
163 | $query->getResult(); |
||
164 | } |
||
165 | |||
166 | View Code Duplication | public function testLoadedProxyAssociationToOneShouldTriggerEvent() |
|
167 | { |
||
168 | $user = $this->_em->find(CmsUser::class, $this->userId); |
||
169 | |||
170 | $mockListener = $this->createMock(PostLoadListener::class); |
||
171 | |||
172 | // CmsEmail (proxy) |
||
173 | $mockListener |
||
174 | ->expects($this->exactly(1)) |
||
175 | ->method('postLoad') |
||
176 | ->will($this->returnValue(true)); |
||
177 | |||
178 | $eventManager = $this->_em->getEventManager(); |
||
179 | |||
180 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
181 | |||
182 | $emailProxy = $user->getEmail(); |
||
183 | |||
184 | $emailProxy->getEmail(); |
||
185 | } |
||
186 | |||
187 | View Code Duplication | public function testLoadedProxyAssociationToManyShouldTriggerEvent() |
|
188 | { |
||
189 | $user = $this->_em->find(CmsUser::class, $this->userId); |
||
190 | |||
191 | $mockListener = $this->createMock(PostLoadListener::class); |
||
192 | |||
193 | // 2 CmsPhonenumber (proxy) |
||
|
|||
194 | $mockListener |
||
195 | ->expects($this->exactly(2)) |
||
196 | ->method('postLoad') |
||
197 | ->will($this->returnValue(true)); |
||
198 | |||
199 | $eventManager = $this->_em->getEventManager(); |
||
200 | |||
201 | $eventManager->addEventListener([Events::postLoad], $mockListener); |
||
202 | |||
203 | $phonenumbersCol = $user->getPhonenumbers(); |
||
204 | |||
205 | $phonenumbersCol->first(); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @group DDC-3005 |
||
210 | */ |
||
211 | public function testAssociationsArePopulatedWhenEventIsFired() |
||
224 | |||
225 | /** |
||
226 | * @group DDC-3005 |
||
227 | */ |
||
228 | public function testEventRaisedCorrectTimesWhenOtherEntityLoadedInEventHandler() |
||
238 | |||
239 | private function loadFixture() |
||
274 | } |
||
275 | |||
276 | class PostLoadListener |
||
277 | { |
||
278 | public function postLoad(LifecycleEventArgs $event) |
||
279 | { |
||
280 | // Expected to be mocked out |
||
327 |
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.