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 | 158 | 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 | 158 | public function dereference($schema) |
|
38 | { |
||
39 | 158 | if (is_string($schema)) { |
|
40 | 24 | $uri = $schema; |
|
41 | 24 | $schema = $this->loadExternalRef($uri); |
|
42 | 24 | $schema = $this->resolveFragment($uri, $schema); |
|
43 | |||
44 | 24 | return $this->crawl($schema, strip_fragment($uri)); |
|
45 | } |
||
46 | |||
47 | 134 | return $this->crawl($schema); |
|
48 | } |
||
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 | 38 | private function getLoader($prefix) |
|
77 | |||
78 | /** |
||
79 | * Register the default file loader. |
||
80 | */ |
||
81 | 158 | 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 | 158 | 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 | 158 | private function crawl($schema, $currentUri = null) |
|
134 | |||
135 | /** |
||
136 | * Resolve the external reference at the given path. |
||
137 | * |
||
138 | * @param object $schema The JSON Schema |
||
139 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
140 | * @param string $ref The JSON reference |
||
141 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
142 | 20 | * |
|
143 | * @return object The schema with the reference resolved. |
||
144 | 20 | */ |
|
145 | 20 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
|
152 | |||
153 | /** |
||
154 | * Merge the resolved reference with the schema, at the given path. |
||
155 | * |
||
156 | * @param object $schema The schema to merge the resolved reference with |
||
157 | * @param object $resolved The resolved schema |
||
158 | 46 | * @param string $path A JSON pointer to the path where the reference should be merged. |
|
159 | * |
||
160 | 46 | * @return void |
|
161 | */ |
||
162 | 16 | private function mergeResolvedReference($schema, $resolved, $path) |
|
177 | |||
178 | /** |
||
179 | * Check if the reference contains a fragment and resolve |
||
180 | * the pointer. Otherwise returns the original schema. |
||
181 | * |
||
182 | 48 | * @param string $ref |
|
183 | * @param object $schema |
||
184 | 48 | * |
|
185 | 48 | * @return object |
|
186 | 10 | */ |
|
187 | 10 | private function resolveFragment($ref, $schema) |
|
200 | |||
201 | /** |
||
202 | * Recursively get all of the references for the given schema. |
||
203 | * Returns an associative array like [path => reference]. |
||
204 | * Example: |
||
205 | * |
||
206 | * ['/properties' => '#/definitions/b'] |
||
207 | 158 | * |
|
208 | * The path does NOT include the $ref. |
||
209 | 158 | * |
|
210 | * @param object $schema The schema to resolve references for. |
||
211 | 158 | * @param string $path The current schema path. |
|
212 | 40 | * |
|
213 | * @return array |
||
214 | */ |
||
215 | 158 | private function getReferences($schema, $path = '') |
|
244 | |||
245 | 104 | /** |
|
246 | * Push a segment onto the given path. |
||
247 | 104 | * |
|
248 | * @param string $path |
||
249 | * @param string $segment |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | private function pathPush($path, $segment) |
||
257 | |||
258 | 158 | /** |
|
259 | * @param string $attribute |
||
260 | * @param mixed $attributeValue |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | private function isRef($attribute, $attributeValue) |
||
268 | 50 | ||
269 | /** |
||
270 | * @param string $value |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | private function isInternalRef($value) |
||
278 | 50 | ||
279 | /** |
||
280 | * @param string $value |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | private function isExternalRef($value) |
||
288 | 40 | ||
289 | /** |
||
290 | 40 | * Load an external ref and return the JSON object. |
|
291 | 38 | * |
|
292 | * @param string $reference |
||
293 | 38 | * |
|
294 | * @return object |
||
295 | 38 | */ |
|
296 | private function loadExternalRef($reference) |
||
307 | |||
308 | 16 | /** |
|
309 | 16 | * Merge a resolved reference into the root of the given schema. |
|
310 | 16 | * |
|
311 | 16 | * @param object $rootSchema |
|
312 | 16 | * @param object $resolvedRef |
|
313 | 16 | */ |
|
314 | private function mergeRootRef($rootSchema, $resolvedRef) |
||
322 | 40 | ||
323 | 2 | /** |
|
324 | 2 | * Validate an absolute path is valid. |
|
325 | * |
||
326 | 2 | * @param string $path |
|
327 | */ |
||
328 | 2 | private function validateAbsolutePath($path) |
|
340 | |||
341 | 20 | /** |
|
342 | * Take a relative reference, and prepend the id of the schema and any |
||
343 | 20 | * sub schemas to get the absolute url. |
|
344 | * |
||
345 | * @param object $schema |
||
346 | * @param string $path |
||
347 | * @param string $ref |
||
348 | * @param string|null $currentUri |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
||
364 | 12 | ||
365 | /** |
||
366 | 12 | * Get the resolved resolution scope by walking the schema and resolving |
|
367 | * every `id` against the msot immediate parent scope. |
||
368 | * |
||
369 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
||
370 | * |
||
371 | * @param object $schema |
||
372 | * @param string $path |
||
373 | * @param string $scope |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | private function getResolvedResolutionScope($schema, $path, $scope) |
||
396 | } |
||
397 |