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 | public function hasBaseSchema()  | 
            ||
| 60 |     { | 
            ||
| 61 | return count($this->stack) > 0;  | 
            ||
| 62 | }  | 
            ||
| 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 internal schema resolution stack.  | 
            ||
| 78 | */  | 
            ||
| 79 | 299 | public function clearStack()  | 
            |
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Returns the URI of the current schema.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @return Uri  | 
            ||
| 88 | *  | 
            ||
| 89 | * @throws EmptyStackException  | 
            ||
| 90 | */  | 
            ||
| 91 | 61 | View Code Duplication | public function getCurrentUri()  | 
            
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * Returns the current schema.  | 
            ||
| 102 | *  | 
            ||
| 103 | * @return stdClass  | 
            ||
| 104 | *  | 
            ||
| 105 | * @throws EmptyStackException  | 
            ||
| 106 | */  | 
            ||
| 107 | 8 | View Code Duplication | public function getCurrentSchema()  | 
            
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * Sets an URI pre-fetch hook. The hook function will be called each time  | 
            ||
| 118 | * a remote reference is about to be fetched. It is passed the original  | 
            ||
| 119 | * pointer URI and must return a new URI string.  | 
            ||
| 120 | *  | 
            ||
| 121 | * @param Closure $preFetchHook  | 
            ||
| 122 | */  | 
            ||
| 123 | 300 | public function setPreFetchHook(Closure $preFetchHook)  | 
            |
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Pushes an URI and its associated schema onto the resolution stack,  | 
            ||
| 130 | * making them the current URI/schema pair. If no schema is passed, the  | 
            ||
| 131 | * current schema is reused (useful when entering a resolution scope  | 
            ||
| 132 | * within the current schema).  | 
            ||
| 133 | *  | 
            ||
| 134 | * @param Uri $uri  | 
            ||
| 135 | * @param stdClass $schema  | 
            ||
| 136 | *  | 
            ||
| 137 | * @throws EmptyStackException  | 
            ||
| 138 | */  | 
            ||
| 139 | 23 | public function enter(Uri $uri, stdClass $schema = null)  | 
            |
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Removes the URI/schema pair at the top of the resolution stack,  | 
            ||
| 152 | * thus returning to the previous URI/schema context.  | 
            ||
| 153 | *  | 
            ||
| 154 | * @throws EmptyStackException  | 
            ||
| 155 | */  | 
            ||
| 156 | 24 | public function leave()  | 
            |
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Resolves a schema reference according to the JSON Reference  | 
            ||
| 167 | * specification draft. Returns an array containing the resolved  | 
            ||
| 168 | * URI and the resolved schema.  | 
            ||
| 169 | *  | 
            ||
| 170 | * @param stdClass $reference  | 
            ||
| 171 | *  | 
            ||
| 172 | * @throws InvalidPointerTargetException  | 
            ||
| 173 | * @throws SelfReferencingPointerException  | 
            ||
| 174 | *  | 
            ||
| 175 | * @return array  | 
            ||
| 176 | */  | 
            ||
| 177 | 59 | public function resolve(stdClass $reference)  | 
            |
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * Caches a schema reference for future use.  | 
            ||
| 210 | *  | 
            ||
| 211 | * @param stdClass $schema  | 
            ||
| 212 | * @param Uri $uri  | 
            ||
| 213 | */  | 
            ||
| 214 | 336 | private function registerSchema(stdClass $schema, Uri $uri)  | 
            |
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Fetches a remote schema and ensures it is valid.  | 
            ||
| 223 | *  | 
            ||
| 224 | * @param string $uri  | 
            ||
| 225 | *  | 
            ||
| 226 | * @throws InvalidRemoteSchemaException  | 
            ||
| 227 | * @throws JsonDecodeException  | 
            ||
| 228 | *  | 
            ||
| 229 | * @return stdClass  | 
            ||
| 230 | */  | 
            ||
| 231 | 23 | private function fetchSchemaAt($uri)  | 
            |
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Resolves a JSON pointer according to RFC 6901.  | 
            ||
| 264 | *  | 
            ||
| 265 | * @param stdClass $schema  | 
            ||
| 266 | * @param Uri $pointerUri  | 
            ||
| 267 | *  | 
            ||
| 268 | * @return mixed  | 
            ||
| 269 | *  | 
            ||
| 270 | * @throws InvalidPointerIndexException  | 
            ||
| 271 | * @throws InvalidSegmentTypeException  | 
            ||
| 272 | * @throws UnresolvedPointerIndexException  | 
            ||
| 273 | * @throws UnresolvedPointerPropertyException  | 
            ||
| 274 | */  | 
            ||
| 275 | 52 | private function resolvePointer(stdClass $schema, Uri $pointerUri)  | 
            |
| 309 | }  | 
            ||
| 310 |