Passed
Branch master (9353c8)
by Henri
02:26 queued 01:07
created

Check   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 142
rs 9.0399
wmc 42

13 Methods

Rating   Name   Duplication   Size   Complexity  
A check_minlength() 0 9 5
A check_requireds() 0 4 2
A check_filter() 0 6 3
A check_type() 0 10 4
A check_mincount() 0 6 3
A check_required() 0 3 2
A toNext() 0 3 2
A validateDate() 0 4 2
A check_maxlength() 0 10 5
A check_regex() 0 10 5
A testArray() 0 6 2
A check_equals() 0 10 4
A check_maxcount() 0 6 3

How to fix   Complexity   

Complex Class

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
2
3
namespace HnrAzevedo\Validator;
4
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)
13
    {
14
        if(self::toNext($param,$value)){    
15
            
16
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
17
18
            foreach($realval as $val){
19
                if($value > strlen($val)) {
20
                    self::$errors[] = "{$param} não atingiu o mínimo de caracteres esperado.";
21
                }
22
            }
23
        }       
24
    }
25
26
    protected static function check_requireds()
27
    {
28
        if(count(self::$required) > 0){
29
            self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.';
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)
123
    {
124
        if(self::toNext($param,$value)){
125
126
            if(!filter_var(json_decode(self::$data['data'])->$param, $value)){
127
                self::$errors[] = "{$param} não passou pela filtragem de dados.";
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
140
    {
141
        return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']);
142
    }
143
144
    protected static function toNext(string $param, $value)
145
    {
146
        return (self::check_required($param) || strlen($value > 0));
147
    }
148
149
}