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 |
||
13 | trait ModelValidator |
||
14 | { |
||
15 | protected $_badAttr; |
||
16 | protected $_sendMethod = 'POST'; |
||
17 | |||
18 | protected $_formName; |
||
19 | |||
20 | public function runValidate(array $rules = null) |
||
57 | |||
58 | /** |
||
59 | * @param $field_name |
||
60 | * @param $filter_name |
||
61 | * @param $filter_argv |
||
62 | * @param bool $html |
||
63 | * @param bool $secure |
||
64 | * @return bool |
||
65 | * @throws SyntaxException |
||
66 | */ |
||
67 | public function validateRecursive($field_name, $filter_name, $filter_argv, $html = false, $secure = false) |
||
68 | { |
||
69 | // check if we got it from form defined request method |
||
70 | if (App::$Request->getMethod() !== $this->_sendMethod) { |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | $inputTypes = []; |
||
75 | // check input data type. Maybe file or input (text) |
||
76 | if (method_exists($this, 'inputTypes')) { |
||
77 | $inputTypes = $this->inputTypes(); |
||
78 | } |
||
79 | |||
80 | // sounds like file |
||
81 | if ($inputTypes[$field_name] === 'file') { |
||
82 | $field_value = $this->getRequest($field_name, 'file'); |
||
83 | } else { // sounds like plain post data |
||
84 | $field_value = $this->getRequest($field_name, $this->_sendMethod); |
||
85 | // remove or safe use html |
||
86 | if ($html === false) { |
||
87 | $field_value = App::$Security->strip_tags($field_value); |
||
|
|||
88 | } else { |
||
89 | if ($secure !== true) { |
||
90 | $field_value = App::$Security->secureHtml($field_value); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $check = false; |
||
96 | // maybe no filter required? |
||
97 | if ($filter_name === 'used') { |
||
98 | $check = true; |
||
99 | } elseif (Str::contains('::', $filter_name)) { // sounds like a callback class::method::method |
||
100 | // string to array via delimiter :: |
||
101 | $callbackArray = explode('::', $filter_name); |
||
102 | // first item is a class name |
||
103 | $class = array_shift($callbackArray); |
||
104 | // last item its a function |
||
105 | $method = array_pop($callbackArray); |
||
106 | // left any items? maybe post-static callbacks? |
||
107 | if (count($callbackArray) > 0) { |
||
108 | foreach ($callbackArray as $obj) { |
||
109 | if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) { // sounds like a variable |
||
110 | $obj = ltrim($obj, '$'); // trim variable symbol '$' |
||
111 | $class = $class::$$obj; // make magic :) |
||
112 | } elseif (method_exists($class, $obj)) { // maybe its a function? |
||
113 | $class = $class::$obj; // call function |
||
114 | } else { |
||
115 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
116 | } |
||
117 | |||
118 | } |
||
119 | } |
||
120 | |||
121 | // check is endpoint method exist |
||
122 | if (method_exists($class, $method)) { |
||
123 | $check = @$class::$method($field_value, $filter_argv); |
||
124 | } else { |
||
125 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
126 | } |
||
127 | } elseif (method_exists('Ffcms\Core\Filter\Native', $filter_name)) { // only full namespace\class path based :( |
||
128 | if ($filter_argv != null) { |
||
129 | $check = Native::$filter_name($field_value, $filter_argv); |
||
130 | } else { |
||
131 | $check = Native::$filter_name($field_value); |
||
132 | } |
||
133 | } else { |
||
134 | throw new SyntaxException('Filter "' . $filter_name . '" is not exist'); |
||
135 | } |
||
136 | if ($check !== true) { // switch only on fail check. |
||
137 | $this->_badAttr[] = $field_name; |
||
138 | } else { |
||
139 | $field_set_name = $field_name; |
||
140 | // prevent array-type setting |
||
141 | if (Str::contains('.', $field_set_name)) { |
||
142 | $field_set_name = strstr($field_set_name, '.', true); |
||
143 | } |
||
144 | if (property_exists($this, $field_set_name)) { |
||
145 | if ($field_name !== $field_set_name) { // array-based property |
||
146 | $dot_path = trim(strstr($field_name, '.'), '.'); |
||
147 | // prevent throws any exceptions for null and false objects |
||
148 | if (!Obj::isArray($this->{$field_set_name})) { |
||
149 | $this->{$field_set_name} = []; |
||
150 | } |
||
151 | // use dot-data provider to compile output array |
||
152 | $dotData = new DotData($this->{$field_set_name}); |
||
153 | $dotData->set($dot_path, $field_value); // todo: check me!!! bug here |
||
154 | // export data from dot-data lib to model property |
||
155 | $this->{$field_set_name} = $dotData->export(); |
||
156 | } else { // just single property |
||
157 | $this->{$field_name} = $field_value; // refresh model property's from post data |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | return $check; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Get fail validation attributes as array if exist |
||
166 | * @return null|array |
||
167 | */ |
||
168 | public function getBadAttribute() |
||
172 | |||
173 | /** |
||
174 | * Set model send method type. Allowed: post, get |
||
175 | * @param string $acceptMethod |
||
176 | */ |
||
177 | final public function setSubmitMethod($acceptMethod) |
||
181 | |||
182 | /** |
||
183 | * Get model submit method. Allowed: post, get |
||
184 | * @return string |
||
185 | */ |
||
186 | final public function getSubmitMethod() |
||
190 | |||
191 | /** |
||
192 | * Check if model get POST-based request as submit of SEND data |
||
193 | * @return bool |
||
194 | */ |
||
195 | final public function send() |
||
203 | |||
204 | /** |
||
205 | * Form default name (used in field building) |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getFormName() |
||
217 | |||
218 | /** |
||
219 | * @deprecated |
||
220 | * Get input params GET/POST/PUT method |
||
221 | * @param string $param |
||
222 | * @return string|null |
||
223 | */ |
||
224 | public function getInput($param) |
||
228 | |||
229 | /** |
||
230 | * @deprecated |
||
231 | * Get uploaded file from user via POST request |
||
232 | * @param string $param |
||
233 | * @return \Symfony\Component\HttpFoundation\File\UploadedFile|null |
||
234 | */ |
||
235 | public function getFile($param) |
||
239 | |||
240 | /** |
||
241 | * Get input param for current model form based on param name and request method |
||
242 | * @param string $param |
||
243 | * @param string $method |
||
244 | * @return string|null|array |
||
245 | */ |
||
246 | public function getRequest($param, $method = 'get') |
||
272 | |||
273 | /** |
||
274 | * Set special type for input data. Example: ['avatar' => 'file', 'login' => 'input'] |
||
275 | * @return array |
||
276 | */ |
||
277 | public function inputTypes() |
||
281 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.