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 |
||
21 | class PersistentCollectionTest extends OrmTestCase |
||
22 | { |
||
23 | /** |
||
24 | * @var PersistentCollection |
||
25 | */ |
||
26 | protected $collection; |
||
27 | |||
28 | /** |
||
29 | * @var EntityManagerMock |
||
30 | */ |
||
31 | private $_emMock; |
||
32 | |||
33 | protected function setUp() |
||
41 | |||
42 | /** |
||
43 | * Set up the PersistentCollection used for collection initialization tests. |
||
44 | */ |
||
45 | public function setUpPersistentCollection() |
||
52 | |||
53 | public function testCanBePutInLazyLoadingMode() |
||
60 | |||
61 | /** |
||
62 | * Test that PersistentCollection::current() initializes the collection. |
||
63 | */ |
||
64 | public function testCurrentInitializesCollection() |
||
69 | |||
70 | /** |
||
71 | * Test that PersistentCollection::key() initializes the collection. |
||
72 | */ |
||
73 | public function testKeyInitializesCollection() |
||
78 | |||
79 | /** |
||
80 | * Test that PersistentCollection::next() initializes the collection. |
||
81 | */ |
||
82 | public function testNextInitializesCollection() |
||
87 | |||
88 | /** |
||
89 | * @group DDC-3382 |
||
90 | */ |
||
91 | public function testNonObjects() |
||
109 | |||
110 | /** |
||
111 | * @group 6110 |
||
112 | */ |
||
113 | View Code Duplication | public function testRemovingElementsAlsoRemovesKeys() |
|
123 | |||
124 | /** |
||
125 | * @group 6110 |
||
126 | */ |
||
127 | public function testClearWillAlsoClearKeys() |
||
133 | |||
134 | /** |
||
135 | * @group 6110 |
||
136 | */ |
||
137 | View Code Duplication | public function testClearWillAlsoResetKeyPositions() |
|
147 | |||
148 | /** |
||
149 | * @group 6613 |
||
150 | * @group 6614 |
||
151 | * @group 6616 |
||
152 | */ |
||
153 | View Code Duplication | public function testWillKeepNewItemsInDirtyCollectionAfterInitialization() |
|
154 | { |
||
155 | /* @var $unitOfWork UnitOfWork|\PHPUnit_Framework_MockObject_MockObject */ |
||
156 | $unitOfWork = $this->createMock(UnitOfWork::class); |
||
157 | |||
158 | $this->_emMock->setUnitOfWork($unitOfWork); |
||
|
|||
159 | |||
160 | $newElement = new \stdClass(); |
||
161 | $persistedElement = new \stdClass(); |
||
162 | |||
163 | $this->collection->add($newElement); |
||
164 | |||
165 | self::assertFalse($this->collection->isInitialized()); |
||
166 | self::assertTrue($this->collection->isDirty()); |
||
167 | |||
168 | $unitOfWork |
||
169 | ->expects(self::once()) |
||
170 | ->method('loadCollection') |
||
171 | ->with($this->collection) |
||
172 | ->willReturnCallback(function (PersistentCollection $persistentCollection) use ($persistedElement) { |
||
173 | $persistentCollection->unwrap()->add($persistedElement); |
||
174 | }); |
||
175 | |||
176 | $this->collection->initialize(); |
||
177 | |||
178 | self::assertSame([$persistedElement, $newElement], $this->collection->toArray()); |
||
179 | self::assertTrue($this->collection->isInitialized()); |
||
180 | self::assertTrue($this->collection->isDirty()); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @group 6613 |
||
185 | * @group 6614 |
||
186 | * @group 6616 |
||
187 | */ |
||
188 | public function testWillDeDuplicateNewItemsThatWerePreviouslyPersistedInDirtyCollectionAfterInitialization() |
||
226 | |||
227 | /** |
||
228 | * @group 6613 |
||
229 | * @group 6614 |
||
230 | * @group 6616 |
||
231 | */ |
||
232 | View Code Duplication | public function testWillNotMarkCollectionAsDirtyAfterInitializationIfNoElementsWereAdded() |
|
268 | } |
||
269 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.