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 | 84 | 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 | 84 | public function dereference($schema) |
|
46 | |||
47 | /** |
||
48 | * Register a Loader for the given prefix. |
||
49 | * |
||
50 | * @param Loader $loader |
||
51 | * @param string $prefix |
||
52 | */ |
||
53 | 62 | public function registerLoader(Loader $loader, $prefix) |
|
57 | |||
58 | /** |
||
59 | * Get the loader for the given prefix. |
||
60 | * |
||
61 | * @param $prefix |
||
62 | * |
||
63 | * @return Loader |
||
64 | * @throws \InvalidArgumentException |
||
65 | */ |
||
66 | 20 | private function getLoader($prefix) |
|
74 | |||
75 | /** |
||
76 | * Register the default file loader. |
||
77 | */ |
||
78 | 84 | private function registerFileLoader() |
|
82 | |||
83 | /** |
||
84 | * Register the default web loaders. If the curl extension is loaded, |
||
85 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
86 | * will be used. You can override this by registering your own loader |
||
87 | * for the 'http' and 'https' protocols. |
||
88 | */ |
||
89 | 84 | private function registerDefaultWebLoaders() |
|
99 | |||
100 | /** |
||
101 | * Crawl the schema and resolve any references. |
||
102 | * |
||
103 | * @param object $schema |
||
104 | * |
||
105 | * @return object |
||
106 | */ |
||
107 | 84 | private function crawl($schema) |
|
148 | |||
149 | /** |
||
150 | * Recursively get all of the references for the given schema. |
||
151 | * Returns an associative array like [path => reference]. |
||
152 | * Example: |
||
153 | * |
||
154 | * ['/properties' => '#/definitions/b'] |
||
155 | * |
||
156 | * The path does NOT include the $ref. |
||
157 | * |
||
158 | * @param object $schema The schema to resolve references for. |
||
159 | * @param string $path The current schema path. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 84 | private function getReferences($schema, $path = '') |
|
194 | |||
195 | /** |
||
196 | * Push a segment onto the given path. |
||
197 | * |
||
198 | * @param string $path |
||
199 | * @param string $segment |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 56 | private function pathPush($path, $segment) |
|
207 | |||
208 | /** |
||
209 | * Escape a JSON Pointer. |
||
210 | * |
||
211 | * @param string $pointer |
||
212 | * @return string |
||
213 | */ |
||
214 | 56 | private function escapePointer($pointer) |
|
219 | |||
220 | /** |
||
221 | * @param string $attribute |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | 84 | private function isRef($attribute) |
|
229 | |||
230 | /** |
||
231 | * @param string $parameter |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 30 | private function isInternalRef($parameter) |
|
239 | |||
240 | /** |
||
241 | * @param string $parameter |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 30 | private function isExternalRef($parameter) |
|
249 | |||
250 | /** |
||
251 | * Load an external ref and return the JSON object. |
||
252 | * |
||
253 | * @param string $reference |
||
254 | * |
||
255 | * @return object |
||
256 | */ |
||
257 | 20 | private function loadExternalRef($reference) |
|
268 | |||
269 | /** |
||
270 | * Merge a resolved reference into the root of the given schema. |
||
271 | * |
||
272 | * @param object $rootSchema |
||
273 | * @param object $resolvedRef |
||
274 | */ |
||
275 | 6 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
283 | |||
284 | /** |
||
285 | * Validate an absolute path is valid. |
||
286 | * |
||
287 | * @param string $path |
||
288 | */ |
||
289 | 20 | private function validateAbsolutePath($path) |
|
297 | |||
298 | /** |
||
299 | * Determine if a reference is relative. |
||
300 | * A reference is relative if it does not being with a prefix. |
||
301 | * |
||
302 | * @param string $ref |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | 6 | private function isRelativeRef($ref) |
|
310 | |||
311 | /** |
||
312 | * Take a relative reference, and prepend the id of the schema and any |
||
313 | * sub schemas to get the absolute url. |
||
314 | * |
||
315 | * @param object $schema |
||
316 | * @param string $path |
||
317 | * @param string $ref |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | 6 | private function makeReferenceAbsolute($schema, $path, $ref) |
|
340 | } |
||
341 |