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 RouteTest extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var Route |
||
23 | */ |
||
24 | public $route; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $expectedMethod = 'GET'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $expectedPattern = '/path'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $expectedHandler = 'handler'; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $expectedNames = ['name-1','name-2']; |
||
45 | |||
46 | /** |
||
47 | * Route setUp. |
||
48 | */ |
||
49 | public function setUp() |
||
58 | |||
59 | /** |
||
60 | * Test immutable Route::withMethod() |
||
61 | */ |
||
62 | View Code Duplication | public function testWithMethod() |
|
71 | |||
72 | /** |
||
73 | * Test immutable Route::withPattern() |
||
74 | */ |
||
75 | View Code Duplication | public function testWithPattern() |
|
84 | |||
85 | /** |
||
86 | * |
||
87 | */ |
||
88 | public function testWithNames() |
||
97 | |||
98 | /** |
||
99 | * |
||
100 | */ |
||
101 | View Code Duplication | public function testWithHandlerString() |
|
111 | |||
112 | /** |
||
113 | * |
||
114 | */ |
||
115 | View Code Duplication | public function testWithHandlerClosure() |
|
129 | |||
130 | /** |
||
131 | * |
||
132 | */ |
||
133 | public function testGetMethod() |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | */ |
||
141 | public function testGetHandler() |
||
145 | |||
146 | /** |
||
147 | * |
||
148 | */ |
||
149 | public function testGetHandlerTypeString() |
||
155 | |||
156 | /** |
||
157 | * |
||
158 | */ |
||
159 | public function testGetHandlerTypeClosure() |
||
168 | |||
169 | /** |
||
170 | * |
||
171 | */ |
||
172 | public function testGetPattern() |
||
176 | |||
177 | /** |
||
178 | * |
||
179 | */ |
||
180 | public function testGetNames() |
||
184 | } |
||
185 |
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.