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 | 82 | 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 | 82 | 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 | 62 | 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 | 18 | private function getLoader($prefix) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Register the default file loader. |
||
| 77 | */ |
||
| 78 | 82 | 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 | 82 | private function registerDefaultWebLoaders() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Crawl the schema and resolve any references. |
||
| 102 | * |
||
| 103 | * @param object $schema |
||
| 104 | * |
||
| 105 | * @return object |
||
| 106 | */ |
||
| 107 | 82 | 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 | 82 | private function getReferences($schema, $path = '') |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Push a segment onto the given path. |
||
| 197 | * |
||
| 198 | * @param string $path |
||
| 199 | * @param string $segment |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 54 | private function pathPush($path, $segment) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $attribute |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 82 | private function isRef($attribute) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $parameter |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 28 | private function isInternalRef($parameter) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $parameter |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 28 | private function isExternalRef($parameter) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Load an external ref and return the JSON object. |
||
| 240 | * |
||
| 241 | * @param string $reference |
||
| 242 | * |
||
| 243 | * @return object |
||
| 244 | */ |
||
| 245 | 18 | private function loadExternalRef($reference) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Merge a resolved reference into the root of the given schema. |
||
| 259 | * |
||
| 260 | * @param object $rootSchema |
||
| 261 | * @param object $resolvedRef |
||
| 262 | */ |
||
| 263 | 6 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Validate an absolute path is valid. |
||
| 274 | * |
||
| 275 | * @param string $path |
||
| 276 | */ |
||
| 277 | 18 | private function validateAbsolutePath($path) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Determine if a reference is relative. |
||
| 288 | * A reference is relative if it does not being with a prefix. |
||
| 289 | * |
||
| 290 | * @param string $ref |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 6 | private function isRelativeRef($ref) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Take a relative reference, and prepend the id of the schema and any |
||
| 301 | * sub schemas to get the absolute url. |
||
| 302 | * |
||
| 303 | * @param object $schema |
||
| 304 | * @param string $path |
||
| 305 | * @param string $ref |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 6 | private function makeReferenceAbsolute($schema, $path, $ref) |
|
| 328 | } |
||
| 329 |