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 |
||
36 | class EdgeTest extends BaseGraphTest { |
||
37 | |||
38 | /** |
||
39 | * @var Edge |
||
40 | */ |
||
41 | private $Edge; |
||
42 | |||
43 | /** |
||
44 | * @var Attribute |
||
45 | */ |
||
46 | private $Attribute; |
||
47 | |||
48 | /** |
||
49 | * Prepares the environment before running a test. |
||
50 | */ |
||
51 | protected function setUp() : void { |
||
62 | |||
63 | /** |
||
64 | * Cleans up the environment after running a test. |
||
65 | */ |
||
66 | protected function tearDown() : void { |
||
70 | |||
71 | /** |
||
72 | * Tests Edge->__construct() |
||
73 | */ |
||
74 | public function test__construct() { |
||
78 | |||
79 | /** |
||
80 | * Tests Edge->build() |
||
81 | * |
||
82 | * @TODO test graph build |
||
83 | */ |
||
84 | public function testBuild() { |
||
92 | |||
93 | /** |
||
94 | * Common logic for the testBuild* test methods. |
||
95 | * |
||
96 | * @param string $expected |
||
97 | * The expected GraphViz output. |
||
98 | * @param array $edges |
||
99 | * |
||
100 | * @internal param array $toSet An array of edges to add.* An array of edges to add. |
||
101 | */ |
||
102 | public function buildTestHelper($expected, array $edges) { |
||
113 | |||
114 | public function testBuildAttributesNormal() { |
||
124 | |||
125 | // Attribute list with empty title in middle on edge bound to root graph. |
||
126 | View Code Duplication | public function testBuildAttributesEmptyMiddle() { |
|
138 | |||
139 | // Attribute list with empty title as single attribute on edge bound to root graph. |
||
140 | public function testBuildAttributesOnlyEmpty() { |
||
150 | |||
151 | // Attribute list with empty title as last attribute on edge bound to root graph. |
||
152 | View Code Duplication | public function testBuildAttributesEmptyLast() { |
|
164 | |||
165 | /** |
||
166 | * Tests Edge::getAllowedChildTypes() |
||
167 | */ |
||
168 | public function testGetAllowedChildTypes() { |
||
171 | |||
172 | /** |
||
173 | * Tests Edge::getType() |
||
174 | */ |
||
175 | public function testGetType() { |
||
178 | } |
||
179 | |||
180 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.