Total Complexity | 48 |
Total Lines | 205 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
5 | class Validation |
||
6 | { |
||
7 | private $app; |
||
8 | |||
9 | private $input; |
||
10 | |||
11 | private $errors = []; |
||
12 | |||
13 | public function __construct(Application $app) |
||
14 | { |
||
15 | $this->app = $app; |
||
16 | } |
||
17 | |||
18 | public function input($input) |
||
19 | { |
||
20 | $this->input = $input; |
||
21 | |||
22 | return $this; |
||
23 | } |
||
24 | |||
25 | public function require($input = null, $msg = null) |
||
26 | { |
||
27 | if (! $this->input) $this->input = $input; |
||
28 | |||
29 | $value = $this->value($this->input); |
||
30 | |||
31 | if (! $value) { |
||
32 | |||
33 | $msg = $msg ?: 'This input is required'; |
||
34 | |||
35 | $this->addError($this->input, $msg); |
||
36 | } |
||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | public function email($input = null, $msg = null) |
||
53 | } |
||
54 | |||
55 | public function number($input = null, $msg = null) |
||
56 | { |
||
57 | if (! $this->input) $this->input = $input; |
||
58 | |||
59 | $value = $this->value($this->input); |
||
60 | |||
61 | if ($value) { |
||
62 | |||
63 | if (! is_numeric($value)) { |
||
64 | |||
65 | $msg = $msg ?: 'the Input must be number'; |
||
66 | |||
67 | $this->addError($this->input, $msg); |
||
68 | } |
||
69 | } |
||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | public function text($input = null, $msg = null) |
||
89 | } |
||
90 | |||
91 | public function minLen($length, $msg = null) |
||
105 | } |
||
106 | |||
107 | public function maxLen($length, $msg = null) |
||
108 | { |
||
109 | $value = $this->value($this->input); |
||
110 | |||
111 | if ($value) { |
||
112 | |||
113 | if (strlen($value) > $length) { |
||
114 | |||
115 | $msg = $msg ?: 'This must be ' . $length . ' or fewer'; |
||
116 | |||
117 | $this->addError($this->input, $msg); |
||
118 | } |
||
119 | } |
||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function match($input, $msg = null) |
||
139 | } |
||
140 | |||
141 | public function unique($data, $msg = null) |
||
170 | } |
||
171 | |||
172 | public function message($msg = null) |
||
173 | { |
||
174 | $this->errors[] = $msg; |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | public function passes() |
||
180 | { |
||
181 | return empty($this->errors); |
||
182 | } |
||
183 | |||
184 | public function fails() |
||
185 | { |
||
186 | return ! empty($this->errors); |
||
187 | } |
||
188 | |||
189 | public function getMsgs() |
||
190 | { |
||
191 | return $this->errors; |
||
192 | } |
||
193 | |||
194 | private function value($input) |
||
195 | { |
||
196 | return $this->app->request->post($input); |
||
1 ignored issue
–
show
|
|||
197 | } |
||
198 | |||
199 | public function addError($input, $msg) |
||
200 | { |
||
201 | if (! $this->checkError($input)) { |
||
202 | |||
203 | $this->errors[$input] = $msg; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | private function checkError($input) |
||
210 | } |
||
211 | } |