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 |
||
7 | class ClassMirrorTest extends \PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | /** |
||
10 | * @test |
||
11 | */ |
||
12 | View Code Duplication | public function it_reflects_allowed_magic_methods() |
|
|
|||
13 | { |
||
14 | $class = new \ReflectionClass('Fixtures\Prophecy\SpecialMethods'); |
||
15 | |||
16 | $mirror = new ClassMirror(); |
||
17 | |||
18 | $node = $mirror->reflect($class, array()); |
||
19 | |||
20 | $this->assertCount(7, $node->getMethods()); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @test |
||
25 | */ |
||
26 | public function it_reflects_protected_abstract_methods() |
||
41 | |||
42 | /** |
||
43 | * @test |
||
44 | */ |
||
45 | public function it_reflects_public_static_methods() |
||
60 | |||
61 | /** |
||
62 | * @test |
||
63 | */ |
||
64 | public function it_marks_required_args_without_types_as_not_optional() |
||
83 | |||
84 | /** |
||
85 | * @test |
||
86 | */ |
||
87 | public function it_properly_reads_methods_arguments_with_types() |
||
117 | |||
118 | /** |
||
119 | * @test |
||
120 | * @requires PHP 5.4 |
||
121 | */ |
||
122 | public function it_properly_reads_methods_arguments_with_callable_types() |
||
147 | |||
148 | /** |
||
149 | * @test |
||
150 | * @requires PHP 5.6 |
||
151 | */ |
||
152 | public function it_properly_reads_methods_variadic_arguments() |
||
181 | |||
182 | /** |
||
183 | * @test |
||
184 | */ |
||
185 | View Code Duplication | public function it_marks_passed_by_reference_args_as_passed_by_reference() |
|
186 | { |
||
187 | $class = new \ReflectionClass('Fixtures\Prophecy\WithReferences'); |
||
188 | |||
189 | $mirror = new ClassMirror(); |
||
190 | |||
191 | $classNode = $mirror->reflect($class, array()); |
||
192 | |||
193 | $this->assertTrue($classNode->hasMethod('methodWithReferenceArgument')); |
||
194 | |||
195 | $argNodes = $classNode->getMethod('methodWithReferenceArgument')->getArguments(); |
||
196 | |||
197 | $this->assertCount(2, $argNodes); |
||
198 | |||
199 | $this->assertTrue($argNodes[0]->isPassedByReference()); |
||
200 | $this->assertTrue($argNodes[1]->isPassedByReference()); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @test |
||
205 | */ |
||
206 | View Code Duplication | public function it_throws_an_exception_if_class_is_final() |
|
207 | { |
||
208 | $class = new \ReflectionClass('Fixtures\Prophecy\FinalClass'); |
||
209 | |||
210 | $mirror = new ClassMirror(); |
||
211 | |||
212 | $this->setExpectedException('Prophecy\Exception\Doubler\ClassMirrorException'); |
||
213 | |||
214 | $mirror->reflect($class, array()); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @test |
||
219 | */ |
||
220 | View Code Duplication | public function it_ignores_final_methods() |
|
221 | { |
||
222 | $class = new \ReflectionClass('Fixtures\Prophecy\WithFinalMethod'); |
||
223 | |||
224 | $mirror = new ClassMirror(); |
||
225 | |||
226 | $classNode = $mirror->reflect($class, array()); |
||
227 | |||
228 | $this->assertCount(0, $classNode->getMethods()); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @test |
||
233 | */ |
||
234 | public function it_marks_final_methods_as_unextendable() |
||
245 | |||
246 | /** |
||
247 | * @test |
||
248 | */ |
||
249 | View Code Duplication | public function it_throws_an_exception_if_interface_provided_instead_of_class() |
|
250 | { |
||
251 | $class = new \ReflectionClass('Fixtures\Prophecy\EmptyInterface'); |
||
252 | |||
253 | $mirror = new ClassMirror(); |
||
254 | |||
255 | $this->setExpectedException('Prophecy\Exception\InvalidArgumentException'); |
||
256 | |||
257 | $mirror->reflect($class, array()); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @test |
||
262 | */ |
||
263 | public function it_reflects_all_interfaces_methods() |
||
284 | |||
285 | /** |
||
286 | * @test |
||
287 | */ |
||
288 | public function it_ignores_virtually_private_methods() |
||
301 | |||
302 | /** |
||
303 | * @test |
||
304 | */ |
||
305 | View Code Duplication | public function it_does_not_throw_exception_for_virtually_private_finals() |
|
306 | { |
||
307 | $class = new \ReflectionClass('Fixtures\Prophecy\WithFinalVirtuallyPrivateMethod'); |
||
308 | |||
309 | $mirror = new ClassMirror(); |
||
310 | |||
311 | $classNode = $mirror->reflect($class, array()); |
||
312 | |||
313 | $this->assertCount(0, $classNode->getMethods()); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @test |
||
318 | * @requires PHP 7 |
||
319 | */ |
||
320 | public function it_reflects_return_typehints() |
||
337 | |||
338 | /** |
||
339 | * @test |
||
340 | */ |
||
341 | public function it_throws_an_exception_if_class_provided_in_interfaces_list() |
||
351 | |||
352 | /** |
||
353 | * @test |
||
354 | */ |
||
355 | public function it_throws_an_exception_if_not_reflection_provided_as_interface() |
||
363 | |||
364 | /** |
||
365 | * @test |
||
366 | */ |
||
367 | View Code Duplication | public function it_doesnt_use_scalar_typehints() |
|
368 | { |
||
369 | $mirror = new ClassMirror(); |
||
370 | |||
371 | $classNode = $mirror->reflect(new \ReflectionClass('ReflectionMethod'), array()); |
||
372 | $method = $classNode->getMethod('export'); |
||
373 | $arguments = $method->getArguments(); |
||
374 | |||
375 | $this->assertNull($arguments[0]->getTypeHint()); |
||
376 | $this->assertNull($arguments[1]->getTypeHint()); |
||
377 | $this->assertNull($arguments[2]->getTypeHint()); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @test |
||
382 | */ |
||
383 | View Code Duplication | public function it_doesnt_fail_to_typehint_nonexistent_FQCN() |
|
384 | { |
||
385 | $mirror = new ClassMirror(); |
||
386 | |||
387 | $classNode = $mirror->reflect(new \ReflectionClass('Fixtures\Prophecy\OptionalDepsClass'), array()); |
||
388 | $method = $classNode->getMethod('iHaveAStrangeTypeHintedArg'); |
||
389 | $arguments = $method->getArguments(); |
||
390 | $this->assertEquals('I\Simply\Am\Nonexistent', $arguments[0]->getTypeHint()); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @test |
||
395 | */ |
||
396 | View Code Duplication | public function it_doesnt_fail_to_typehint_nonexistent_RQCN() |
|
397 | { |
||
398 | $mirror = new ClassMirror(); |
||
399 | |||
400 | $classNode = $mirror->reflect(new \ReflectionClass('Fixtures\Prophecy\OptionalDepsClass'), array()); |
||
401 | $method = $classNode->getMethod('iHaveAnEvenStrangerTypeHintedArg'); |
||
402 | $arguments = $method->getArguments(); |
||
403 | $this->assertEquals('I\Simply\Am\Not', $arguments[0]->getTypeHint()); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @test |
||
408 | */ |
||
409 | function it_changes_argument_names_if_they_are_varying() |
||
453 | } |
||
454 |
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.