Complex classes like AbstractForm 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 AbstractForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class AbstractForm { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var Collection; |
||
| 16 | */ |
||
| 17 | protected $fields; |
||
| 18 | |||
| 19 | protected $configPath; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $config; |
||
| 25 | |||
| 26 | protected $values; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Request |
||
| 30 | */ |
||
| 31 | protected $request; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Custom error messages |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $messages = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MessageBag |
||
| 41 | */ |
||
| 42 | protected $messageBag; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Custom rules (callback or method name) |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $customRules = []; |
||
| 49 | |||
| 50 | protected $customFieldRules = []; |
||
| 51 | |||
| 52 | public function __construct() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Reads config from yaml file |
||
| 59 | * @param null $configPath |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | public function loadFromConfigYaml($configPath = null) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Loads fields from array |
||
| 77 | * @param array $config |
||
| 78 | * @throws \Exception |
||
| 79 | */ |
||
| 80 | public function loadFromConfigArray($config) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets field from collection |
||
| 96 | * @param $fieldName |
||
| 97 | * @return Fields\FieldsInterface |
||
| 98 | */ |
||
| 99 | public function get($fieldName) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Gets title of field |
||
| 105 | * @param $fieldName |
||
| 106 | */ |
||
| 107 | public function title($fieldName) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns all fields |
||
| 113 | * @return Collection |
||
| 114 | */ |
||
| 115 | public function all() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets values for all fields from passed array or object (eg. model). |
||
| 121 | * @param array|Arrayable $values |
||
| 122 | * @return null |
||
| 123 | */ |
||
| 124 | public function values($values) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Validates all fields with Laravel and custom rules |
||
| 141 | * @param Request $request |
||
| 142 | * @param array $passedRules |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function validate(Request $request, $passedRules = []) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Run validate() before this |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function passed() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns messages generated during validation |
||
| 197 | * @return MessageBag |
||
| 198 | */ |
||
| 199 | public function messages() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns response for browser |
||
| 205 | */ |
||
| 206 | public function response($redirect = null) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Calls custom rule which can be an callback or private method |
||
| 216 | * @param $rule |
||
| 217 | * @param $request |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | protected function callCustomRules($rule, $request) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Gets titles of attributes from fields (used in error messages in validation) |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function getAttributeNames() { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Add custom callable rule to field |
||
| 252 | * @param string $fieldName |
||
| 253 | * @param $rule |
||
| 254 | * @return AbstractForm |
||
| 255 | */ |
||
| 256 | public function customFieldRule($fieldName, $rule) { |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Collects rules from all fields |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function collectRules() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Makes new field from passed class |
||
| 283 | * @param string $fieldName |
||
| 284 | * @param string $fieldClass |
||
| 285 | * @param array $args |
||
| 286 | * @return Fields\FieldsInterface |
||
| 287 | */ |
||
| 288 | public function make($fieldName, $fieldClass, $args = []) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Calls requested element from fields array or creates it |
||
| 295 | * @param string $method |
||
| 296 | * @param mixed $args |
||
| 297 | * @return mixed|Fields\FieldsInterface |
||
| 298 | * @throws \Exception |
||
| 299 | */ |
||
| 300 | public function __call($method, $args) { |
||
| 321 | |||
| 322 | } |