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 |
||
11 | class JSONTextTest extends SapphireTest |
||
|
|||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $fixtures = [ |
||
17 | 'array' => 'tests/fixtures/json/array.json', |
||
18 | 'object' => 'tests/fixtures/json/object.json' |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * JSONTextTest constructor. |
||
23 | * |
||
24 | * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
||
25 | */ |
||
26 | public function __construct() |
||
32 | |||
33 | /** |
||
34 | * @todo There are a ton more permutations of a JSONPath regex |
||
35 | * See the trace() method in JSONPath for more examples to work from |
||
36 | */ |
||
37 | View Code Duplication | public function testIsValidExpression() |
|
53 | |||
54 | /** |
||
55 | * @return void |
||
56 | */ |
||
57 | View Code Duplication | public function testIsValidJson() |
|
73 | |||
74 | |||
75 | /** |
||
76 | * Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need |
||
77 | * to allow otherwise invalid JSON by means of an optional 2nd param |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function testIsValidDBValue() |
||
92 | |||
93 | /** |
||
94 | * Properly excercise our internal SS type conversion. |
||
95 | */ |
||
96 | public function testToSSTypes() |
||
118 | |||
119 | /** |
||
120 | * Get the contents of a fixture |
||
121 | * |
||
122 | * @param string $fixture |
||
123 | * @return string |
||
124 | */ |
||
125 | private function getFixture($fixture) |
||
130 | } |
||
131 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.