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 |
||
| 22 | class Walker |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Registry |
||
| 26 | */ |
||
| 27 | private $registry; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Resolver |
||
| 31 | */ |
||
| 32 | private $resolver; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var stdClass[] |
||
| 36 | */ |
||
| 37 | private $parsedSchemas = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var stdClass[] |
||
| 41 | */ |
||
| 42 | private $resolvedSchemas = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Constraint[][] |
||
| 46 | */ |
||
| 47 | private $constraintsCache = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param Registry $registry |
||
| 53 | * @param Resolver $resolver |
||
| 54 | */ |
||
| 55 | 485 | public function __construct(Registry $registry, Resolver $resolver) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Recursively resolve JSON pointer references within a given schema. |
||
| 63 | * |
||
| 64 | * @param stdClass $schema The schema to resolve |
||
| 65 | * @param Uri $uri The URI of the schema |
||
| 66 | * |
||
| 67 | * @return stdClass |
||
| 68 | */ |
||
| 69 | 299 | public function resolveReferences(stdClass $schema, Uri $uri) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param stdClass $schema |
||
| 78 | * @param Uri $uri |
||
| 79 | * |
||
| 80 | * @return stdClass |
||
| 81 | */ |
||
| 82 | 299 | private function doResolveReferences(stdClass $schema, Uri $uri) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Recursively normalizes a given schema and validates its syntax. |
||
| 122 | * |
||
| 123 | * @param stdClass $schema |
||
| 124 | * @param Context $context |
||
| 125 | * |
||
| 126 | * @return stdClass |
||
| 127 | */ |
||
| 128 | 357 | public function parseSchema(stdClass $schema, Context $context) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Validates an instance against a given schema, populating a context |
||
| 154 | * with encountered violations. |
||
| 155 | * |
||
| 156 | * @param mixed $instance |
||
| 157 | * @param stdClass $schema |
||
| 158 | * @param Context $context |
||
| 159 | */ |
||
| 160 | 348 | public function applyConstraints($instance, stdClass $schema, Context $context) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Checks if given schema has been already visited. |
||
| 192 | * |
||
| 193 | * @param stdClass $schema |
||
| 194 | * @param array $stack |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 363 | private function isLooping(stdClass $schema, array &$stack) |
|
| 208 | } |
||
| 209 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.