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) |
|
46 | |||
47 | /** |
||
48 | * Register a Loader for the given prefix. |
||
49 | * |
||
50 | * @param Loader $loader |
||
51 | * @param string $prefix |
||
52 | */ |
||
53 | 120 | 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 | 30 | private function getLoader($prefix) |
|
74 | |||
75 | /** |
||
76 | * Register the default file loader. |
||
77 | */ |
||
78 | 148 | 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 | 148 | private function registerDefaultWebLoaders() |
|
99 | |||
100 | /** |
||
101 | * Crawl the schema and resolve any references. |
||
102 | * |
||
103 | * @param object $schema |
||
104 | * |
||
105 | * @return object |
||
106 | */ |
||
107 | 148 | 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 | 148 | private function getReferences($schema, $path = '') |
|
190 | 148 | ||
191 | /** |
||
192 | 148 | * Push a segment onto the given path. |
|
193 | * |
||
194 | * @param string $path |
||
195 | * @param string $segment |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | private function pathPush($path, $segment) |
||
203 | 98 | ||
204 | /** |
||
205 | 98 | * @param string $attribute |
|
206 | * @param mixed $attributeValue |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | private function isRef($attribute, $attributeValue) |
||
214 | 148 | ||
215 | /** |
||
216 | 148 | * @param string $parameter |
|
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | private function isInternalRef($parameter) |
||
224 | 38 | ||
225 | /** |
||
226 | 38 | * @param string $parameter |
|
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | private function isExternalRef($parameter) |
||
234 | 38 | ||
235 | /** |
||
236 | 38 | * Load an external ref and return the JSON object. |
|
237 | * |
||
238 | * @param string $reference |
||
239 | * |
||
240 | * @return object |
||
241 | */ |
||
242 | private function loadExternalRef($reference) |
||
253 | 30 | ||
254 | /** |
||
255 | 30 | * Merge a resolved reference into the root of the given schema. |
|
256 | * |
||
257 | * @param object $rootSchema |
||
258 | * @param object $resolvedRef |
||
259 | */ |
||
260 | private function mergeRootRef($rootSchema, $resolvedRef) |
||
268 | 12 | ||
269 | 12 | /** |
|
270 | 12 | * Validate an absolute path is valid. |
|
271 | 12 | * |
|
272 | * @param string $path |
||
273 | */ |
||
274 | private function validateAbsolutePath($path) |
||
282 | |||
283 | /** |
||
284 | * Determine if a reference is relative. |
||
285 | 30 | * A reference is relative if it does not being with a prefix. |
|
286 | * |
||
287 | * @param string $ref |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | private function isRelativeRef($ref) |
||
295 | 12 | ||
296 | /** |
||
297 | 12 | * Take a relative reference, and prepend the id of the schema and any |
|
298 | * sub schemas to get the absolute url. |
||
299 | * |
||
300 | * @param object $schema |
||
301 | * @param string $path |
||
302 | * @param string $ref |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | private function makeReferenceAbsolute($schema, $path, $ref) |
||
325 | } |
||
326 |