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 |
||
22 | class RouterTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var \PHPUnit_Framework_MockObject_MockObject|AdaptorInterface |
||
27 | */ |
||
28 | public $adaptor; |
||
29 | |||
30 | /** |
||
31 | * @var \PHPUnit_Framework_MockObject_MockObject|RouteCollectionInterface |
||
32 | */ |
||
33 | public $collection; |
||
34 | |||
35 | /** |
||
36 | * @var RouterInterface |
||
37 | */ |
||
38 | public $router; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * |
||
43 | */ |
||
44 | public function setUp() |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | */ |
||
58 | View Code Duplication | public function testGetRouterResponseFromPath() |
|
72 | |||
73 | /** |
||
74 | * |
||
75 | */ |
||
76 | View Code Duplication | public function testGetRouterResponse() |
|
90 | |||
91 | |||
92 | /** |
||
93 | * |
||
94 | */ |
||
95 | public function testWithAdaptor() |
||
101 | |||
102 | /** |
||
103 | * |
||
104 | */ |
||
105 | public function testGetAdaptor() |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | */ |
||
115 | public function testWithCollection() |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | */ |
||
129 | public function testGetCollection() |
||
135 | |||
136 | /** |
||
137 | * |
||
138 | */ |
||
139 | public function testGetCollectionCached() |
||
152 | |||
153 | |||
154 | /** |
||
155 | * |
||
156 | */ |
||
157 | public function testMergeCollection() |
||
169 | |||
170 | /** |
||
171 | * |
||
172 | */ |
||
173 | public function testIsCached() |
||
180 | } |
||
181 |
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.