Complex classes like Dereferencer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Dereferencer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Dereferencer |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $loaders; |
||
19 | |||
20 | /** |
||
21 | * Create a new Dereferencer. |
||
22 | */ |
||
23 | 156 | public function __construct() |
|
28 | |||
29 | /** |
||
30 | * Return the schema with all references resolved. |
||
31 | * |
||
32 | * @param string|object $schema Either a valid path like "http://json-schema.org/draft-03/schema#" |
||
33 | * or the object resulting from a json_decode call. |
||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | 156 | public function dereference($schema) |
|
49 | |||
50 | /** |
||
51 | * Register a Loader for the given prefix. |
||
52 | * |
||
53 | * @param Loader $loader |
||
54 | * @param string $prefix |
||
55 | */ |
||
56 | 122 | public function registerLoader(Loader $loader, $prefix) |
|
60 | |||
61 | /** |
||
62 | * Get the loader for the given prefix. |
||
63 | * |
||
64 | * @param $prefix |
||
65 | * |
||
66 | * @return Loader |
||
67 | * @throws \InvalidArgumentException |
||
68 | */ |
||
69 | 36 | private function getLoader($prefix) |
|
77 | |||
78 | /** |
||
79 | * Register the default file loader. |
||
80 | */ |
||
81 | 156 | private function registerFileLoader() |
|
85 | |||
86 | /** |
||
87 | * Register the default web loaders. If the curl extension is loaded, |
||
88 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
89 | * will be used. You can override this by registering your own loader |
||
90 | * for the 'http' and 'https' protocols. |
||
91 | */ |
||
92 | 156 | private function registerDefaultWebLoaders() |
|
102 | |||
103 | /** |
||
104 | * Crawl the schema and resolve any references. |
||
105 | * |
||
106 | * @param object $schema |
||
107 | * @param string|null $currentUri |
||
108 | * |
||
109 | * @return object |
||
110 | */ |
||
111 | 156 | private function crawl($schema, $currentUri = null) |
|
132 | |||
133 | /** |
||
134 | * Resolve the external referefence at the given path. |
||
135 | * |
||
136 | * @param object $schema The JSON Schema |
||
137 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
138 | * @param string $ref The JSON reference |
||
139 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
140 | * @return object The schema with the reference resolved. |
||
141 | */ |
||
142 | 20 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
|
149 | |||
150 | /** |
||
151 | * Merge the resolved reference with the schema, at the given path. |
||
152 | * |
||
153 | * @param object $schema The schema to merge the resolved reference with |
||
154 | * @param object $resolved The resolved schema |
||
155 | * @param string $path A JSON pointer to the path where the reference should be merged. |
||
156 | * @return void |
||
157 | */ |
||
158 | 44 | private function mergeResolvedReference($schema, $resolved, $path) |
|
173 | |||
174 | /** |
||
175 | * Check if the reference contains a fragment and resolve |
||
176 | * the pointer. Otherwise returns the original schema. |
||
177 | * |
||
178 | * @param string $ref |
||
179 | * @param object $schema |
||
180 | * @return object |
||
181 | */ |
||
182 | 46 | private function resolveFragment($ref, $schema) |
|
192 | |||
193 | /** |
||
194 | * Recursively get all of the references for the given schema. |
||
195 | * Returns an associative array like [path => reference]. |
||
196 | * Example: |
||
197 | * |
||
198 | * ['/properties' => '#/definitions/b'] |
||
199 | * |
||
200 | * The path does NOT include the $ref. |
||
201 | * |
||
202 | * @param object $schema The schema to resolve references for. |
||
203 | * @param string $path The current schema path. |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 156 | private function getReferences($schema, $path = '') |
|
236 | |||
237 | /** |
||
238 | * Push a segment onto the given path. |
||
239 | * |
||
240 | * @param string $path |
||
241 | * @param string $segment |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 102 | private function pathPush($path, $segment) |
|
249 | |||
250 | /** |
||
251 | * @param string $attribute |
||
252 | * @param mixed $attributeValue |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 156 | private function isRef($attribute, $attributeValue) |
|
260 | |||
261 | /** |
||
262 | * @param string $value |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | 48 | private function isInternalRef($value) |
|
270 | |||
271 | /** |
||
272 | * @param string $value |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 48 | private function isExternalRef($value) |
|
280 | |||
281 | /** |
||
282 | * Load an external ref and return the JSON object. |
||
283 | * |
||
284 | * @param string $reference |
||
285 | * |
||
286 | * @return object |
||
287 | */ |
||
288 | 38 | private function loadExternalRef($reference) |
|
299 | |||
300 | /** |
||
301 | * Merge a resolved reference into the root of the given schema. |
||
302 | * |
||
303 | * @param object $rootSchema |
||
304 | * @param object $resolvedRef |
||
305 | */ |
||
306 | 16 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
314 | |||
315 | /** |
||
316 | * Validate an absolute path is valid. |
||
317 | * |
||
318 | * @param string $path |
||
319 | */ |
||
320 | 38 | private function validateAbsolutePath($path) |
|
332 | |||
333 | /** |
||
334 | * Determine if a reference is relative. |
||
335 | * A reference is relative if it does not being with a prefix. |
||
336 | * |
||
337 | * @param string $ref |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 20 | private function isRelativeRef($ref) |
|
345 | |||
346 | /** |
||
347 | * Take a relative reference, and prepend the id of the schema and any |
||
348 | * sub schemas to get the absolute url. |
||
349 | * |
||
350 | * @param object $schema |
||
351 | * @param string $path |
||
352 | * @param string $ref |
||
353 | * @param string|null $currentUri |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 20 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
|
368 | |||
369 | /** |
||
370 | * Given the URI of the schema, get the intial resolution scope. |
||
371 | * |
||
372 | * If a URI is given, this method returns the URI without the schema filename or any reference fragment. |
||
373 | * I.E, Given 'http://localhost:1234/album.json#/artist', this method would return `http://localhost:1234/`. |
||
374 | * |
||
375 | * @param string|null $uri |
||
376 | * @return string |
||
377 | */ |
||
378 | 12 | private function getInitialResolutionScope($uri) |
|
382 | |||
383 | /** |
||
384 | * Given a JSON pointer, walk the path and resolve any found IDs against the parent scope. |
||
385 | * |
||
386 | * @param object $schema The JSON Schema object. |
||
387 | * @param string $path A JSON Pointer to the path we are resolving the scope for. |
||
388 | * @param string $parentScope The initial resolution scope. Usually the URI of the schema. |
||
389 | * @return string The resolved scope |
||
390 | */ |
||
391 | 12 | private function getResolvedResolutionScope($schema, $path, $parentScope) |
|
411 | |||
412 | /** |
||
413 | * Resolve an ID against the parent scope, and return the resolved scope. |
||
414 | * |
||
415 | * @param string $id The ID of the Schema. |
||
416 | * @param string $parentScope The parent scope of the ID. |
||
417 | * @return string |
||
418 | */ |
||
419 | 6 | private function resolveIdAgainstParentScope($id, $parentScope) |
|
429 | } |
||
430 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: