Complex classes like FormValidatorExecutor 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 FormValidatorExecutor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class FormValidatorExecutor |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var FormValidatorDataObject |
||
| 38 | */ |
||
| 39 | protected $dataObject; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var FormObject |
||
| 43 | */ |
||
| 44 | protected $formObject; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var FormResult |
||
| 48 | */ |
||
| 49 | protected $result; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ConditionProcessor |
||
| 53 | */ |
||
| 54 | private $conditionProcessor; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $fieldsActivationChecked = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Current queue, used to prevent infinite loop. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $fieldsActivationChecking = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $fieldsValidated = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Array of arbitrary data which are handled by validators. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $validationData = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var PhpConditionDataObject |
||
| 82 | */ |
||
| 83 | protected $phpConditionDataObject; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param FormObject $formObject |
||
| 87 | * @param FormValidatorDataObject $dataObject |
||
| 88 | */ |
||
| 89 | public function __construct(FormObject $formObject, FormValidatorDataObject $dataObject) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return FormValidatorExecutor |
||
| 100 | */ |
||
| 101 | public function applyBehaviours() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * This function will take care of deactivating the validation for fields |
||
| 112 | * that do not match their activation condition. |
||
| 113 | * |
||
| 114 | * @return FormValidatorExecutor |
||
| 115 | */ |
||
| 116 | public function checkFieldsActivation() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param Field $field |
||
| 129 | */ |
||
| 130 | protected function checkFieldActivation(Field $field) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Field $field |
||
| 157 | */ |
||
| 158 | protected function checkFieldValidatorActivation(Field $field) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return FormValidatorExecutor |
||
| 171 | */ |
||
| 172 | public function validateFields() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param Field $field |
||
| 183 | */ |
||
| 184 | protected function launchFieldValidation(Field $field) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Will loop on each validator and apply it on the field. The validation |
||
| 197 | * result is merged with the form result. |
||
| 198 | * |
||
| 199 | * @param Field $field |
||
| 200 | */ |
||
| 201 | public function validateField(Field $field) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Field $field |
||
| 228 | * @param Validator $validator |
||
| 229 | * @return Result |
||
| 230 | */ |
||
| 231 | protected function processFieldValidator(Field $field, Validator $validator) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Loops on registered callbacks that should be called after the given field |
||
| 268 | * validation. |
||
| 269 | * |
||
| 270 | * @param Field $field |
||
| 271 | */ |
||
| 272 | protected function callFieldValidationCallback(Field $field) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return FormResult |
||
| 285 | */ |
||
| 286 | public function getResult() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param Field $field |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | protected function getFieldActivationProcessResult(Field $field) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param Validator $validator |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | protected function getValidatorActivationProcessResult(Validator $validator) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param Field $field |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | protected function fieldActivationHasBeenChecked(Field $field) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param Field $field |
||
| 320 | */ |
||
| 321 | protected function markFieldActivationAsChecked(Field $field) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param Field $field |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | protected function fieldActivationIsBeingChecked(Field $field) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param Field $field |
||
| 337 | */ |
||
| 338 | protected function markFieldActivationCheckBegin(Field $field) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param Field $field |
||
| 345 | */ |
||
| 346 | protected function markFieldActivationCheckEnd(Field $field) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param Field $field |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | protected function fieldWasValidated(Field $field) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param Field $field |
||
| 362 | */ |
||
| 363 | protected function markFieldAsValidated(Field $field) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return ConditionProcessor |
||
| 370 | */ |
||
| 371 | protected function getConditionProcessor() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return PhpConditionDataObject |
||
| 378 | */ |
||
| 379 | protected function getPhpConditionDataObject() |
||
| 383 | } |
||
| 384 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.