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 |
||
10 | class StoreTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | /** |
||
13 | * It should throw an exception if an object is not the correct class. |
||
14 | * |
||
15 | * @expectedException \InvalidArgumentException |
||
16 | * @expectedExceptionMessage All objects in collection must be of class "Psi\Bridge\ObjectAgent\Doctrine\Collections\Tests\Unit\TestClass |
||
17 | */ |
||
18 | public function testExceptionIntruder() |
||
27 | |||
28 | /** |
||
29 | * It should throw an exception if getting a collection for an unknown class. |
||
30 | * |
||
31 | * @expectedException \InvalidArgumentException |
||
32 | * @expectedExceptionMessage No collections available of class "stdClass" |
||
33 | */ |
||
34 | public function testGetCollectionException() |
||
39 | |||
40 | /** |
||
41 | * It should create a non-existing collection. |
||
42 | */ |
||
43 | public function testGetOrCreateCollection() |
||
51 | |||
52 | /** |
||
53 | * It should remove an object. |
||
54 | */ |
||
55 | View Code Duplication | public function testDelete() |
|
68 | |||
69 | /** |
||
70 | * It should find an object. |
||
71 | */ |
||
72 | View Code Duplication | public function testFind() |
|
85 | |||
86 | /** |
||
87 | * It should say if it has a collection or not. |
||
88 | */ |
||
89 | public function testHasCollection() |
||
97 | } |
||
98 | |||
102 |
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.