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) |
|
132 | |||
133 | /** |
||
134 | * Resolve the external reference 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 | * |
||
141 | * @return object The schema with the reference resolved. |
||
142 | 20 | */ |
|
143 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
||
150 | |||
151 | /** |
||
152 | * Merge the resolved reference with the schema, at the given path. |
||
153 | * |
||
154 | * @param object $schema The schema to merge the resolved reference with |
||
155 | * @param object $resolved The resolved schema |
||
156 | * @param string $path A JSON pointer to the path where the reference should be merged. |
||
157 | * |
||
158 | 46 | * @return void |
|
159 | */ |
||
160 | 46 | private function mergeResolvedReference($schema, $resolved, $path) |
|
175 | |||
176 | /** |
||
177 | * Check if the reference contains a fragment and resolve |
||
178 | * the pointer. Otherwise returns the original schema. |
||
179 | * |
||
180 | * @param string $ref |
||
181 | * @param object $schema |
||
182 | 48 | * |
|
183 | * @return object |
||
184 | 48 | */ |
|
185 | 48 | private function resolveFragment($ref, $schema) |
|
196 | |||
197 | /** |
||
198 | * Recursively get all of the references for the given schema. |
||
199 | * Returns an associative array like [path => reference]. |
||
200 | * Example: |
||
201 | * |
||
202 | * ['/properties' => '#/definitions/b'] |
||
203 | * |
||
204 | * The path does NOT include the $ref. |
||
205 | * |
||
206 | * @param object $schema The schema to resolve references for. |
||
207 | 158 | * @param string $path The current schema path. |
|
208 | * |
||
209 | 158 | * @return array |
|
210 | */ |
||
211 | 158 | private function getReferences($schema, $path = '') |
|
240 | |||
241 | /** |
||
242 | * Push a segment onto the given path. |
||
243 | * |
||
244 | * @param string $path |
||
245 | 104 | * @param string $segment |
|
246 | * |
||
247 | 104 | * @return string |
|
248 | */ |
||
249 | private function pathPush($path, $segment) |
||
253 | |||
254 | /** |
||
255 | * @param string $attribute |
||
256 | 158 | * @param mixed $attributeValue |
|
257 | * |
||
258 | 158 | * @return bool |
|
259 | */ |
||
260 | private function isRef($attribute, $attributeValue) |
||
264 | |||
265 | /** |
||
266 | 50 | * @param string $value |
|
267 | * |
||
268 | 50 | * @return bool |
|
269 | */ |
||
270 | private function isInternalRef($value) |
||
274 | |||
275 | /** |
||
276 | 50 | * @param string $value |
|
277 | * |
||
278 | 50 | * @return bool |
|
279 | */ |
||
280 | private function isExternalRef($value) |
||
284 | |||
285 | /** |
||
286 | * Load an external ref and return the JSON object. |
||
287 | * |
||
288 | 40 | * @param string $reference |
|
289 | * |
||
290 | 40 | * @return object |
|
291 | 38 | */ |
|
292 | private function loadExternalRef($reference) |
||
303 | |||
304 | /** |
||
305 | * Merge a resolved reference into the root of the given schema. |
||
306 | 16 | * |
|
307 | * @param object $rootSchema |
||
308 | 16 | * @param object $resolvedRef |
|
309 | 16 | */ |
|
310 | 16 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
318 | |||
319 | /** |
||
320 | 40 | * Validate an absolute path is valid. |
|
321 | * |
||
322 | 40 | * @param string $path |
|
323 | 2 | */ |
|
324 | 2 | private function validateAbsolutePath($path) |
|
336 | |||
337 | /** |
||
338 | * Take a relative reference, and prepend the id of the schema and any |
||
339 | * sub schemas to get the absolute url. |
||
340 | * |
||
341 | 20 | * @param object $schema |
|
342 | * @param string $path |
||
343 | 20 | * @param string $ref |
|
344 | * @param string|null $currentUri |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
||
380 | } |
||
381 |