Passed
Branch master (a7647a)
by refat
06:43 queued 03:32
created

Validation   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 73
c 6
b 0
f 0
dl 0
loc 210
rs 8.8798
wmc 44

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A input() 0 5 1
A email() 0 17 4
A require() 0 16 4
A text() 0 20 5
A minLen() 0 15 4
A match() 0 17 5
A number() 0 20 5
A maxLen() 0 15 4
A checkError() 0 3 1
A message() 0 5 1
A getMsgs() 0 3 1
A value() 0 3 1
A addError() 0 5 2
A fails() 0 3 1
A passes() 0 3 1
A unique() 0 16 3

How to fix   Complexity   

Complex Class

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
2
3
namespace System;
4
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) {
28
29
      $this->input = $input;
30
    }
31
32
    $value = $this->value($this->input);
33
34
    if (!$value) {
35
36
        $msg = $msg ?: 'This input is required';
37
38
        $this->addError($this->input, $msg);
39
    }
40
    return $this;
41
  }
42
43
  public function email($input = null, $msg = null)
44
  {
45
    if (!$this->input) {
46
47
      $this->input = $input;
48
    }
49
50
    $value = $this->value($this->input);
51
52
    if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
53
54
      $msg = $msg ?: sprintf('%s is not valid Email', ucfirst($this->input));
55
56
      $this->addError($this->input, $msg);
57
    }
58
59
    return $this;
60
  }
61
62
  public function number($input = null, $msg = null)
63
  {
64
    if (!$this->input) {
65
66
      $this->input = $input;
67
    }
68
69
    $value = $this->value($this->input);
70
71
    if ($value) {
72
73
      if (!is_numeric($value)) {
74
75
        $msg = $msg ?: 'the Input must be number';
76
77
        $this->addError($this->input, $msg);
78
      }
79
    }
80
81
    return $this;
82
  }
83
84
  public function text($input = null, $msg = null)
85
  {
86
    if (!$this->input) {
87
88
      $this->input = $input;
89
    }
90
91
    $value = $this->value($this->input);
92
93
    if ($value) {
94
95
      if (is_numeric($value)) {
96
97
        $msg = $msg ?: 'the Input must be Text';
98
99
        $this->addError($this->input, $msg);
100
      }
101
    }
102
103
    return $this;
104
  }
105
106
  public function minLen($length, $msg = null)
107
  {
108
    $value = $this->value($this->input);
109
110
    if ($value) {
111
112
      if (strlen($value) < $length) {
113
114
        $msg = $msg ?: 'This input must be at least ' . $length;
115
116
        $this->addError($this->input, $msg);
117
      }
118
    }
119
120
    return $this;
121
  }
122
123
  public function maxLen($length, $msg = null)
124
  {
125
    $value = $this->value($this->input);
126
127
    if ($value) {
128
129
      if (strlen($value) > $length) {
130
131
        $msg = $msg ?: 'This must be ' . $length . ' or fewer';
132
133
        $this->addError($this->input, $msg);
134
      }
135
    }
136
137
    return $this;
138
  }
139
140
  public function match($input, $msg = null)
141
  {
142
    $valuePassword = $this->value($this->input);
143
144
    $valueConfirm = $this->value($input);
145
146
    if ($valuePassword && $valueConfirm) {
147
148
      if ($valuePassword !== $valueConfirm) {
149
150
        $msg = $msg ?: 'Passwords does not match';
151
152
        $this->addError('match', $msg);
153
      }
154
    }
155
156
    return $this;
157
  }
158
159
  public function unique(array $data, $msg = null)
160
  {
161
    $value = $this->value($this->input);
162
163
    list($table, $column) = $data;
164
165
    $result = $this->app->db->select($column)->from($table)->where($column . ' = ? ', $value)->fetch();
166
167
    if ($result) {
168
169
      $msg = $msg ?: sprintf('%s is already exist', ucfirst($this->input));
170
171
      $this->addError($this->input, $msg);
172
    }
173
174
    return $this;
175
  }
176
177
  public function message($msg = null)
178
  {
179
    $this->errors[] = $msg;
180
181
    return $this;
182
  }
183
184
  public function passes()
185
  {
186
    return empty($this->errors);
187
  }
188
189
  public function fails()
190
  {
191
    return !empty($this->errors);
192
  }
193
194
  public function getMsgs()
195
  {
196
    return $this->errors;
197
  }
198
199
  private function value($input)
200
  {
201
    return $this->app->request->post($input);
202
  }
203
204
  public function addError($input, $msg)
205
  {
206
    if (!$this->checkError($input)) {
207
208
      $this->errors[$input] = $msg;
209
    }
210
  }
211
212
  private function checkError($input)
213
  {
214
    return array_key_exists($input, $this->errors);
215
  }
216
}