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 |
||
34 | class NodeTest extends TestCase |
||
35 | { |
||
36 | /** |
||
37 | * @test |
||
38 | */ |
||
39 | public function shouldGetTypeForSimpleType() |
||
50 | |||
51 | /** |
||
52 | * @test |
||
53 | */ |
||
54 | View Code Duplication | public function shouldGetTypeForObjectType() |
|
|
|||
55 | { |
||
56 | //given |
||
57 | $elements = [ |
||
58 | new Node('string', '$name', false) |
||
59 | ]; |
||
60 | $node = new Node('object', '$user', false, $elements); |
||
61 | |||
62 | //when |
||
63 | $type = $node->getType(); |
||
64 | |||
65 | //then |
||
66 | $this->assertEquals('object', $type); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @test |
||
71 | */ |
||
72 | View Code Duplication | public function shouldReturnName() |
|
73 | { |
||
74 | //given |
||
75 | $node = new Node('int', '$age', false); |
||
76 | |||
77 | //when |
||
78 | $name = $node->getName(); |
||
79 | |||
80 | //then |
||
81 | $this->assertEquals('$age', $name); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @test |
||
86 | */ |
||
87 | View Code Duplication | public function shouldGetSanitizedName() |
|
88 | { |
||
89 | //given |
||
90 | $node = new Node('int', '$age', false); |
||
91 | |||
92 | //when |
||
93 | $name = $node->getSanitizedName(); |
||
94 | |||
95 | //then |
||
96 | $this->assertEquals('age', $name); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @test |
||
101 | */ |
||
102 | public function shouldGetNameForArray() |
||
113 | |||
114 | /** |
||
115 | * @test |
||
116 | */ |
||
117 | View Code Duplication | public function shouldGetNameForObject() |
|
118 | { |
||
119 | //given |
||
120 | $elements = [ |
||
121 | new Node('string', '$name', false) |
||
122 | ]; |
||
123 | $node = new Node('object', '$user', false, $elements); |
||
124 | |||
125 | //when |
||
126 | $name = $node->getNameForObject(); |
||
127 | |||
128 | //then |
||
129 | $this->assertEquals('User', $name); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @test |
||
134 | */ |
||
135 | View Code Duplication | public function shouldSingularizeeNameForObject() |
|
136 | { |
||
137 | //given |
||
138 | $elements = [ |
||
139 | new Node('string', '$name', false) |
||
140 | ]; |
||
141 | $node = new Node('object', '$users', true, $elements); |
||
142 | |||
143 | //when |
||
144 | $name = $node->getNameForObject(); |
||
145 | |||
146 | //then |
||
147 | $this->assertEquals('User', $name); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @test |
||
152 | */ |
||
153 | public function shouldReturnFalseWhenTypeIsNotArray() |
||
164 | |||
165 | /** |
||
166 | * @test |
||
167 | */ |
||
168 | public function shouldReturnTrueWhenTypeIsArray() |
||
179 | |||
180 | /** |
||
181 | * @test |
||
182 | */ |
||
183 | public function shouldReturnFalseWhenTypeIsNotObject() |
||
194 | |||
195 | /** |
||
196 | * @test |
||
197 | */ |
||
198 | View Code Duplication | public function shouldReturnTrueWhenTypeIsObject() |
|
199 | { |
||
212 | } |
||
213 |
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.