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 | abstract class PropertyMatcherTester extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | public function testMatch() |
||
21 | { |
||
22 | $matcher = $this->buildMatcher(); |
||
23 | |||
24 | View Code Duplication | foreach ($this->getEquals() as $prop => $given) { |
|
|
|||
25 | $property = static::getMockBuilder(\ReflectionProperty::class)->disableOriginalConstructor()->getMock(); |
||
26 | $property->expects(static::any())->method('getName')->willReturn($prop); |
||
27 | $msg = sprintf("property '%s' not match with '%s'", $prop, $given); |
||
28 | static::assertTrue($matcher->match($property, $given), $msg); |
||
29 | } |
||
30 | |||
31 | View Code Duplication | foreach ($this->getNotEquals() as $prop => $given) { |
|
32 | $property = static::getMockBuilder(\ReflectionProperty::class)->disableOriginalConstructor()->getMock(); |
||
33 | $property->expects(static::any())->method('getName')->willReturn($prop); |
||
34 | $msg = sprintf("property '%s' match with '%s'", $prop, $given); |
||
35 | static::assertFalse($matcher->match($property, $given), $msg); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * buildMatcher. |
||
41 | * |
||
42 | * @return PropertyMatcherInterface |
||
43 | */ |
||
44 | abstract public function buildMatcher(); |
||
45 | |||
46 | /** |
||
47 | * getEquals. |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | abstract public function getEquals(); |
||
52 | |||
53 | /** |
||
54 | * getNotEquals. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | abstract public function getNotEquals(); |
||
59 | } |
||
60 |
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.