Conditions | 16 |
Paths | 8 |
Total Lines | 53 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | protected function validateValue($value) |
||
20 | { |
||
21 | $valid = true; |
||
22 | $input = preg_replace('/[^\d]/', '', $value); |
||
23 | |||
24 | $uf = substr($input, -4, 2); |
||
25 | |||
26 | if ( |
||
27 | ((mb_strlen($input) < 5) || (mb_strlen($input) > 13)) || |
||
28 | (str_repeat($input[1], mb_strlen($input)) == $input) || |
||
29 | ($uf < 1 || $uf > 28) |
||
30 | ) { |
||
31 | $valid = false; |
||
32 | } |
||
33 | |||
34 | if ($valid) { |
||
35 | $dv = substr($input, -2); |
||
36 | $sequencia = substr($input, 0, -4); |
||
37 | $base = 2; |
||
38 | |||
39 | for ($i = 0; $i < 2; $i++) { |
||
40 | $fator = 9; |
||
41 | $soma = 0; |
||
42 | |||
43 | for ($j = (mb_strlen($sequencia) - 1); $j > -1; $j--) { |
||
44 | $soma += $sequencia[$j] * $fator; |
||
45 | |||
46 | if ($fator == $base) { |
||
47 | $fator = 10; |
||
48 | } |
||
49 | |||
50 | $fator--; |
||
51 | } |
||
52 | |||
53 | $digito = $soma % 11; |
||
54 | if (($digito == 0) and ($uf < 3)) { |
||
55 | $digito = 1; |
||
56 | } elseif ($digito == 10) { |
||
57 | $digito = 0; |
||
58 | } |
||
59 | |||
60 | if ($dv[$i] != $digito) { |
||
61 | $valid = false; |
||
62 | } |
||
63 | |||
64 | switch ($i) { |
||
65 | case '0': |
||
66 | $sequencia = $uf . $digito; |
||
67 | break; |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | return ($valid) ? [] : [$this->message, []]; |
||
72 | } |
||
90 | } |