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 |
||
15 | class ResolverTest extends BaseTestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var Resolver |
||
19 | */ |
||
20 | private $resolver; |
||
21 | |||
22 | protected function setUp() |
||
26 | |||
27 | /** |
||
28 | * @expectedException \JVal\Exception\Resolver\EmptyStackException |
||
29 | */ |
||
30 | public function testGetCurrentSchemaThrowsIfStackIsEmpty() |
||
34 | |||
35 | /** |
||
36 | * @expectedException \JVal\Exception\Resolver\EmptyStackException |
||
37 | */ |
||
38 | public function testGetCurrentUriThrowsIfStackIsEmpty() |
||
42 | |||
43 | /** |
||
44 | * @expectedException \JVal\Exception\Resolver\EmptyStackException |
||
45 | */ |
||
46 | public function testLeaveThrowsIfStackIsEmpty() |
||
50 | |||
51 | /** |
||
52 | * @dataProvider rootRefProvider |
||
53 | * |
||
54 | * @param string $schemaName |
||
55 | */ |
||
56 | public function testResolveLocalRoot($schemaName) |
||
63 | |||
64 | /** |
||
65 | * @dataProvider chainProvider |
||
66 | * |
||
67 | * @param stdClass $schema |
||
68 | * @param string $pointerUri |
||
69 | * @param stdClass $resolved |
||
70 | */ |
||
71 | View Code Duplication | public function testResolveChain(stdClass $schema, $pointerUri, stdClass $resolved) |
|
79 | |||
80 | /** |
||
81 | * @dataProvider unresolvablePointerPropertyProvider |
||
82 | * @expectedException \JVal\Exception\Resolver\UnresolvedPointerPropertyException |
||
83 | * |
||
84 | * @param stdClass $schema |
||
85 | * @param string $pointerUri |
||
86 | */ |
||
87 | View Code Duplication | public function testResolveThrowsOnUnresolvedPointerProperty(stdClass $schema, $pointerUri) |
|
94 | |||
95 | /** |
||
96 | * @dataProvider invalidPointerIndexProvider |
||
97 | * @expectedException \JVal\Exception\Resolver\InvalidPointerIndexException |
||
98 | * |
||
99 | * @param stdClass $schema |
||
100 | * @param string $pointerUri |
||
101 | */ |
||
102 | View Code Duplication | public function testResolveThrowsOnInvalidPointerIndex(stdClass $schema, $pointerUri) |
|
109 | |||
110 | /** |
||
111 | * @dataProvider unresolvablePointerIndexProvider |
||
112 | * @expectedException \JVal\Exception\Resolver\UnresolvedPointerIndexException |
||
113 | * |
||
114 | * @param stdClass $schema |
||
115 | * @param string $pointerUri |
||
116 | */ |
||
117 | View Code Duplication | public function testResolveThrowsOnUnresolvedPointerIndex(stdClass $schema, $pointerUri) |
|
124 | |||
125 | /** |
||
126 | * @dataProvider invalidPointerSegmentProvider |
||
127 | * @expectedException \JVal\Exception\Resolver\InvalidSegmentTypeException |
||
128 | * |
||
129 | * @param stdClass $schema |
||
130 | * @param string $pointerUri |
||
131 | */ |
||
132 | View Code Duplication | public function testResolveThrowsOnInvalidPointerSegment(stdClass $schema, $pointerUri) |
|
139 | |||
140 | /** |
||
141 | * @dataProvider invalidPointerTargetProvider |
||
142 | * @expectedException \JVal\Exception\Resolver\InvalidPointerTargetException |
||
143 | * |
||
144 | * @param stdClass $schema |
||
145 | * @param string $pointerUri |
||
146 | */ |
||
147 | View Code Duplication | public function testResolveThrowsOnInvalidPointerTarget(stdClass $schema, $pointerUri) |
|
154 | |||
155 | /** |
||
156 | * @dataProvider selfReferencingPointerProvider |
||
157 | * @expectedException \JVal\Exception\Resolver\SelfReferencingPointerException |
||
158 | * |
||
159 | * @param stdClass $schema |
||
160 | * @param stdClass $reference |
||
161 | */ |
||
162 | public function testResolveThrowsOnSelfReferencingPointer(stdClass $schema, stdClass $reference) |
||
167 | |||
168 | /** |
||
169 | * @group network |
||
170 | * @dataProvider unfetchableUriProvider |
||
171 | * @expectedException \JVal\Exception\Resolver\UnfetchableUriException |
||
172 | * |
||
173 | * @param string $pointerUri |
||
174 | */ |
||
175 | View Code Duplication | public function testResolveThrowsOnUnfetchableUri($pointerUri) |
|
182 | |||
183 | /** |
||
184 | * @group network |
||
185 | * @dataProvider remoteUriProvider |
||
186 | * |
||
187 | * @param string $pointerUri |
||
188 | * @param stdClass $expectedSchema |
||
189 | */ |
||
190 | View Code Duplication | public function testResolveRemoteSchema($pointerUri, stdClass $expectedSchema) |
|
198 | |||
199 | /** |
||
200 | * @expectedException \JVal\Exception\JsonDecodeException |
||
201 | */ |
||
202 | View Code Duplication | public function testResolveThrowsOnUndecodableRemoteSchema() |
|
210 | |||
211 | /** |
||
212 | * @expectedException \JVal\Exception\Resolver\InvalidRemoteSchemaException |
||
213 | */ |
||
214 | View Code Duplication | public function testResolveThrowsOnInvalidRemoteSchema() |
|
222 | |||
223 | public function rootRefProvider() |
||
231 | |||
232 | public function chainProvider() |
||
251 | |||
252 | public function unresolvablePointerPropertyProvider() |
||
262 | |||
263 | View Code Duplication | public function invalidPointerIndexProvider() |
|
272 | |||
273 | View Code Duplication | public function unresolvablePointerIndexProvider() |
|
282 | |||
283 | View Code Duplication | public function invalidPointerSegmentProvider() |
|
292 | |||
293 | View Code Duplication | public function invalidPointerTargetProvider() |
|
302 | |||
303 | public function selfReferencingPointerProvider() |
||
313 | |||
314 | public function unfetchableUriProvider() |
||
324 | |||
325 | public function remoteUriProvider() |
||
336 | |||
337 | private function getVendorDir() |
||
352 | } |
||
353 |
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.