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 | 166 | 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 | 164 | public function dereference($schema) |
|
38 | { |
||
39 | 164 | if (is_string($schema)) { |
|
40 | 28 | $uri = $schema; |
|
41 | 28 | $schema = $this->loadExternalRef($uri); |
|
42 | 26 | $schema = $this->resolveFragment($uri, $schema); |
|
43 | |||
44 | 26 | return $this->crawl($schema, strip_fragment($uri)); |
|
45 | } |
||
46 | |||
47 | 136 | 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 all registered loaders, keyed by the prefix they are registered to load schemas for. |
||
63 | * |
||
64 | * @return Loader[] |
||
65 | */ |
||
66 | 2 | public function getLoaders() |
|
67 | { |
||
68 | 2 | return $this->loaders; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get the loader for the given prefix. |
||
73 | * |
||
74 | * @param $prefix |
||
75 | * |
||
76 | * @return Loader |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | 44 | private function getLoader($prefix) |
|
80 | { |
||
81 | 44 | if (!array_key_exists($prefix, $this->loaders)) { |
|
82 | 2 | throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix)); |
|
83 | } |
||
84 | |||
85 | 42 | return $this->loaders[$prefix]; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Register the default file loader. |
||
90 | */ |
||
91 | 166 | private function registerFileLoader() |
|
92 | { |
||
93 | 166 | $this->loaders['file'] = new FileLoader(); |
|
94 | 166 | } |
|
95 | |||
96 | /** |
||
97 | * Register the default web loaders. If the curl extension is loaded, |
||
98 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
99 | * will be used. You can override this by registering your own loader |
||
100 | * for the 'http' and 'https' protocols. |
||
101 | */ |
||
102 | 166 | private function registerDefaultWebLoaders() |
|
103 | { |
||
104 | 166 | if (function_exists('curl_init')) { |
|
105 | 166 | $this->loaders['https'] = new CurlWebLoader('https://'); |
|
106 | 166 | $this->loaders['http'] = new CurlWebLoader('http://'); |
|
107 | 166 | } else { |
|
108 | $this->loaders['https'] = new FileGetContentsWebLoader('https://'); |
||
109 | $this->loaders['http'] = new FileGetContentsWebLoader('http://'); |
||
110 | } |
||
111 | 166 | } |
|
112 | |||
113 | /** |
||
114 | * Crawl the schema and resolve any references. |
||
115 | * |
||
116 | * @param object $schema |
||
117 | * @param string|null $currentUri |
||
118 | * |
||
119 | * @return object |
||
120 | */ |
||
121 | 162 | private function crawl($schema, $currentUri = null) |
|
122 | { |
||
123 | 162 | $references = $this->getReferences($schema); |
|
124 | |||
125 | 162 | foreach ($references as $path => $ref) { |
|
126 | // resolve |
||
127 | 52 | if ($this->isExternalRef($ref)) { |
|
128 | 24 | $resolved = new Reference(function () use ($schema, $path, $ref, $currentUri) { |
|
129 | 24 | return $this->resolveExternalReference($schema, $path, $ref, $currentUri); |
|
130 | 24 | }, $ref); |
|
131 | 24 | } else { |
|
132 | 42 | $resolved = new Reference($schema, $ref); |
|
133 | } |
||
134 | |||
135 | // handle any fragments |
||
136 | 52 | $resolved = $this->resolveFragment($ref, $resolved); |
|
137 | |||
138 | // merge |
||
139 | 52 | $this->mergeResolvedReference($schema, $resolved, $path); |
|
140 | 160 | } |
|
141 | |||
142 | 160 | return $schema; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Resolve the external reference at the given path. |
||
147 | * |
||
148 | * @param object $schema The JSON Schema |
||
149 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
150 | * @param string $ref The JSON reference |
||
151 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
152 | * |
||
153 | * @return object The schema with the reference resolved. |
||
154 | */ |
||
155 | 24 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
|
156 | { |
||
157 | 24 | $ref = $this->makeReferenceAbsolute($schema, $path, $ref, $currentUri); |
|
158 | 24 | $resolved = $this->loadExternalRef($ref); |
|
159 | |||
160 | 22 | return $this->crawl($resolved, strip_fragment($ref)); |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Merge the resolved reference with the schema, at the given path. |
||
165 | * |
||
166 | * @param object $schema The schema to merge the resolved reference with |
||
167 | * @param object $resolved The resolved schema |
||
168 | * @param string $path A JSON pointer to the path where the reference should be merged. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | 52 | private function mergeResolvedReference($schema, $resolved, $path) |
|
173 | { |
||
174 | 52 | if ($path === '') { |
|
175 | // Immediately resolve any root references. |
||
176 | 18 | while ($resolved instanceof Reference) { |
|
177 | 18 | $resolved = $resolved->resolve(); |
|
178 | 16 | } |
|
179 | 16 | $this->mergeRootRef($schema, $resolved); |
|
180 | 16 | } else { |
|
181 | 48 | $pointer = new Pointer($schema); |
|
182 | 48 | if ($pointer->has($path)) { |
|
183 | 48 | $pointer->set($path, $resolved); |
|
184 | 48 | } |
|
185 | } |
||
186 | 50 | } |
|
187 | |||
188 | /** |
||
189 | * Check if the reference contains a fragment and resolve |
||
190 | * the pointer. Otherwise returns the original schema. |
||
191 | * |
||
192 | * @param string $ref |
||
193 | * @param object $schema |
||
194 | * |
||
195 | * @return object |
||
196 | */ |
||
197 | 54 | private function resolveFragment($ref, $schema) |
|
198 | { |
||
199 | 54 | $fragment = parse_url($ref, PHP_URL_FRAGMENT); |
|
200 | 54 | if ($this->isExternalRef($ref) && is_string($fragment)) { |
|
201 | 10 | if ($schema instanceof Reference) { |
|
202 | 6 | $schema = $schema->resolve(); |
|
203 | 6 | } |
|
204 | 10 | $pointer = new Pointer($schema); |
|
205 | 10 | return $pointer->get($fragment); |
|
206 | } |
||
207 | |||
208 | 54 | return $schema; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Recursively get all of the references for the given schema. |
||
213 | * Returns an associative array like [path => reference]. |
||
214 | * Example: |
||
215 | * |
||
216 | * ['/properties' => '#/definitions/b'] |
||
217 | * |
||
218 | * The path does NOT include the $ref. |
||
219 | * |
||
220 | * @param object $schema The schema to resolve references for. |
||
221 | * @param string $path The current schema path. |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | 162 | private function getReferences($schema, $path = '') |
|
226 | { |
||
227 | 162 | $refs = []; |
|
228 | |||
229 | 162 | if (!is_array($schema) && !is_object($schema)) { |
|
230 | 40 | return $refs; |
|
231 | } |
||
232 | |||
233 | 162 | foreach ($schema as $attribute => $parameter) { |
|
234 | 162 | switch (true) { |
|
235 | 162 | case $this->isRef($attribute, $parameter): |
|
236 | 52 | $refs[$path] = $parameter; |
|
237 | 52 | break; |
|
238 | 160 | case is_object($parameter): |
|
239 | 94 | $refs = array_merge($refs, $this->getReferences($parameter, $this->pathPush($path, $attribute))); |
|
240 | 94 | break; |
|
241 | 158 | case is_array($parameter): |
|
242 | 62 | foreach ($parameter as $k => $v) { |
|
243 | 58 | $refs = array_merge( |
|
244 | 58 | $refs, |
|
245 | 58 | $this->getReferences($v, $this->pathPush($this->pathPush($path, $attribute), $k)) |
|
246 | 58 | ); |
|
247 | 62 | } |
|
248 | 62 | break; |
|
249 | } |
||
250 | 162 | } |
|
251 | |||
252 | 162 | return $refs; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Push a segment onto the given path. |
||
257 | * |
||
258 | * @param string $path |
||
259 | * @param string $segment |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | 108 | private function pathPush($path, $segment) |
|
264 | { |
||
265 | 108 | return $path . '/' . escape_pointer($segment); |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param string $attribute |
||
270 | * @param mixed $attributeValue |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | 162 | private function isRef($attribute, $attributeValue) |
|
275 | { |
||
276 | 162 | return $attribute === '$ref' && is_string($attributeValue); |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param string $value |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 54 | private function isInternalRef($value) |
|
285 | { |
||
286 | 54 | return is_string($value) && substr($value, 0, 1) === '#'; |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param string $value |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 54 | private function isExternalRef($value) |
|
298 | |||
299 | /** |
||
300 | * Load an external ref and return the JSON object. |
||
301 | * |
||
302 | * @param string $reference |
||
303 | * |
||
304 | * @return object |
||
305 | */ |
||
306 | 46 | private function loadExternalRef($reference) |
|
307 | { |
||
308 | 46 | $this->validateAbsolutePath($reference); |
|
309 | 44 | list($prefix, $path) = explode('://', $reference, 2); |
|
317 | |||
318 | /** |
||
319 | * Merge a resolved reference into the root of the given schema. |
||
320 | * |
||
321 | * @param object $rootSchema |
||
322 | * @param object $resolvedRef |
||
323 | */ |
||
324 | 16 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
332 | |||
333 | /** |
||
334 | * Validate an absolute path is valid. |
||
335 | * |
||
336 | * @param string $path |
||
337 | */ |
||
338 | 46 | private function validateAbsolutePath($path) |
|
350 | |||
351 | /** |
||
352 | * Take a relative reference, and prepend the id of the schema and any |
||
353 | * sub schemas to get the absolute url. |
||
354 | * |
||
355 | * @param object $schema |
||
356 | * @param string $path |
||
357 | * @param string $ref |
||
358 | * @param string|null $currentUri |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | 24 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
|
374 | |||
375 | /** |
||
376 | * Get the resolved resolution scope by walking the schema and resolving |
||
377 | * every `id` against the most immediate parent scope. |
||
378 | * |
||
379 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
||
380 | * |
||
381 | * @param object $schema |
||
382 | * @param string $path |
||
383 | * @param string $scope |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | 16 | private function getResolvedResolutionScope($schema, $path, $scope) |
|
406 | } |
||
407 |