Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 29 | class Resolver |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Schema resolution stack. Each item on the stack is an array |
||
| 33 | * containing an uri and a schema. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $stack = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Schema cache. Each schema visited at a given URI is stored |
||
| 41 | * in the cache to avoid superfluous requests. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $schemas = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @see setPreFetchHook |
||
| 49 | * |
||
| 50 | * @var Closure |
||
| 51 | */ |
||
| 52 | private $preFetchHook; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns whether a base schema has been set. |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 299 | public function hasBaseSchema() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Sets the current schema, on which resolutions will be based. |
||
| 66 | * |
||
| 67 | * @param stdClass $schema |
||
| 68 | * @param Uri $uri |
||
| 69 | */ |
||
| 70 | 336 | public function setBaseSchema(stdClass $schema, Uri $uri) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Clears current schema. |
||
| 78 | * |
||
| 79 | * @throws EmptyStackException |
||
| 80 | */ |
||
| 81 | 299 | public function clearBaseSchema() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Returns the URI of the current schema. |
||
| 92 | * |
||
| 93 | * @return Uri |
||
| 94 | * |
||
| 95 | * @throws EmptyStackException |
||
| 96 | */ |
||
| 97 | 61 | View Code Duplication | public function getCurrentUri() |
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the current schema. |
||
| 108 | * |
||
| 109 | * @return stdClass |
||
| 110 | * |
||
| 111 | * @throws EmptyStackException |
||
| 112 | */ |
||
| 113 | 8 | View Code Duplication | public function getCurrentSchema() |
| 121 | |||
| 122 | /** |
||
| 123 | * Sets an URI pre-fetch hook. The hook function will be called each time |
||
| 124 | * a remote reference is about to be fetched. It is passed the original |
||
| 125 | * pointer URI and must return a new URI string. |
||
| 126 | * |
||
| 127 | * @param Closure $preFetchHook |
||
| 128 | */ |
||
| 129 | 300 | public function setPreFetchHook(Closure $preFetchHook) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Pushes an URI and its associated schema onto the resolution stack, |
||
| 136 | * making them the current URI/schema pair. If no schema is passed, the |
||
| 137 | * current schema is reused (useful when entering a resolution scope |
||
| 138 | * within the current schema). |
||
| 139 | * |
||
| 140 | * @param Uri $uri |
||
| 141 | * @param stdClass $schema |
||
| 142 | * |
||
| 143 | * @throws EmptyStackException |
||
| 144 | */ |
||
| 145 | 23 | public function enter(Uri $uri, stdClass $schema = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Removes the URI/schema pair at the top of the resolution stack, |
||
| 158 | * thus returning to the previous URI/schema context. |
||
| 159 | * |
||
| 160 | * @throws EmptyStackException |
||
| 161 | */ |
||
| 162 | 24 | public function leave() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Resolves a schema reference according to the JSON Reference |
||
| 173 | * specification draft. Returns an array containing the resolved |
||
| 174 | * URI and the resolved schema. |
||
| 175 | * |
||
| 176 | * @param stdClass $reference |
||
| 177 | * |
||
| 178 | * @throws InvalidPointerTargetException |
||
| 179 | * @throws SelfReferencingPointerException |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | 59 | public function resolve(stdClass $reference) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Caches a schema reference for future use. |
||
| 216 | * |
||
| 217 | * @param stdClass $schema |
||
| 218 | * @param Uri $uri |
||
| 219 | */ |
||
| 220 | 336 | private function registerSchema(stdClass $schema, Uri $uri) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Fetches a remote schema and ensures it is valid. |
||
| 229 | * |
||
| 230 | * @param string $uri |
||
| 231 | * |
||
| 232 | * @throws InvalidRemoteSchemaException |
||
| 233 | * @throws JsonDecodeException |
||
| 234 | * |
||
| 235 | * @return stdClass |
||
| 236 | */ |
||
| 237 | 23 | private function fetchSchemaAt($uri) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Resolves a JSON pointer according to RFC 6901. |
||
| 270 | * |
||
| 271 | * @param stdClass $schema |
||
| 272 | * @param Uri $pointerUri |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | * |
||
| 276 | * @throws InvalidPointerIndexException |
||
| 277 | * @throws InvalidSegmentTypeException |
||
| 278 | * @throws UnresolvedPointerIndexException |
||
| 279 | * @throws UnresolvedPointerPropertyException |
||
| 280 | */ |
||
| 281 | 52 | private function resolvePointer(stdClass $schema, Uri $pointerUri) |
|
| 315 | } |
||
| 316 |