Completed
Push — master ( 0c2e7c...16d3e7 )
by Roberto
05:22 queued 03:20
created

Element::standarize()   B

Complexity

Conditions 10
Paths 31

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.1493

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 31
cts 35
cp 0.8857
rs 7.0933
c 0
b 0
f 0
cc 10
nc 31
nop 1
crap 10.1493

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace NFePHP\EFD\Common;
4
5
use \stdClass;
6
use NFePHP\Common\Strings;
7
8
abstract class Element
9
{
10
11
    public $std;
12
    protected $parameters;
13
    private $reg;
14
15 9
    public function __construct($reg)
16
    {
17 9
        $this->reg = $reg;
18 9
    }
19
20
    public function postValidation()
21
    {
22
        return true;
23
    }
24
25
    /**
26
     * Valida e ajusta os dados de entrada para os padões estabelecidos
27
     * @param \stdClass $std
28
     */
29 9
    protected function standarize(\stdClass $std)
30
    {
31 9
        if (empty($this->parameters)) {
32
            throw new \Exception('Parametros não estabelecidos na classe');
33
        }
34 9
        $errors = [];
35
        //passa todos as variáveis do stdClass para minusculo
36 9
        $arr = array_change_key_case(get_object_vars($std), CASE_LOWER);
37 9
        $std = json_decode(json_encode($arr));
38
        //paga as chaves dos parametros e passa para minusculo
39 9
        $stdParam = json_decode(json_encode($this->parameters));
40 9
        $this->parameters = array_change_key_case(get_object_vars($stdParam), CASE_LOWER);
41 9
        $paramKeys = array_keys($this->parameters);
42
        //passa os paramatros com as chaves modificadas para um stdClass
43 9
        $stdParam = json_decode(json_encode($this->parameters));
44
        //verifica se foram passados os dados obrigatórios
45 9
        foreach ($std as $key => $value) {
46 9
            if ($stdParam->$key->required && $std->$key === null) {
47 9
                $errors[] = "$key é requerido.";
48
            }
49
        }
50 9
        $newstd = new \stdClass();
51 9
        foreach ($paramKeys as $key) {
52 9
            if (!key_exists($key, $arr)) {
53
                $newstd->$key = null;
54
            } else {
55 9
                if ($std->$key === null) {
56
                    $newstd->$key = null;
57
                    continue;
58
                }
59
                //se o valor para o parametro foi passado, então validar
60 9
                $resp = $this->isFieldInError(
61 9
                    $std->$key,
62 9
                    $stdParam->$key,
63 9
                    strtoupper($key),
64 9
                    $this->reg
65
                );
66 9
                if ($resp) {
67 6
                    $errors[] = $resp;
68
                }
69
                //e formatar o dado passado
70 9
                $formated = $this->formater(
71 9
                    $std->$key,
72 9
                    $stdParam->$key->format,
73 9
                    strtoupper($key)
74
                );
75
                //Strings::replaceUnacceptableCharacters()
76 9
                $newstd->$key = $formated;
77
            }
78
        }
79
        //se algum erro for detectado disparar um Exception
80 9
        if (!empty($errors)) {
81 6
            throw new \InvalidArgumentException(implode("\n", $errors));
82
        }
83 3
        return $newstd;
84
    }
85
86
    /**
87
     * Verifica os campos comrelação ao tipo e seu regex
88
     * @param string|integer|float $input
89
     * @param \stdClass $param
90
     * @param string $fieldname
91
     * @return string|boolean
92
     */
93 9
    protected function isFieldInError($input, $param, $fieldname, $element)
94
    {
95 9
        $type = $param->type;
96 9
        $regex = $param->regex;
97 9
        if (empty($regex)) {
98
            return false;
99
        }
100 6
        switch ($type) {
101 9
            case 'integer':
102 9
                if (!is_integer($input)) {
103 3
                    return "[$this->reg] $element campo: $fieldname deve ser um valor numérico inteiro.";
104
                }
105 6
                break;
106
            case 'numeric':
107
                if (!is_numeric($input)) {
108
                    return "[$this->reg] $element campo: $fieldname deve ser um numero.";
109
                }
110
                break;
111
            case 'string':
112
                if (!is_string($input)) {
113
                    return "[$this->reg] $element campo: $fieldname deve ser uma string.";
114
                }
115
                break;
116
        }
117 6
        $input = (string) $input;
118 6
        if ($regex === 'email') {
119
            if (!filter_var($input, FILTER_VALIDATE_EMAIL)) {
120
                return "[$this->reg] $element campo: $fieldname Esse email [$input] está incorreto.";
121
            }
122
            return false;
123
        }
124 6
        if (!preg_match("/$regex/", $input)) {
125 3
            return "[$this->reg] $element campo: $fieldname valor incorreto [$input]. (validação: $regex)";
126
        }
127 3
        return false;
128
    }
129
130
    /**
131
     * Formata os campos float
132
     * @param string|integer|float $value
133
     * @param string $format
134
     * @return string|integer
135
     * @throws \InvalidArgumentException
136
     */
137 9
    protected function formater($value, $format = null, $fieldname = '')
138
    {
139 9
        if (empty($value)) {
140
            return $value;
141
        }
142 9
        if (!is_numeric($value)) {
143
            //se não é numerico então permitir apenas ASCII
144 3
            return Strings::toASCII($value);
145
        }
146 6
        if (empty($format)) {
147 6
            return $value;
148
        }
149
        $n = explode('v', $format);
150
        $mdec = strpos($n[1], '-');
151
        $p = explode('.', $value);
152
        $ndec = !empty($p[1]) ? strlen($p[1]) : 0; //decimal digits
153
        $nint = strlen($p[0]); //integer digits
154
        if ($nint > $n[0]) {
155
            throw new \InvalidArgumentException("[$this->reg] O [$fieldname] é maior "
156
            . "que o permitido [$format].");
157
        }
158
        if ($mdec !== false) {
159
            //is multi decimal
160
            $mm = explode('-', $n[1]);
161
            $decmin = $mm[0];
162
            $decmax = $mm[1];
163
            //verificar a quantidade de decimais informada
164
            //se maior ou igual ao minimo e menor ou igual ao maximo
165
            if ($ndec >= $decmin && $ndec <= $decmax) {
166
                //deixa como está
167
                return $value;
168
            }
169
            //se menor que o minimo, formata para o minimo
170
            if ($ndec < $decmin) {
171
                return number_format($value, $decmin, ',', '');
172
            }
173
            //se maior que o maximo, formata para o maximo
174
            if ($ndec > $decmax) {
175
                return number_format($value, $decmax, ',', '');
176
            }
177
        }
178
        return number_format($value, $n[1], ',', '');
179
    }
180
181
    /**
182
     * Construtor do elemento
183
     * @param type $reg
0 ignored issues
show
Bug introduced by
There is no parameter named $reg. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
184
     * @return string
185
     */
186 3
    protected function build()
187
    {
188 3
        $register = '';
189 3
        foreach ($this->parameters as $key => $params) {
190 3
            $register .= $this->std->$key . '|';
191
        }
192 3
        return $register;
193
    }
194
195
    /**
196
     * Retorna o elemento formatado em uma string
197
     * @return string
198
     */
199 3
    public function __toString()
200
    {
201 3
        return '|' . $this->reg . '|' . $this->build();
202
    }
203
}
204