Complex classes like Properties 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 Properties, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Properties extends AbstractIterableValidator |
||
| 32 | { |
||
| 33 | const PROPERTIES_INDEX = 'properties'; |
||
| 34 | const PATTERN_PROPERTIES_INDEX = 'patternProperties'; |
||
| 35 | const ADDITIONAL_PROPERTIES_INDEX = 'additionalProperties'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
||
| 39 | */ |
||
| 40 | private $properties = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
| 44 | */ |
||
| 45 | private $additionalProperties = true; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
||
| 49 | */ |
||
| 50 | private $patternProperties = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | * |
||
| 55 | * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification() |
||
| 56 | */ |
||
| 57 | 107 | public function setSpecification($specification = null) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @param array $specification |
||
| 68 | * |
||
| 69 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 70 | */ |
||
| 71 | 104 | private function initAdditionalProperties(array $specification) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param array $specification |
||
| 80 | * |
||
| 81 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 82 | */ |
||
| 83 | 104 | private function initPatternProperties(array $specification) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $specification |
||
| 92 | * |
||
| 93 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 94 | */ |
||
| 95 | 106 | private function initProperties(array $specification) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | * |
||
| 105 | * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
||
| 106 | */ |
||
| 107 | 105 | public function validate($value) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param array $value |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 104 | private function executePropertiesValidation(array $value) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param array $rest |
||
| 131 | * |
||
| 132 | * @return bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface|bool |
||
| 133 | */ |
||
| 134 | 97 | private function validateAdditionalProperties(array $rest) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param array $value |
||
| 152 | * @param array $rest |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 99 | private function validateByPattern(array $value, array &$rest) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $value |
||
| 175 | * @param array $rest |
||
| 176 | * @param string $pattern |
||
| 177 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 12 | private function validateByPatternUsingValidator( |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $value |
||
| 200 | * @param array $rest |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | 104 | private function validateByProperty(array $value, array &$rest) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param \stdClass|array $value |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 104 | private function convertToArray($value) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Checks and sets items specification. |
||
| 234 | * |
||
| 235 | * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
| 236 | * |
||
| 237 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 238 | */ |
||
| 239 | 102 | private function setAdditionalProperties($specification) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Checks and sets pattern properties specification. |
||
| 250 | * |
||
| 251 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] $specification |
||
| 252 | * |
||
| 253 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 254 | */ |
||
| 255 | 99 | private function setProperties($specification) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Checks and sets pattern properties specification. |
||
| 271 | * |
||
| 272 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] $specification |
||
| 273 | * |
||
| 274 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
| 275 | */ |
||
| 276 | 93 | private function setPatternProperties($specification) |
|
| 289 | } |
||
| 290 |