Complex classes like Volan 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 Volan, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Volan |
||
| 15 | { |
||
| 16 | |||
| 17 | use ErrorHandlerTrait; |
||
| 18 | use LoggerTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * If set to false all required fields set can be empty |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private $requiredMode = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * If set to false allows excessive keys in array. Default is true |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $strictMode; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $params = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $currentNode = ''; |
||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $schema = []; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $schema |
||
| 51 | * @param bool $strictMode |
||
| 52 | */ |
||
| 53 | public function __construct($schema, $strictMode = true) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets required mode |
||
| 64 | * |
||
| 65 | * @param bool $mode |
||
| 66 | */ |
||
| 67 | public function setRequiredMode($mode = true) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets custom params |
||
| 74 | * |
||
| 75 | * @param array $arr |
||
| 76 | * @return void |
||
| 77 | * |
||
| 78 | */ |
||
| 79 | public function setParams($arr = []) |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * @param array $arr |
||
| 87 | * |
||
| 88 | * @return ValidatorResult |
||
| 89 | */ |
||
| 90 | public function validate($arr) |
||
| 112 | |||
| 113 | public function getCurrentNode() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $node |
||
| 120 | * @param CustomArrayObject $schema |
||
| 121 | * @param mixed $element |
||
| 122 | * |
||
| 123 | * @throws Exception |
||
| 124 | */ |
||
| 125 | private function validateNode($node, CustomArrayObject $schema, $element = []) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param CustomArrayObject $nodeSchema |
||
| 172 | * @param array $nodeData |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | private function isChildElementHasStrictKeys(CustomArrayObject $nodeSchema, $nodeData) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param AbstractValidator $validator |
||
| 191 | * @param CustomArrayObject $schema |
||
| 192 | * @param mixed $nodeData |
||
| 193 | * |
||
| 194 | */ |
||
| 195 | private function validateExcessiveKeys(AbstractValidator $validator, CustomArrayObject $schema, $nodeData = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param CustomArrayObject $schema |
||
| 212 | * @param mixed $nodeData |
||
| 213 | * |
||
| 214 | * @throws Exception |
||
| 215 | */ |
||
| 216 | private function validateExcessiveKeysAbsent($schema, $nodeData) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $node |
||
| 225 | * |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | private function validateTypeFieldIsPresent($node) |
||
| 229 | { |
||
| 230 | if (empty($node['_type'])) { |
||
| 231 | throw new Exception("Element: {$this->currentNode} has no compulsory field: _type", ValidatorResult::ERROR_NODE_HAS_NO_FIELD_TYPE); |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->getLogger()->info("Element: {$this->currentNode} has field: _type"); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | |||
| 239 | * @param mixed $nodeData |
||
| 240 | * |
||
| 241 | * @throws Exception |
||
| 242 | */ |
||
| 243 | private function validateRequiredFieldIsPresent($nodeData = null) |
||
| 244 | { |
||
| 245 | if (is_null($nodeData)) { |
||
| 246 | throw new Exception("{$this->currentNode} element has flag *required*", ValidatorResult::ERROR_REQUIRED_FIELD_IS_EMPTY); |
||
| 247 | } |
||
| 248 | |||
| 249 | $this->getLogger()->info('*required* check passed'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param array $node |
||
| 254 | * |
||
| 255 | * @return AbstractValidator |
||
| 256 | * |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | private function getClassValidator($node) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Validate that schema begins with root element |
||
| 283 | * |
||
| 284 | * @param CustomArrayObject $schema |
||
| 285 | * |
||
| 286 | * @throws Exception |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | private function validateSchemaBeginsWithRootKey(CustomArrayObject $schema) |
||
| 296 | /* |
||
| 297 | * Converts string constisting _ to PSR compatible class name |
||
| 298 | * |
||
| 299 | * @param string |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | private function getPSRCompatibleClassName($string) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param AbstractValidator $validator |
||
| 317 | * @param mixed $nodeData |
||
| 318 | * |
||
| 319 | * @throws Exception |
||
| 320 | */ |
||
| 321 | private function validateNodeValue(AbstractValidator $validator, $nodeData = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param AbstractValidator $validator |
||
| 335 | * @param mixed $nodeData |
||
| 336 | * |
||
| 337 | * @throws Exception |
||
| 338 | */ |
||
| 339 | private function validateNesting(AbstractValidator $validator, $nodeData) |
||
| 345 | } |
||
| 346 |