| Total Complexity | 42 |
| Total Lines | 141 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Check 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 Check, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | Trait Check{ |
||
| 8 | protected static array $data = []; |
||
| 9 | protected static array $validators = []; |
||
| 10 | protected static string $model = ''; |
||
| 11 | protected static array $required = []; |
||
| 12 | |||
| 13 | protected static function check_minlength(string $param, $value) |
||
| 14 | { |
||
| 15 | if(self::toNext($param,$value)){ |
||
| 16 | |||
| 17 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
||
| 18 | |||
| 19 | foreach($realval as $val){ |
||
| 20 | if($value > strlen($val)) { |
||
| 21 | throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.",1); |
||
| 22 | } |
||
| 23 | } |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | protected static function check_requireds() |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | protected static function check_regex(string $param, $value) |
||
| 35 | { |
||
| 36 | if(self::toNext($param,$value)){ |
||
| 37 | |||
| 38 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
||
| 39 | |||
| 40 | foreach($realval as $val){ |
||
| 41 | |||
| 42 | if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){ |
||
| 43 | throw new Exception("{$param} inválido(a).",1); |
||
| 44 | } |
||
| 45 | |||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | protected static function check_mincount(string $param, $value) |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | protected static function check_maxcount(string $param, $value) |
||
| 61 | { |
||
| 62 | if(self::toNext($param,$value)){ |
||
| 63 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
||
| 64 | if(count($array) > $value){ |
||
| 65 | throw new Exception("{$param} ultrapassou o esperado.",1); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | protected static function testArray(string $param, $value): ?array |
||
| 71 | { |
||
| 72 | if(!is_array($value)){ |
||
| 73 | throw new Exception("Era esperado um informação em array para {$param}."); |
||
| 74 | } |
||
| 75 | return $value; |
||
| 76 | } |
||
| 77 | |||
| 78 | protected static function check_equals(string $param, $value) |
||
| 79 | { |
||
| 80 | if(self::toNext($param,$value)){ |
||
| 81 | |||
| 82 | if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
||
| 83 | throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.",1); |
||
| 84 | } |
||
| 85 | |||
| 86 | if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
||
| 87 | throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value),1); |
||
| 88 | } |
||
| 89 | |||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | protected static function check_maxlength(string $param, $value) |
||
| 103 | } |
||
| 104 | |||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | protected static function check_type(string $param, $value) |
||
| 110 | { |
||
| 111 | if(self::toNext($param,$value)){ |
||
| 112 | |||
| 113 | switch ($value) { |
||
| 114 | case 'date': |
||
| 115 | if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){ |
||
| 116 | throw new Exception("{$param} não é uma data válida."); |
||
| 117 | } |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | protected static function check_filter(string $param, $value) |
||
| 129 | } |
||
| 130 | |||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public static function validateDate($date, $format = 'Y-m-d H:i:s') |
||
| 135 | { |
||
| 136 | $d = \DateTime::createFromFormat($format, $date); |
||
| 137 | return $d && $d->format($format) == $date; |
||
| 138 | } |
||
| 139 | |||
| 140 | protected static function check_required(string $param): bool |
||
| 143 | } |
||
| 144 | |||
| 145 | protected static function toNext(string $param, $value) |
||
| 148 | } |
||
| 149 | |||
| 150 | } |