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 | /** |
||
| 113 | * Sets values for all fields from passed array or object (eg. model). |
||
| 114 | * @param array|Arrayable $values |
||
| 115 | * @return null |
||
| 116 | */ |
||
| 117 | public function values($values) { |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Validates all fields with Laravel and custom rules |
||
| 135 | * @param Request $request |
||
| 136 | * @param array $passedRules |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function validate(Request $request, $passedRules = []) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Run validate() before this |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function passed() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns messages generated during validation |
||
| 191 | * @return MessageBag |
||
| 192 | */ |
||
| 193 | public function messages() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns response for browser |
||
| 199 | */ |
||
| 200 | public function response($redirect = null) { |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Calls custom rule which can be an callback or private method |
||
| 210 | * @param $rule |
||
| 211 | * @param $request |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | protected function callCustomRules($rule, $request) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets titles of attributes from fields (used in error messages in validation) |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getAttributeNames() { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Add custom callable rule to field |
||
| 246 | * @param string $fieldName |
||
| 247 | * @param $rule |
||
| 248 | * @return AbstractForm |
||
| 249 | */ |
||
| 250 | public function customFieldRule($fieldName, $rule) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Collects rules from all fields |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function collectRules() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Makes new field from passed class |
||
| 277 | * @param string $fieldName |
||
| 278 | * @param string $fieldClass |
||
| 279 | * @param array $args |
||
| 280 | * @return Fields\FieldsInterface |
||
| 281 | */ |
||
| 282 | public function make($fieldName, $fieldClass, $args = []) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Calls requested element from fields array or creates it |
||
| 289 | * @param string $method |
||
| 290 | * @param mixed $args |
||
| 291 | * @return mixed|Fields\FieldsInterface |
||
| 292 | * @throws \Exception |
||
| 293 | */ |
||
| 294 | public function __call($method, $args) { |
||
| 315 | |||
| 316 | } |