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 | 148 | 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 | 148 | public function dereference($schema) |
|
47 | |||
48 | /** |
||
49 | * Register a Loader for the given prefix. |
||
50 | * |
||
51 | * @param Loader $loader |
||
52 | * @param string $prefix |
||
53 | 120 | */ |
|
54 | 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 | 30 | */ |
|
67 | private function getLoader($prefix) |
||
75 | |||
76 | /** |
||
77 | * Register the default file loader. |
||
78 | 148 | */ |
|
79 | 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 | 148 | */ |
|
90 | 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 | 148 | * @return object |
|
108 | */ |
||
109 | 148 | 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 | 148 | * @return array |
|
164 | */ |
||
165 | 148 | private function getReferences($schema, $path = '') |
|
192 | 148 | ||
193 | /** |
||
194 | * Push a segment onto the given path. |
||
195 | * |
||
196 | * @param string $path |
||
197 | * @param string $segment |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | private function pathPush($path, $segment) |
||
205 | 98 | ||
206 | /** |
||
207 | * @param string $attribute |
||
208 | * @param mixed $attributeValue |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | private function isRef($attribute, $attributeValue) |
||
216 | 148 | ||
217 | /** |
||
218 | * @param string $parameter |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | private function isInternalRef($parameter) |
||
226 | 38 | ||
227 | /** |
||
228 | * @param string $parameter |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | private function isExternalRef($parameter) |
||
236 | 38 | ||
237 | /** |
||
238 | * Load an external ref and return the JSON object. |
||
239 | * |
||
240 | * @param string $reference |
||
241 | * |
||
242 | * @return object |
||
243 | */ |
||
244 | private function loadExternalRef($reference) |
||
255 | 30 | ||
256 | /** |
||
257 | * Merge a resolved reference into the root of the given schema. |
||
258 | * |
||
259 | * @param object $rootSchema |
||
260 | * @param object $resolvedRef |
||
261 | */ |
||
262 | private function mergeRootRef($rootSchema, $resolvedRef) |
||
270 | 12 | ||
271 | 12 | /** |
|
272 | * Validate an absolute path is valid. |
||
273 | * |
||
274 | * @param string $path |
||
275 | */ |
||
276 | private function validateAbsolutePath($path) |
||
284 | |||
285 | 30 | /** |
|
286 | * Determine if a reference is relative. |
||
287 | * A reference is relative if it does not being with a prefix. |
||
288 | * |
||
289 | * @param string $ref |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | private function isRelativeRef($ref) |
||
297 | 12 | ||
298 | /** |
||
299 | * Take a relative reference, and prepend the id of the schema and any |
||
300 | * sub schemas to get the absolute url. |
||
301 | * |
||
302 | * @param object $schema |
||
303 | * @param string $path |
||
304 | * @param string $ref |
||
305 | * @param string|null $currentUri |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
||
346 | } |
||
347 |
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: