| Total Complexity | 43 |
| Total Lines | 147 |
| 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 | protected static array $errors = []; |
||
| 13 | |||
| 14 | protected static function check_minlength(string $param, $value) |
||
| 15 | { |
||
| 16 | if(self::toNext($param,$value)){ |
||
| 17 | |||
| 18 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
||
| 19 | |||
| 20 | foreach($realval as $val){ |
||
| 21 | if($value > strlen($val)) { |
||
| 22 | throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.",1); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | protected static function check_errors(): bool |
||
| 29 | { |
||
| 30 | return (count(self::$errors) === 0); |
||
| 31 | } |
||
| 32 | |||
| 33 | protected static function check_requireds() |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | protected static function check_regex(string $param, $value) |
||
| 41 | { |
||
| 42 | if(self::toNext($param,$value)){ |
||
| 43 | |||
| 44 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
||
| 45 | |||
| 46 | foreach($realval as $val){ |
||
| 47 | |||
| 48 | if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){ |
||
| 49 | throw new Exception("{$param} inválido(a).",1); |
||
| 50 | } |
||
| 51 | |||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | protected static function check_mincount(string $param, $value) |
||
| 57 | { |
||
| 58 | if(self::toNext($param,$value)){ |
||
| 59 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
||
| 60 | if(count($array) < $value){ |
||
| 61 | throw new Exception("{$param} não atingiu o mínimo esperado.",1); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | protected static function check_maxcount(string $param, $value) |
||
| 67 | { |
||
| 68 | if(self::toNext($param,$value)){ |
||
| 69 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
||
| 70 | if(count($array) > $value){ |
||
| 71 | throw new Exception("{$param} ultrapassou o esperado.",1); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | protected static function testArray(string $param, $value): ?array |
||
| 77 | { |
||
| 78 | if(!is_array($value)){ |
||
| 79 | throw new Exception("Era esperado um informação em array para {$param}."); |
||
| 80 | } |
||
| 81 | return $value; |
||
| 82 | } |
||
| 83 | |||
| 84 | protected static function check_equals(string $param, $value) |
||
| 85 | { |
||
| 86 | if(self::toNext($param,$value)){ |
||
| 87 | |||
| 88 | if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
||
| 89 | throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.",1); |
||
| 90 | } |
||
| 91 | |||
| 92 | if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
||
| 93 | throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value),1); |
||
| 94 | } |
||
| 95 | |||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | protected static function check_maxlength(string $param, $value) |
||
| 100 | { |
||
| 101 | if(self::toNext($param,$value)){ |
||
| 102 | |||
| 103 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
||
| 104 | |||
| 105 | foreach($realval as $val){ |
||
| 106 | |||
| 107 | if($value < strlen($val)) { |
||
| 108 | throw new Exception("{$param} ultrapassou o máximo de caracteres esperado.",1); |
||
| 109 | } |
||
| 110 | |||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | protected static function check_type(string $param, $value) |
||
| 116 | { |
||
| 117 | if(self::toNext($param,$value)){ |
||
| 118 | |||
| 119 | switch ($value) { |
||
| 120 | case 'date': |
||
| 121 | if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){ |
||
| 122 | throw new Exception("{$param} não é uma data válida."); |
||
| 123 | } |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | protected static function check_filter(string $param, $value) |
||
| 135 | } |
||
| 136 | |||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public static function validateDate($date, $format = 'Y-m-d H:i:s') |
||
| 141 | { |
||
| 142 | $d = \DateTime::createFromFormat($format, $date); |
||
| 143 | return $d && $d->format($format) == $date; |
||
| 144 | } |
||
| 145 | |||
| 146 | protected static function check_required(string $param): bool |
||
| 149 | } |
||
| 150 | |||
| 151 | protected static function toNext(string $param, $value) |
||
| 154 | } |
||
| 155 | |||
| 156 | } |