Complex classes like Validation 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 Validation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | trait Validation |
||
19 | { |
||
20 | /** |
||
21 | * Validation rules (GUMP rules array) |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $validationRules = []; |
||
26 | |||
27 | /** |
||
28 | * Initial validation rules (automatically copied from $validationRules when instantiated) |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $validationRulesDefault = []; |
||
33 | |||
34 | /** |
||
35 | * Filter rules (GUMP filters array) |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $filterRules = []; |
||
40 | |||
41 | /** |
||
42 | * Initial filter rules (automatically copied from $filterRules when instantiated) |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $filterRulesDefault = []; |
||
47 | |||
48 | /** |
||
49 | * Boolean flag from validation run results if object is valid |
||
50 | * |
||
51 | * @var boolean |
||
52 | */ |
||
53 | protected $valid = false; |
||
54 | |||
55 | /** |
||
56 | * Errors from last validation run |
||
57 | * |
||
58 | * @var bool|array |
||
59 | */ |
||
60 | protected $validationErrors; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * initialize |
||
65 | */ |
||
66 | public function __construct() |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Get filter rules array |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function filterRulesGet(): array |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Get default filter rules array |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function filterRulesDefault(): array |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Get validation rules array |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function validationRulesGet(): array |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Get default validation rules array |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function validationRulesDefault(): array |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Set filter rules from array |
||
120 | * |
||
121 | * @param array $rules |
||
122 | * @return array |
||
123 | */ |
||
124 | public function filterRules(array $rules = []): array |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Set validation rules from array |
||
134 | * |
||
135 | * @param array $rules |
||
136 | * @return array |
||
137 | */ |
||
138 | public function validationRules(array $rules = []): array |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Reset filter rules to default |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function filterReset(): array |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Reset validation rules to default |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function validationReset(): array |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Add extra filter rules from array |
||
174 | * |
||
175 | * @param array $rules |
||
176 | * @return array |
||
177 | */ |
||
178 | public function filterRulesAdd(array $rules = []): array |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Add extra validation rules from array |
||
188 | * |
||
189 | * @param array $rules |
||
190 | * @return array |
||
191 | */ |
||
192 | public function validationRulesAdd(array $rules = []): array |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Enforce validation required validation check on given fields |
||
202 | * or all fields if no array passed in |
||
203 | * |
||
204 | * @param array optional $fields |
||
205 | * @return array $validationRules |
||
206 | */ |
||
207 | public function validationRequired(array $fields = []): array |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Apply filter rules only |
||
234 | * |
||
235 | * @param array $data |
||
236 | * @param array $rules |
||
237 | * @return array $data |
||
238 | */ |
||
239 | public function filter(array $data = [], array $rules = []): array |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Filter, then validate |
||
253 | * |
||
254 | * @param boolean $run GUMP - call 'run' (return true/false) otherwise call 'validate' (return array of errors) |
||
255 | * @param array $data optional data array if different values to check outside of this mapper object fields |
||
256 | * @param array $validationRules |
||
257 | * @param array $filterRules |
||
258 | * @return boolean|array of validated data if 'run' otherwise array of errors or boolean if passed 'validate' |
||
259 | * @link https://github.com/Wixel/GUMP |
||
260 | */ |
||
261 | public function validate($run = true, array $data = [], array $validationRules = [], array $filterRules = []) |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Process errors of results from (return array of $this->validate(true)) $validator->run($data) into friendlier notifications |
||
286 | * |
||
287 | * @param mixed $errors errors from $validator->run($data) or get last errors |
||
288 | * @return array $notifications |
||
289 | */ |
||
290 | public function validationErrors($errors = []): array |
||
355 | } |
||
356 |