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 |
||
37 | class IsValidTest extends TestCase |
||
38 | { |
||
39 | /** |
||
40 | * @test |
||
41 | * @dataProvider validStyles |
||
42 | * @param string $style |
||
43 | */ |
||
44 | public function shouldNotThrowExceptionWhenStyleIsValid($style) |
||
52 | |||
53 | public function validStyles() |
||
60 | |||
61 | /** |
||
62 | * @test |
||
63 | */ |
||
64 | public function shouldThrowExceptionWhenStyleIsInValid() |
||
76 | |||
77 | /** |
||
78 | * @test |
||
79 | * @dataProvider validUses |
||
80 | * @param string $use |
||
81 | */ |
||
82 | public function shouldNotThrowExceptionWhenUseIsValid($use) |
||
90 | |||
91 | public function validUses() |
||
98 | |||
99 | /** |
||
100 | * @test |
||
101 | */ |
||
102 | public function shouldThrowExceptionWhenUseIsInValid() |
||
114 | |||
115 | /** |
||
116 | * @test |
||
117 | * @dataProvider validSoapVersions |
||
118 | * @param string $soapVersion |
||
119 | */ |
||
120 | public function shouldNotThrowExceptionWhenSoapVersionIsValid($soapVersion) |
||
128 | |||
129 | public function validSoapVersions() |
||
136 | |||
137 | /** |
||
138 | * @test |
||
139 | */ |
||
140 | public function shouldThrowExceptionWhenSoapVersionIsInValid() |
||
152 | |||
153 | /** |
||
154 | * @test |
||
155 | */ |
||
156 | public function shouldNotThrowExceptionWhenValueIsNotEmpty() |
||
164 | |||
165 | /** |
||
166 | * @test |
||
167 | * @dataProvider emptyValues |
||
168 | * @param mixed $value |
||
169 | */ |
||
170 | View Code Duplication | public function shouldThrowExceptionWhenValueIsEmpty($value) |
|
|
|||
171 | { |
||
172 | //when |
||
173 | try { |
||
174 | IsValid::notEmpty($value); |
||
175 | $this->assertFalse(true, 'Triggered when exception is not throw'); |
||
176 | } catch (InvalidArgumentException $e) { |
||
177 | //then |
||
178 | $this->assertEquals('Value cannot be empty', $e->getMessage()); |
||
179 | $this->assertInstanceOf('\InvalidArgumentException', $e); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @test |
||
185 | * @dataProvider emptyValues |
||
186 | * @param mixed $value |
||
187 | * @param string $customMessage |
||
188 | */ |
||
189 | View Code Duplication | public function shouldThrowExceptionWithCustomMessageWhenValueIsEmpty($value, $customMessage) |
|
190 | { |
||
191 | //when |
||
192 | try { |
||
193 | IsValid::notEmpty($value, $customMessage); |
||
194 | $this->assertFalse(true, 'Triggered when exception is not throw'); |
||
195 | } catch (InvalidArgumentException $e) { |
||
196 | //then |
||
197 | $this->assertEquals($customMessage, $e->getMessage()); |
||
198 | $this->assertInstanceOf('\InvalidArgumentException', $e); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | public function emptyValues() |
||
209 | |||
210 | /** |
||
211 | * @test |
||
212 | * @dataProvider validParameterStyles |
||
213 | * @param string $parameterStyle |
||
214 | */ |
||
215 | public function shouldNotThrowExceptionWhenParameterStyleIsValid($parameterStyle) |
||
223 | |||
224 | public function validParameterStyles() |
||
231 | |||
232 | /** |
||
233 | * @test |
||
234 | */ |
||
235 | View Code Duplication | public function shouldThrowExceptionWhenParameterStyleIsInValid() |
|
247 | |||
248 | /** |
||
249 | * @test |
||
250 | */ |
||
251 | View Code Duplication | public function shouldThrowExceptionWhenSetParameterStyleWrappedForRpc() |
|
263 | } |
||
264 |
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.