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 | * Initializes the resolver with a root schema, on which resolutions will be based. |
||
| 56 | * |
||
| 57 | * @param stdClass $schema |
||
| 58 | * @param Uri $uri |
||
| 59 | */ |
||
| 60 | 336 | public function initialize(stdClass $schema, Uri $uri) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Returns the URI of the current schema. |
||
| 68 | * |
||
| 69 | * @return Uri |
||
| 70 | * |
||
| 71 | * @throws EmptyStackException |
||
| 72 | */ |
||
| 73 | 61 | View Code Duplication | public function getCurrentUri() |
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the current schema. |
||
| 84 | * |
||
| 85 | * @return stdClass |
||
| 86 | * |
||
| 87 | * @throws EmptyStackException |
||
| 88 | */ |
||
| 89 | 8 | View Code Duplication | public function getCurrentSchema() |
| 97 | |||
| 98 | /** |
||
| 99 | * Sets an URI pre-fetch hook. The hook function will be called each time |
||
| 100 | * a remote reference is about to be fetched. It is passed the original |
||
| 101 | * pointer URI and must return a new URI string. |
||
| 102 | * |
||
| 103 | * @param Closure $preFetchHook |
||
| 104 | */ |
||
| 105 | 300 | public function setPreFetchHook(Closure $preFetchHook) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Pushes an URI and its associated schema onto the resolution stack, |
||
| 112 | * making them the current URI/schema pair. If no schema is passed, the |
||
| 113 | * current schema is reused (useful when entering a resolution scope |
||
| 114 | * within the current schema). |
||
| 115 | * |
||
| 116 | * @param Uri $uri |
||
| 117 | * @param stdClass $schema |
||
| 118 | * |
||
| 119 | * @throws EmptyStackException |
||
| 120 | */ |
||
| 121 | 23 | public function enter(Uri $uri, stdClass $schema = null) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Removes the URI/schema pair at the top of the resolution stack, |
||
| 134 | * thus returning to the previous URI/schema context. |
||
| 135 | * |
||
| 136 | * @throws EmptyStackException |
||
| 137 | */ |
||
| 138 | 24 | public function leave() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Resolves a schema reference according to the JSON Reference |
||
| 149 | * specification draft. Returns an array containing the resolved |
||
| 150 | * URI and the resolved schema. |
||
| 151 | * |
||
| 152 | * @param stdClass $reference |
||
| 153 | * |
||
| 154 | * @throws InvalidPointerTargetException |
||
| 155 | * @throws SelfReferencingPointerException |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 59 | public function resolve(stdClass $reference) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Caches a schema reference for future use. |
||
| 192 | * |
||
| 193 | * @param stdClass $schema |
||
| 194 | * @param Uri $uri |
||
| 195 | */ |
||
| 196 | 336 | private function registerSchema(stdClass $schema, Uri $uri) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Fetches a remote schema and ensures it is valid. |
||
| 205 | * |
||
| 206 | * @param string $uri |
||
| 207 | * |
||
| 208 | * @throws InvalidRemoteSchemaException |
||
| 209 | * @throws JsonDecodeException |
||
| 210 | * |
||
| 211 | * @return stdClass |
||
| 212 | */ |
||
| 213 | 23 | private function fetchSchemaAt($uri) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Resolves a JSON pointer according to RFC 6901. |
||
| 246 | * |
||
| 247 | * @param stdClass $schema |
||
| 248 | * @param Uri $pointerUri |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | * |
||
| 252 | * @throws InvalidPointerIndexException |
||
| 253 | * @throws InvalidSegmentTypeException |
||
| 254 | * @throws UnresolvedPointerIndexException |
||
| 255 | * @throws UnresolvedPointerPropertyException |
||
| 256 | */ |
||
| 257 | 52 | private function resolvePointer(stdClass $schema, Uri $pointerUri) |
|
| 291 | } |
||
| 292 |