Complex classes like InputFilter 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 InputFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class InputFilter extends \ArrayObject |
||
| 17 | { |
||
| 18 | use HasChildrenTrait; |
||
| 19 | use HasAttributesTrait; |
||
| 20 | use HasFiltersTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Uploaded files will have their names prefixed with this value |
||
| 24 | * A form element of type "file" with the name "picture" will upload |
||
| 25 | * the file into $_FILES['__upload_picture']. |
||
| 26 | * |
||
| 27 | * This is done to prevent collisions with hidden fields that |
||
| 28 | * might hold values from AJAX uploads. |
||
| 29 | */ |
||
| 30 | protected $uploadPrefix = '__upload_'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | protected $isInitialized = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | protected $isPrepared = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Data validation object |
||
| 44 | * |
||
| 45 | * @var \Sirius\Validation\Validator |
||
| 46 | */ |
||
| 47 | protected $validator; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Data filtrator object |
||
| 51 | * |
||
| 52 | * @var \Sirius\Filtration\Filtrator |
||
| 53 | */ |
||
| 54 | protected $filtrator; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The upload handlers that are registered withing this form |
||
| 58 | * |
||
| 59 | * @var HandlerAggregate |
||
| 60 | */ |
||
| 61 | protected $uploadHandlers; |
||
| 62 | |||
| 63 | protected $rawValues = array(); |
||
| 64 | |||
| 65 | protected $values = array(); |
||
| 66 | |||
| 67 | 33 | public function __construct( |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Initialize the form |
||
| 91 | * This is the place to put your definition (properties, elements) |
||
| 92 | * |
||
| 93 | * @return InputFilter |
||
| 94 | */ |
||
| 95 | 11 | public function init() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Returns whether the form was initialized or not |
||
| 107 | * |
||
| 108 | * @return boolean |
||
| 109 | */ |
||
| 110 | 11 | public function isInitialized() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Return the element factory |
||
| 117 | * |
||
| 118 | * @return ElementFactory |
||
| 119 | */ |
||
| 120 | 5 | public function getElementFactory() |
|
| 124 | |||
| 125 | 1 | public function setElementFactory() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get the prefix for elements that are of type upload |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 3 | public function getUploadPrefix() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Prepare the validator, filtrator and upload handlers objects |
||
| 142 | * |
||
| 143 | * @param bool $force force the execution of the preparation code even if already prepared |
||
| 144 | * |
||
| 145 | * @throws \LogicException |
||
| 146 | * @return InputFilter |
||
| 147 | */ |
||
| 148 | 11 | public function prepare($force = false) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Populates the filtrator object with the filtering rules |
||
| 193 | * associated with the InputFilter instance (not individual elements) |
||
| 194 | */ |
||
| 195 | 11 | protected function prepareFiltration() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Return whether the input is prepared or not |
||
| 212 | * |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | 4 | public function isPrepared() |
|
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the input's validator object |
||
| 223 | * |
||
| 224 | * @return \Sirius\Validation\Validator |
||
| 225 | */ |
||
| 226 | 11 | public function getValidator() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the filtrator object |
||
| 233 | * |
||
| 234 | * @return \Sirius\Filtration\Filtrator |
||
| 235 | */ |
||
| 236 | 11 | public function getFiltrator() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieves the upload handlers aggregate object |
||
| 243 | * |
||
| 244 | * @return UploadHandlerAggregate |
||
| 245 | */ |
||
| 246 | 5 | public function getUploadHandlers() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Sets a upload handler to be executed on files with a specific selector |
||
| 257 | * @example |
||
| 258 | * $form->setUploadHandler('resume', $resumeHandler); |
||
| 259 | * $form->setUploadHandler('pictures[*]', $pictureHandler); |
||
| 260 | * |
||
| 261 | * @param string $selector |
||
| 262 | * @param UploadHandler $handler |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | 1 | public function setUploadHandler($selector, UploadHandler $handler) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Populates a form with values. If you have uploads you must merge data from POST and FILES. |
||
| 275 | * |
||
| 276 | * @param array $values |
||
| 277 | * |
||
| 278 | * @throws \LogicException |
||
| 279 | */ |
||
| 280 | 4 | public function populate($values = array()) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Processes the uploaded files: |
||
| 293 | * 1. Validates the files |
||
| 294 | * 2. Confirms valid files |
||
| 295 | * 3. Add the upload errors to the validation errors |
||
| 296 | */ |
||
| 297 | 4 | protected function processUploads() |
|
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns whether the data is valid. |
||
| 324 | * If filters the data, process the uploads and performs the validation |
||
| 325 | * |
||
| 326 | * @param bool $skipDataProcessing skip filtration and upload handling |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 4 | public function isValid($skipDataProcessing = false) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get the errors from the validator object |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | 1 | public function getErrors() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | 1 | public function getValues() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param $name |
||
| 367 | * |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | 3 | public function getValue($name) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param $name |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | 1 | public function getRawValue($name) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | 1 | public function getRawValues() |
|
| 392 | } |
||
| 393 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..