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 Serializer extends atoum |
||
19 | { |
||
20 | /** |
||
21 | * testJsonEncode |
||
22 | * |
||
23 | * @access public |
||
24 | * @return void |
||
25 | */ |
||
26 | public function testJsonEncode() |
||
27 | { |
||
28 | $this->createNewInstance(); |
||
29 | |||
30 | $this |
||
31 | ->given($cart = $this->createCart()) |
||
32 | ->then |
||
33 | ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
34 | ->isIdenticalTo([ |
||
35 | '@id' => '/v1/carts/8', |
||
36 | 'status' => 'payed', |
||
37 | "clientPhoneNumber" => '+33 1 23 45 67 89', |
||
38 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
39 | 'cart_items' => [], |
||
40 | 'order' => null, |
||
41 | ]) |
||
42 | |||
43 | // reverse the serialization |
||
44 | ->then |
||
45 | ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
46 | ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart') |
||
47 | ->string($cart->getId()) |
||
48 | ->isEqualTo('/v1/carts/8') |
||
49 | ->string($cart->getStatus()) |
||
50 | ->isEqualTo('payed') |
||
51 | ->datetime($cart->getCreatedAt()) |
||
52 | ->isEqualTo(new \DateTime('2015-09-20T12:08:00')) |
||
53 | ->array($cart->getCartItemList()) |
||
54 | ->isEmpty() |
||
55 | |||
56 | ; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * testJsonEncodeRelation |
||
61 | * |
||
62 | * @access public |
||
63 | * @return void |
||
64 | */ |
||
65 | public function testJsonEncodeRelationWithLink() |
||
66 | { |
||
67 | $this->createNewInstance(); |
||
68 | |||
69 | $this |
||
70 | ->given($cart = $this->createCart()) |
||
71 | ->and($cartItem = $this->createKnownCartItem()) |
||
72 | ->and($cart->addCartItemList($cartItem)) |
||
73 | |||
74 | ->then |
||
75 | ->array($data = $this->testedInstance->serialize( |
||
76 | $cart, |
||
77 | 'Mapado\RestClientSdk\Tests\Model\Cart', |
||
78 | [ 'serializeRelations' => ['cart_items'] ] |
||
79 | )) |
||
80 | ->isIdenticalTo([ |
||
81 | '@id' => '/v1/carts/8', |
||
82 | 'status' => 'payed', |
||
83 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
84 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
85 | 'cart_items' => [ |
||
86 | [ |
||
87 | '@id' => '/v1/cart_items/16', |
||
88 | 'amount' => 1, |
||
89 | 'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
||
90 | 'data' => [ |
||
91 | 'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
||
92 | 'who' => 'Jane', |
||
93 | ], |
||
94 | 'cart' => '/v1/carts/8', |
||
95 | 'product' => '/v1/products/10', |
||
96 | 'cartItemDetailList' => [], |
||
97 | ], |
||
98 | ], |
||
99 | 'order' => null, |
||
100 | ]) |
||
101 | |||
102 | ->then |
||
103 | ->array($data = $this->testedInstance->serialize( |
||
104 | $cart, |
||
105 | 'Mapado\RestClientSdk\Tests\Model\Cart', |
||
106 | [ 'serializeRelations' => ['cart_items'] ] |
||
107 | )) |
||
108 | ->isIdenticalTo([ |
||
109 | '@id' => '/v1/carts/8', |
||
110 | 'status' => 'payed', |
||
111 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
112 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
113 | 'cart_items' => [ |
||
114 | [ |
||
115 | '@id' => '/v1/cart_items/16', |
||
116 | 'amount' => 1, |
||
117 | 'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
||
118 | 'data' => [ |
||
119 | 'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
||
120 | 'who' => 'Jane', |
||
121 | ], |
||
122 | 'cart' => '/v1/carts/8', |
||
123 | 'product' => '/v1/products/10', |
||
124 | 'cartItemDetailList' => [], |
||
125 | ], |
||
126 | ], |
||
127 | 'order' => null, |
||
128 | ]) |
||
129 | |||
130 | // reverse the serialization |
||
131 | ->then |
||
132 | ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
133 | ->array($cart->getCartItemList()) |
||
134 | ->size->isEqualTo(1) |
||
135 | ->object($cartItem = current($cart->getCartItemList())) |
||
136 | ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
||
137 | ->string($cartItem->getId()) |
||
138 | ->isEqualTo('/v1/cart_items/16') |
||
139 | ; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * testJsonEncodeRelationWithoutLink |
||
144 | * |
||
145 | * @access public |
||
146 | * @return void |
||
147 | */ |
||
148 | public function testJsonEncodeRelationWithoutLink() |
||
149 | { |
||
150 | $this->createNewInstance(); |
||
151 | |||
152 | $this |
||
153 | ->given($cart = $this->createCart()) |
||
154 | ->and($cartItem = $this->createNewCartItem()) |
||
155 | ->and($cart->addCartItemList($cartItem)) |
||
156 | ->then |
||
157 | ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
158 | ->isIdenticalTo([ |
||
159 | '@id' => '/v1/carts/8', |
||
160 | 'status' => 'payed', |
||
161 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
162 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
163 | 'cart_items' => [ |
||
164 | [ |
||
165 | 'amount' => 2, |
||
166 | 'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
||
167 | 'data' => [ |
||
168 | 'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
||
169 | 'who' => 'John', |
||
170 | ], |
||
171 | 'product' => '/v1/products/10', |
||
172 | 'cartItemDetailList' => [], |
||
173 | ], |
||
174 | ], |
||
175 | 'order' => null, |
||
176 | ]) |
||
177 | |||
178 | // reverse the serialization |
||
179 | ->then |
||
180 | ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
181 | ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity |
||
182 | ->isEmpty() |
||
183 | ; |
||
184 | } |
||
185 | |||
186 | View Code Duplication | public function testSerializeThreeLevel() |
|
|
|||
187 | { |
||
188 | $this->createNewInstance(); |
||
189 | |||
190 | $this |
||
191 | ->given($cart = $this->createNewCart()) |
||
192 | ->and($cartItem = $this->createNewCartItem()) |
||
193 | ->and($cart->addCartItemList($cartItem)) |
||
194 | ->then |
||
195 | ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
196 | ->isIdenticalTo([ |
||
197 | 'status' => 'payed', |
||
198 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
199 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
200 | 'cart_items' => [ |
||
201 | [ |
||
202 | 'amount' => 2, |
||
203 | 'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
||
204 | 'data' => [ |
||
205 | 'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
||
206 | 'who' => 'John', |
||
207 | ], |
||
208 | 'product' => '/v1/products/10', |
||
209 | 'cartItemDetailList' => [], |
||
210 | ], |
||
211 | ], |
||
212 | 'order' => null, |
||
213 | ]) |
||
214 | ; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * testJsonEncodeRelationWithoutLinkMultipleLevel |
||
219 | * |
||
220 | * @access public |
||
221 | * @return void |
||
222 | */ |
||
223 | public function testJsonEncodeRelationWithoutLinkMultipleLevel() |
||
224 | { |
||
225 | $this->createNewInstance(); |
||
226 | $this |
||
227 | ->given($cart = $this->createCart()) |
||
228 | ->and($cartItem = $this->createNewCartItem(false)) |
||
229 | ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail())) |
||
230 | ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail())) |
||
231 | ->if($cart->addCartItemList($cartItem)) |
||
232 | ->then |
||
233 | ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
234 | ->isIdenticalTo([ |
||
235 | '@id' => '/v1/carts/8', |
||
236 | 'status' => 'payed', |
||
237 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
238 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
239 | 'cart_items' => [ |
||
240 | [ |
||
241 | 'amount' => 2, |
||
242 | 'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
||
243 | 'data' => [ |
||
244 | 'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
||
245 | 'who' => 'John', |
||
246 | ], |
||
247 | 'cartItemDetailList' => [ |
||
248 | [ 'name' => 'Bill' ], |
||
249 | [ 'name' => 'Bill', ], |
||
250 | ], |
||
251 | ], |
||
252 | ], |
||
253 | 'order' => null, |
||
254 | ]) |
||
255 | ; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * testJsonEncodeMixRelations |
||
260 | * |
||
261 | * @access public |
||
262 | * @return void |
||
263 | */ |
||
264 | public function testJsonEncodeMixRelations() |
||
265 | { |
||
266 | $this->createNewInstance(); |
||
267 | |||
268 | $this |
||
269 | ->given($cart = $this->createCart()) |
||
270 | ->and($cartItem = $this->createNewCartItem()) |
||
271 | ->and($knownedCartItem = $this->createKnownCartItem()) |
||
272 | ->if($cart->addCartItemList($knownedCartItem)) |
||
273 | ->and($cart->addCartItemList($cartItem)) |
||
274 | ->then |
||
275 | ->array($data = $this->testedInstance->serialize( |
||
276 | $cart, |
||
277 | 'Mapado\RestClientSdk\Tests\Model\Cart', |
||
278 | [ 'serializeRelations' => ['cart_items'] ] |
||
279 | )) |
||
280 | ->isIdenticalTo([ |
||
281 | '@id' => '/v1/carts/8', |
||
282 | 'status' => 'payed', |
||
283 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
284 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
285 | 'cart_items' => [ |
||
286 | [ |
||
287 | '@id' => '/v1/cart_items/16', |
||
288 | 'amount' => 1, |
||
289 | 'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
||
290 | 'data' => [ |
||
291 | 'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
||
292 | 'who' => 'Jane', |
||
293 | ], |
||
294 | 'cart' => '/v1/carts/8', |
||
295 | 'product' => '/v1/products/10', |
||
296 | 'cartItemDetailList' => [], |
||
297 | ], |
||
298 | [ |
||
299 | 'amount' => 2, |
||
300 | 'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
||
301 | 'data' => [ |
||
302 | 'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
||
303 | 'who' => 'John', |
||
304 | ], |
||
305 | 'product' => '/v1/products/10', |
||
306 | 'cartItemDetailList' => [], |
||
307 | ], |
||
308 | ], |
||
309 | 'order' => null, |
||
310 | ]) |
||
311 | |||
312 | // reverse the serialization |
||
313 | ->then |
||
314 | ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
315 | ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity |
||
316 | ->size->isEqualTo(1) |
||
317 | ->object($cartItem = current($cart->getCartItemList())) |
||
318 | ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
||
319 | ->string($cartItem->getId()) |
||
320 | ->isEqualTo('/v1/cart_items/16') |
||
321 | ; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * testNotAllowedSerialization |
||
326 | * |
||
327 | * @access public |
||
328 | * @return void |
||
329 | */ |
||
330 | public function testNotAllowedSerialization() |
||
347 | |||
348 | /** |
||
349 | * testMultipleLevelSerialization |
||
350 | * |
||
351 | * @access public |
||
352 | * @return void |
||
353 | */ |
||
354 | View Code Duplication | public function testMultipleLevelSerialization() |
|
355 | { |
||
356 | $this->createNewInstance(); |
||
357 | $this |
||
358 | ->given($cart = $this->createNewCart()) |
||
359 | ->and($cartItem = $this->createNewCartItem()) |
||
360 | ->and($cartItem->setCart($cart)) |
||
361 | ->then |
||
362 | ->array($this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
||
363 | ->isIdenticalTo([ |
||
364 | 'status' => 'payed', |
||
365 | 'clientPhoneNumber' => '+33 1 23 45 67 89', |
||
366 | 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
||
367 | 'cart_items' => [ |
||
368 | [ |
||
369 | 'amount' => 2, |
||
370 | 'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
||
371 | 'data' => [ |
||
372 | 'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
||
373 | 'who' => 'John', |
||
374 | ], |
||
375 | 'product' => '/v1/products/10', |
||
376 | 'cartItemDetailList' => [], |
||
377 | ], |
||
378 | ], |
||
379 | 'order' => null, |
||
380 | ]) |
||
381 | |||
382 | ; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * testLinkedUnserialize |
||
387 | * |
||
388 | * @access public |
||
389 | * @return void |
||
390 | */ |
||
391 | public function testLinkedUnserialize() |
||
469 | |||
470 | public function testSerializeNullValues() |
||
471 | { |
||
472 | $this->createNewInstance(); |
||
473 | $this |
||
474 | ->given($cart = $this->createNewCart()) |
||
475 | ->and($cart->setStatus(null)) |
||
476 | ->and($cart->setOrder(null)) |
||
488 | |||
489 | public function testSerializingAttributeNameDiffThanPropertyName() |
||
502 | |||
503 | public function testWeirdIdentifier() |
||
581 | |||
582 | /** |
||
583 | * getMapping |
||
584 | * |
||
585 | * @access private |
||
586 | * @return Mapping |
||
587 | */ |
||
588 | private function getMapping($idKey = '@id') |
||
668 | |||
669 | /** |
||
670 | * createNewCart |
||
671 | * |
||
672 | * @access private |
||
673 | * @return AbstractModel |
||
674 | */ |
||
675 | private function createNewCart() |
||
687 | |||
688 | /** |
||
689 | * createCart |
||
690 | * |
||
691 | * @access private |
||
692 | * @return void |
||
693 | */ |
||
694 | private function createCart() |
||
701 | |||
702 | /** |
||
703 | * createKnownCartItem |
||
704 | * |
||
705 | * @access private |
||
706 | * @return AbstractModel |
||
707 | */ |
||
708 | private function createKnownCartItem() |
||
722 | |||
723 | /** |
||
724 | * createNewCartItem |
||
725 | * |
||
726 | * @access private |
||
727 | * @return AbstractModel |
||
728 | */ |
||
729 | private function createNewCartItem($addKnownedProduct = true) |
||
745 | |||
746 | /** |
||
747 | * createNewProduct |
||
748 | * |
||
749 | * @access private |
||
750 | * @return AbstractModel |
||
751 | */ |
||
752 | private function createNewProduct() |
||
761 | |||
762 | |||
763 | /** |
||
764 | * createKnownedProduct |
||
765 | * |
||
766 | * @access private |
||
767 | * @return AbstractModel |
||
768 | */ |
||
769 | private function createKnownedProduct() |
||
776 | |||
777 | private function createNewCartItemDetail() |
||
785 | |||
786 | /** |
||
787 | * createNewInstance |
||
788 | * |
||
789 | * @access private |
||
790 | * @return void |
||
791 | */ |
||
792 | private function createNewInstance($mapping = null) |
||
817 | |||
818 | private function getCartRepositoryMock($sdk, $restClient, $modelName) |
||
834 | } |
||
835 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.