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