Complex classes like ModelValidator 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 ModelValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | trait ModelValidator |
||
19 | { |
||
20 | protected $_badAttr; |
||
21 | protected $_sendMethod = 'POST'; |
||
22 | |||
23 | protected $_formName; |
||
24 | |||
25 | /** |
||
26 | * Start validation for defined fields in rules() model method. |
||
27 | * @param array|null $rules |
||
28 | * @return bool |
||
29 | * @throws SyntaxException |
||
30 | */ |
||
31 | public function runValidate(array $rules = null) |
||
68 | |||
69 | /** |
||
70 | * Try to recursive validate field by defined rules and set result to model properties if validation is successful passed |
||
71 | * @param string|array $field_name |
||
72 | * @param string $filter_name |
||
73 | * @param mixed $filter_argv |
||
74 | * @param bool $html |
||
75 | * @param bool $secure |
||
76 | * @return bool |
||
77 | * @throws SyntaxException |
||
78 | */ |
||
79 | public function validateRecursive($field_name, $filter_name, $filter_argv, $html = false, $secure = false) |
||
157 | |||
158 | /** |
||
159 | * Get field value from input POST/GET/AJAX data with defined security level (html - safe html, !secure = fully unescaped) |
||
160 | * @param string $field_name |
||
161 | * @param bool $html |
||
162 | * @param bool $secure |
||
163 | * @return array|null|string |
||
164 | * @throws \InvalidArgumentException |
||
165 | */ |
||
166 | private function getFieldValue($field_name, $html = false, $secure = false) |
||
192 | |||
193 | /** |
||
194 | * Get fail validation attributes as array if exist |
||
195 | * @return null|array |
||
196 | */ |
||
197 | public function getBadAttribute() |
||
201 | |||
202 | /** |
||
203 | * Set model send method type. Allowed: post, get |
||
204 | * @param string $acceptMethod |
||
205 | */ |
||
206 | final public function setSubmitMethod($acceptMethod) |
||
210 | |||
211 | /** |
||
212 | * Get model submit method. Allowed: post, get |
||
213 | * @return string |
||
214 | */ |
||
215 | final public function getSubmitMethod() |
||
219 | |||
220 | /** |
||
221 | * Check if model get POST-based request as submit of SEND data |
||
222 | * @return bool |
||
223 | * @throws \InvalidArgumentException |
||
224 | */ |
||
225 | final public function send() |
||
233 | |||
234 | /** |
||
235 | * Form default name (used in field building) |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getFormName() |
||
247 | |||
248 | /** |
||
249 | * @deprecated |
||
250 | * Get input params GET/POST/PUT method |
||
251 | * @param string $param |
||
252 | * @return string|null |
||
253 | */ |
||
254 | public function getInput($param) |
||
258 | |||
259 | /** |
||
260 | * @deprecated |
||
261 | * Get uploaded file from user via POST request |
||
262 | * @param string $param |
||
263 | * @return \Symfony\Component\HttpFoundation\File\UploadedFile|null |
||
264 | */ |
||
265 | public function getFile($param) |
||
269 | |||
270 | /** |
||
271 | * Get input param for current model form based on param name and request method |
||
272 | * @param string $param |
||
273 | * @param string $method |
||
274 | * @return string|null|array |
||
275 | * @throws \InvalidArgumentException |
||
276 | */ |
||
277 | public function getRequest($param, $method = 'get') |
||
303 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.