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 | 158 | 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 | 158 | public function dereference($schema) |
|
| 38 | { |
||
| 39 | 158 | if (is_string($schema)) { |
|
| 40 | 24 | $uri = $schema; |
|
| 41 | 24 | $schema = $this->loadExternalRef($uri); |
|
| 42 | 24 | $schema = $this->resolveFragment($uri, $schema); |
|
| 43 | |||
| 44 | 24 | return $this->crawl($schema, strip_fragment($uri)); |
|
| 45 | } |
||
| 46 | |||
| 47 | 134 | 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 | public function getLoaders() |
||
| 70 | |||
| 71 | 38 | /** |
|
| 72 | * Get the loader for the given prefix. |
||
| 73 | * |
||
| 74 | * @param $prefix |
||
| 75 | 38 | * |
|
| 76 | * @return Loader |
||
| 77 | * @throws \InvalidArgumentException |
||
| 78 | */ |
||
| 79 | private function getLoader($prefix) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Register the default file loader. |
||
| 90 | */ |
||
| 91 | private function registerFileLoader() |
||
| 95 | 158 | ||
| 96 | 158 | /** |
|
| 97 | 158 | * 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 | 158 | */ |
|
| 102 | private function registerDefaultWebLoaders() |
||
| 112 | |||
| 113 | 158 | /** |
|
| 114 | * Crawl the schema and resolve any references. |
||
| 115 | 158 | * |
|
| 116 | * @param object $schema |
||
| 117 | 48 | * @param string|null $currentUri |
|
| 118 | 20 | * |
|
| 119 | 18 | * @return object |
|
| 120 | 42 | */ |
|
| 121 | private function crawl($schema, $currentUri = null) |
||
| 144 | 20 | ||
| 145 | 20 | /** |
|
| 146 | * Resolve the external reference at the given path. |
||
| 147 | 18 | * |
|
| 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 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
||
| 162 | 16 | ||
| 163 | 8 | /** |
|
| 164 | 8 | * Merge the resolved reference with the schema, at the given path. |
|
| 165 | 16 | * |
|
| 166 | 16 | * @param object $schema The schema to merge the resolved reference with |
|
| 167 | 44 | * @param object $resolved The resolved schema |
|
| 168 | 44 | * @param string $path A JSON pointer to the path where the reference should be merged. |
|
| 169 | 44 | * |
|
| 170 | 44 | * @return void |
|
| 171 | */ |
||
| 172 | 46 | private function mergeResolvedReference($schema, $resolved, $path) |
|
| 187 | 10 | ||
| 188 | /** |
||
| 189 | * Check if the reference contains a fragment and resolve |
||
| 190 | 48 | * the pointer. Otherwise returns the original schema. |
|
| 191 | * |
||
| 192 | * @param string $ref |
||
| 193 | * @param object $schema |
||
| 194 | * |
||
| 195 | * @return object |
||
| 196 | */ |
||
| 197 | private function resolveFragment($ref, $schema) |
||
| 210 | |||
| 211 | 158 | /** |
|
| 212 | 40 | * Recursively get all of the references for the given schema. |
|
| 213 | * Returns an associative array like [path => reference]. |
||
| 214 | * Example: |
||
| 215 | 158 | * |
|
| 216 | 158 | * ['/properties' => '#/definitions/b'] |
|
| 217 | 158 | * |
|
| 218 | 48 | * The path does NOT include the $ref. |
|
| 219 | 48 | * |
|
| 220 | 156 | * @param object $schema The schema to resolve references for. |
|
| 221 | 90 | * @param string $path The current schema path. |
|
| 222 | 90 | * |
|
| 223 | 154 | * @return array |
|
| 224 | 62 | */ |
|
| 225 | 58 | private function getReferences($schema, $path = '') |
|
| 254 | |||
| 255 | /** |
||
| 256 | 158 | * Push a segment onto the given path. |
|
| 257 | * |
||
| 258 | 158 | * @param string $path |
|
| 259 | * @param string $segment |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | private function pathPush($path, $segment) |
||
| 267 | |||
| 268 | 50 | /** |
|
| 269 | * @param string $attribute |
||
| 270 | * @param mixed $attributeValue |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | private function isRef($attribute, $attributeValue) |
||
| 278 | 50 | ||
| 279 | /** |
||
| 280 | * @param string $value |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | private function isInternalRef($value) |
||
| 288 | 40 | ||
| 289 | /** |
||
| 290 | 40 | * @param string $value |
|
| 291 | 38 | * |
|
| 292 | * @return bool |
||
| 293 | 38 | */ |
|
| 294 | 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 | 16 | private function loadExternalRef($reference) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Merge a resolved reference into the root of the given schema. |
||
| 320 | 40 | * |
|
| 321 | * @param object $rootSchema |
||
| 322 | 40 | * @param object $resolvedRef |
|
| 323 | 2 | */ |
|
| 324 | 2 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Validate an absolute path is valid. |
||
| 335 | * |
||
| 336 | * @param string $path |
||
| 337 | */ |
||
| 338 | 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 | 20 | * @param string $ref |
|
| 358 | * @param string|null $currentUri |
||
| 359 | 20 | * |
|
| 360 | 14 | * @return string |
|
| 361 | */ |
||
| 362 | 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 | 12 | * |
|
| 379 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
||
| 380 | 12 | * |
|
| 381 | * @param object $schema |
||
| 382 | * @param string $path |
||
| 383 | * @param string $scope |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | private function getResolvedResolutionScope($schema, $path, $scope) |
||
| 406 | } |
||
| 407 |