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:
Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Validation; |
||
| 53 | class Validator implements ValidatorInterface |
||
| 54 | { |
||
| 55 | use Captures, Compares, Converters, ExpressionsX, Generics, Types, Values, Wrappers; |
||
| 56 | |||
| 57 | /** Rule description index */ |
||
| 58 | const RULE_INDEX = 0; |
||
| 59 | |||
| 60 | /** Rule description index */ |
||
| 61 | const RULE_ATTRIBUTES = self::RULE_INDEX + 1; |
||
| 62 | |||
| 63 | /** Rule description index */ |
||
| 64 | const RULE_TO_ONE = self::RULE_ATTRIBUTES + 1; |
||
| 65 | |||
| 66 | /** Rule description index */ |
||
| 67 | const RULE_TO_MANY = self::RULE_TO_ONE + 1; |
||
| 68 | |||
| 69 | /** Rule description index */ |
||
| 70 | const RULE_UNLISTED_ATTRIBUTE = self::RULE_TO_MANY + 1; |
||
| 71 | |||
| 72 | /** Rule description index */ |
||
| 73 | const RULE_UNLISTED_RELATIONSHIP = self::RULE_UNLISTED_ATTRIBUTE + 1; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ContainerInterface |
||
| 77 | */ |
||
| 78 | private $container; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var SchemaInterface|null |
||
| 82 | */ |
||
| 83 | private $schema = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $jsonType; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var RuleInterface[] |
||
| 92 | */ |
||
| 93 | private $rules; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | private $errorStatus; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var null|ErrorCollection |
||
| 102 | */ |
||
| 103 | private $errorCollection = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var null|CaptureAggregatorInterface |
||
| 107 | */ |
||
| 108 | private $captureAggregator = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param ContainerInterface $container |
||
| 112 | * @param string $jsonType |
||
| 113 | * @param RuleInterface[] $rules |
||
| 114 | * @param int $errorStatus |
||
| 115 | */ |
||
| 116 | public function __construct( |
||
| 117 | ContainerInterface $container, |
||
| 118 | string $jsonType, |
||
| 119 | array $rules, |
||
| 120 | $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
||
| 121 | ) { |
||
| 122 | if (array_key_exists(static::RULE_UNLISTED_ATTRIBUTE, $rules) === false) { |
||
| 123 | $rules[static::RULE_UNLISTED_ATTRIBUTE] = static::fail(); |
||
| 124 | } |
||
| 125 | if (array_key_exists(static::RULE_UNLISTED_RELATIONSHIP, $rules) === false) { |
||
| 126 | $rules[static::RULE_UNLISTED_RELATIONSHIP] = static::fail(); |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->container = $container; |
||
| 130 | $this->jsonType = $jsonType; |
||
| 131 | $this->rules = $rules; |
||
| 132 | $this->errorStatus = $errorStatus; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritdoc |
||
| 137 | */ |
||
| 138 | public function assert(array $jsonData): ValidatorInterface |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | public function check(array $jsonData): bool |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function getErrors(): ErrorCollection |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | public function getCaptures(): array |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | protected function resetErrors() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return ErrorCollection |
||
| 195 | */ |
||
| 196 | protected function createErrorCollection(): ErrorCollection |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected function getJsonType(): string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return ContainerInterface |
||
| 215 | */ |
||
| 216 | protected function getContainer(): ContainerInterface |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return ModelSchemeInfoInterface |
||
| 223 | */ |
||
| 224 | protected function getModelSchemes(): ModelSchemeInfoInterface |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return SchemaInterface |
||
| 231 | */ |
||
| 232 | protected function getSchema(): SchemaInterface |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return RuleInterface[] |
||
| 245 | */ |
||
| 246 | protected function getRules(): array |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | protected function getErrorStatus(): int |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return CaptureAggregatorInterface |
||
| 261 | */ |
||
| 262 | protected function createCaptureAggregator(): CaptureAggregatorInterface |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return ErrorAggregatorInterface |
||
| 269 | */ |
||
| 270 | protected function createErrorAggregator(): ErrorAggregatorInterface |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return CaptureAggregatorInterface |
||
| 277 | */ |
||
| 278 | public function getCaptureAggregator(): CaptureAggregatorInterface |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function resetCaptureAggregator() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $jsonData |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | private function validateType(array $jsonData) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param RuleInterface $rule |
||
| 314 | * @param mixed $input |
||
| 315 | * |
||
| 316 | * @return Generator |
||
| 317 | */ |
||
| 318 | protected function validateRule(RuleInterface $rule, $input): Generator |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $jsonData |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | private function validateId(array $jsonData) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $jsonData |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | private function validateAttributes(array $jsonData) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | private function createRelationshipCaptures(): array |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param array $jsonData |
||
| 449 | * @param array $relationshipCaptures |
||
| 450 | * |
||
| 451 | * @return void |
||
| 452 | */ |
||
| 453 | private function validateRelationshipCaptures(array $jsonData, array $relationshipCaptures) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param RuleInterface $typeRule |
||
| 465 | * @param RuleInterface $idRule |
||
| 466 | * |
||
| 467 | * @return RuleInterface |
||
| 468 | */ |
||
| 469 | private function createOptionalIdentity(RuleInterface $typeRule, RuleInterface $idRule): RuleInterface |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $name |
||
| 479 | * @param RuleInterface $typeRule |
||
| 480 | * @param RuleInterface $idRule |
||
| 481 | * |
||
| 482 | * @return RuleInterface |
||
| 483 | */ |
||
| 484 | private function createSingleData($name, RuleInterface $typeRule, RuleInterface $idRule): RuleInterface |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $name |
||
| 496 | * @param RuleInterface $typeRule |
||
| 497 | * @param RuleInterface $idRule |
||
| 498 | * |
||
| 499 | * @return RuleInterface |
||
| 500 | */ |
||
| 501 | private function createMultiData(string $name, RuleInterface $typeRule, RuleInterface $idRule): RuleInterface |
||
| 509 | } |
||
| 510 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.