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 | 152 | 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 | 152 | public function dereference($schema) |
|
47 | |||
48 | /** |
||
49 | * Register a Loader for the given prefix. |
||
50 | * |
||
51 | * @param Loader $loader |
||
52 | * @param string $prefix |
||
53 | */ |
||
54 | 120 | public function registerLoader(Loader $loader, $prefix) |
|
58 | |||
59 | /** |
||
60 | * Get the loader for the given prefix. |
||
61 | * |
||
62 | * @param $prefix |
||
63 | * |
||
64 | * @return Loader |
||
65 | * @throws \InvalidArgumentException |
||
66 | */ |
||
67 | 32 | private function getLoader($prefix) |
|
75 | |||
76 | /** |
||
77 | * Register the default file loader. |
||
78 | */ |
||
79 | 152 | private function registerFileLoader() |
|
83 | |||
84 | /** |
||
85 | * Register the default web loaders. If the curl extension is loaded, |
||
86 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
87 | * will be used. You can override this by registering your own loader |
||
88 | * for the 'http' and 'https' protocols. |
||
89 | */ |
||
90 | 152 | private function registerDefaultWebLoaders() |
|
100 | |||
101 | /** |
||
102 | * Crawl the schema and resolve any references. |
||
103 | * |
||
104 | * @param object $schema |
||
105 | * @param string|null $currentUri |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | 152 | private function crawl($schema, $currentUri = null) |
|
150 | |||
151 | /** |
||
152 | * Recursively get all of the references for the given schema. |
||
153 | * Returns an associative array like [path => reference]. |
||
154 | * Example: |
||
155 | * |
||
156 | * ['/properties' => '#/definitions/b'] |
||
157 | * |
||
158 | * The path does NOT include the $ref. |
||
159 | * |
||
160 | * @param object $schema The schema to resolve references for. |
||
161 | * @param string $path The current schema path. |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | 152 | private function getReferences($schema, $path = '') |
|
196 | |||
197 | /** |
||
198 | * Push a segment onto the given path. |
||
199 | * |
||
200 | * @param string $path |
||
201 | * @param string $segment |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 100 | private function pathPush($path, $segment) |
|
209 | |||
210 | /** |
||
211 | * @param string $attribute |
||
212 | * @param mixed $attributeValue |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 152 | private function isRef($attribute, $attributeValue) |
|
220 | |||
221 | /** |
||
222 | * @param string $parameter |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 42 | private function isInternalRef($parameter) |
|
230 | |||
231 | /** |
||
232 | * @param string $parameter |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | 42 | private function isExternalRef($parameter) |
|
240 | |||
241 | /** |
||
242 | * Load an external ref and return the JSON object. |
||
243 | * |
||
244 | * @param string $reference |
||
245 | * |
||
246 | * @return object |
||
247 | */ |
||
248 | 34 | private function loadExternalRef($reference) |
|
259 | |||
260 | /** |
||
261 | * Merge a resolved reference into the root of the given schema. |
||
262 | * |
||
263 | * @param object $rootSchema |
||
264 | * @param object $resolvedRef |
||
265 | */ |
||
266 | 12 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
274 | |||
275 | /** |
||
276 | * Validate an absolute path is valid. |
||
277 | * |
||
278 | * @param string $path |
||
279 | */ |
||
280 | 34 | private function validateAbsolutePath($path) |
|
288 | |||
289 | /** |
||
290 | * Determine if a reference is relative. |
||
291 | * A reference is relative if it does not being with a prefix. |
||
292 | * |
||
293 | * @param string $ref |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | 16 | private function isRelativeRef($ref) |
|
301 | |||
302 | /** |
||
303 | * Take a relative reference, and prepend the id of the schema and any |
||
304 | * sub schemas to get the absolute url. |
||
305 | * |
||
306 | * @param object $schema |
||
307 | * @param string $path |
||
308 | * @param string $ref |
||
309 | * @param string|null $currentUri |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | 16 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
|
350 | } |
||
351 |
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: