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 |
||
16 | class JSONTextTest extends SapphireTest |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $fixtures = [ |
||
22 | 'array' => 'fixtures/json/array.json', |
||
23 | 'object' => 'fixtures/json/object.json' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * JSONTextTest constructor. |
||
28 | * |
||
29 | * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
||
30 | */ |
||
31 | View Code Duplication | public function __construct() |
|
39 | |||
40 | /** |
||
41 | * @todo There are a ton more permutations of a JSONPath regex |
||
42 | * See the trace() method in JSONPath for more examples to work from |
||
43 | */ |
||
44 | View Code Duplication | public function testIsValidExpression() |
|
60 | |||
61 | /** |
||
62 | * @return void |
||
63 | */ |
||
64 | View Code Duplication | public function testIsValidJson() |
|
80 | |||
81 | |||
82 | /** |
||
83 | * Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need |
||
84 | * to allow otherwise invalid JSON by means of an optional 2nd param |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function testIsValidDBValue() |
||
99 | |||
100 | /** |
||
101 | * Properly excercise our internal SS type conversion. |
||
102 | */ |
||
103 | public function testToSSTypes() |
||
125 | |||
126 | /** |
||
127 | * Get the contents of a fixture |
||
128 | * |
||
129 | * @param string $fixture |
||
130 | * @return string |
||
131 | */ |
||
132 | private function getFixture($fixture) |
||
137 | } |
||
138 |
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.