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 |
||
16 | class ArrayChangeKeyCaseTest extends TestCase |
||
17 | { |
||
18 | const SAMPLE_ARRAY = [ |
||
19 | 'oneKeyUsingCamelCase' => 1, |
||
20 | 'OneKeyUsingPascalCase' => 1, |
||
21 | 'one key with spaces' => 1, |
||
22 | 'one-key-with-hyphens' => 1, |
||
23 | 'and_one_with_underscores' => 1, |
||
24 | 'what About_Mixed-separators!?!_and-numb333r5?' => 1, |
||
25 | 'one' => 1, |
||
26 | 'onewithsymbol!' => 1, |
||
27 | '~~ ~~' => 1, |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @test |
||
32 | */ |
||
33 | public function it_change_nothing_for_non_associative_arrays() |
||
40 | |||
41 | /** |
||
42 | * @test |
||
43 | */ |
||
44 | public function it_change_nothing_for_invalid_case_flag() |
||
51 | |||
52 | /** |
||
53 | * @test |
||
54 | */ |
||
55 | public function it_works_with_php_core_upper_case() |
||
62 | |||
63 | /** |
||
64 | * @test |
||
65 | */ |
||
66 | public function it_works_with_php_core_lower_case() |
||
73 | |||
74 | /** |
||
75 | * @test |
||
76 | */ |
||
77 | public function it_works_with_spatie_upper_case() |
||
84 | |||
85 | /** |
||
86 | * @test |
||
87 | */ |
||
88 | public function it_works_with_spatie_lower_case() |
||
95 | |||
96 | /** |
||
97 | * @test |
||
98 | */ |
||
99 | public function it_works_with_snake_case() |
||
116 | |||
117 | /** |
||
118 | * @test |
||
119 | */ |
||
120 | public function it_works_with_title_case() |
||
137 | |||
138 | /** |
||
139 | * @test |
||
140 | */ |
||
141 | View Code Duplication | public function it_works_with_camel_case() |
|
158 | |||
159 | /** |
||
160 | * @test |
||
161 | */ |
||
162 | View Code Duplication | public function it_works_with_pascal_case() |
|
179 | |||
180 | /** |
||
181 | * @test |
||
182 | */ |
||
183 | public function it_works_with_lisp_case() |
||
200 | } |
||
201 |
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.