| Conditions | 36 |
| Paths | 62 |
| Total Lines | 101 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 103 | public static function validate(array $validators): int |
||
| 104 | { |
||
| 105 | $validate = 0; |
||
| 106 | foreach ($validators as $key => $value) { |
||
| 107 | |||
| 108 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
||
| 109 | |||
| 110 | $v = $valuee; |
||
| 111 | |||
| 112 | if(is_array($valuee)){ |
||
| 113 | $v = null; |
||
| 114 | foreach ($valuee as $vvv) { |
||
| 115 | $v .= $vvv; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $valuee = $v; |
||
| 120 | |||
| 121 | if(!array_key_exists($keyy, $validators)){ |
||
| 122 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
||
| 123 | } |
||
| 124 | |||
| 125 | if($keyy===$key){ |
||
| 126 | |||
| 127 | $validate++; |
||
| 128 | |||
| 129 | foreach ($value as $subkey => $subvalue) { |
||
| 130 | |||
| 131 | switch ($subkey) { |
||
| 132 | case 'minlength': |
||
| 133 | if(array_key_exists('required', $value)){ |
||
| 134 | if($value['required'] or strlen($valuee)!==0){ |
||
| 135 | if(strlen($valuee)===0){ |
||
| 136 | throw new Exception("O campo '{$key}' é obrigatório.",1); |
||
| 137 | } |
||
| 138 | |||
| 139 | if(strlen($valuee) < (int) $subvalue){ |
||
| 140 | throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | break; |
||
| 145 | |||
| 146 | case 'type': |
||
| 147 | if(array_key_exists('required', $value)){ |
||
| 148 | if($value['required'] or strlen($valuee)!==0){ |
||
| 149 | switch ($subvalue) { |
||
| 150 | case 'date': |
||
| 151 | $date = explode('/', $valuee); |
||
| 152 | if(count($date) != 3){ |
||
| 153 | throw new Exception('Data inválida.',1); |
||
| 154 | } |
||
| 155 | if(! checkdate( intval($date[1]), intval($date[0]), intval($date[2]) )){ |
||
| 156 | throw new Exception('Data inválida.',1); |
||
| 157 | } |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | break; |
||
| 163 | |||
| 164 | case 'maxlength': |
||
| 165 | if(array_key_exists('required', $value)){ |
||
| 166 | if($value['required'] or strlen($valuee)!==0){ |
||
| 167 | if(strlen($valuee)>(int)$subvalue){ |
||
| 168 | throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'regex': |
||
| 175 | if(array_key_exists('required', $value)){ |
||
| 176 | if($value['required'] or strlen($valuee)!==0){ |
||
| 177 | if(!@preg_match($subvalue,$valuee)){ |
||
| 178 | throw new Exception("{$key} inválido(a).",1); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | break; |
||
| 183 | |||
| 184 | case 'equals': |
||
| 185 | $equals = false; |
||
| 186 | foreach (self::$data as $ke => $sub) { |
||
| 187 | if($ke===$subvalue){ |
||
| 188 | $equals=true; |
||
| 189 | if($valuee !== $sub){ |
||
| 190 | throw new Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | if(!$equals){ |
||
| 195 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
||
| 196 | } |
||
| 197 | break; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | return $validate; |
||
| 204 | } |
||
| 240 |