Passed
Push — master ( 7cdedb...e314df )
by Carlos C
57s queued 10s
created

ReglaTasaCuota::__construct()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 5
nop 9
dl 0
loc 34
ccs 19
cts 19
cp 1
crap 10
rs 7.6666

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogos\CFDI;
6
7
use LogicException;
8
use PhpCfdi\SatCatalogos\Common\EntryWithVigencias;
9
use PhpCfdi\SatCatalogos\Common\EntryWithVigenciasTrait;
10
use PhpCfdi\SatCatalogos\Exceptions\SatCatalogosLogicException;
11
12
class ReglaTasaCuota implements EntryWithVigencias
13
{
14 1
    use EntryWithVigenciasTrait;
15
16
    public const TIPO_FIJO = 'Fijo';
17
18
    public const TIPO_RANGO = 'Rango';
19
20
    /** @var string */
21
    private $tipo;
22
23
    /** @var string */
24
    private $impuesto;
25
26
    /** @var string */
27
    private $factor;
28
29
    /** @var bool */
30
    private $traslado;
31
32
    /** @var bool */
33
    private $retencion;
34
35
    /** @var string */
36
    private $minimo;
37
38
    /** @var string */
39
    private $valor;
40
41
    /**
42
     * ReglaTasaCuota constructor.
43
     * @param string $tipo
44
     * @param string $impuesto
45
     * @param string $factor
46
     * @param bool $traslado
47
     * @param bool $retencion
48
     * @param string $minimo
49
     * @param string $valor Valor fijo o bien el máximo del rango
50
     * @param int $vigenteDesde
51
     * @param int $vigenteHasta
52
     */
53 17
    public function __construct(
54
        string $tipo,
55
        string $impuesto,
56
        string $factor,
57
        bool $traslado,
58
        bool $retencion,
59
        string $minimo,
60
        string $valor,
61
        int $vigenteDesde,
62
        int $vigenteHasta
63
    ) {
64 17
        if (self::TIPO_FIJO !== $tipo && self::TIPO_RANGO !== $tipo) {
65 2
            throw new SatCatalogosLogicException('El campo tipo no tiene uno de los valores permitidos');
66
        }
67 15
        if (ReglasTasaCuota::IMPUESTO_IEPS !== $impuesto
68 15
            && ReglasTasaCuota::IMPUESTO_IVA !== $impuesto
69 15
            && ReglasTasaCuota::IMPUESTO_ISR !== $impuesto
70
        ) {
71 2
            throw new SatCatalogosLogicException('El campo impuesto no tiene uno de los valores permitidos');
72
        }
73 13
        if (ReglasTasaCuota::FACTOR_CUOTA !== $factor && ReglasTasaCuota::FACTOR_TASA !== $factor) {
74 2
            throw new SatCatalogosLogicException('El campo factor no tiene uno de los valores permitidos');
75
        }
76 11
        if (! $traslado && ! $retencion) {
77 1
            throw new SatCatalogosLogicException('Los campos retención y traslado no pueden ser falsos ambos');
78
        }
79 10
        $this->tipo = $tipo;
80 10
        $this->impuesto = $impuesto;
81 10
        $this->factor = $factor;
82 10
        $this->traslado = $traslado;
83 10
        $this->retencion = $retencion;
84 10
        $this->minimo = $minimo;
85 10
        $this->valor = $valor;
86 10
        $this->setUpVigencias($vigenteDesde, $vigenteHasta);
87 10
    }
88
89 2
    public function tipo(): string
90
    {
91 2
        return $this->tipo;
92
    }
93
94 1
    public function impuesto(): string
95
    {
96 1
        return $this->impuesto;
97
    }
98
99 1
    public function factor(): string
100
    {
101 1
        return $this->factor;
102
    }
103
104 3
    public function traslado(): bool
105
    {
106 3
        return $this->traslado;
107
    }
108
109 3
    public function retencion(): bool
110
    {
111 3
        return $this->retencion;
112
    }
113
114 1
    public function minimo(): string
115
    {
116 1
        return $this->minimo;
117
    }
118
119 1
    public function maximo(): string
120
    {
121 1
        return $this->valor;
122
    }
123
124 1
    public function valor(): string
125
    {
126 1
        return $this->valor;
127
    }
128
129 7
    public function valorIsValid(string $valor): bool
130
    {
131 7
        if (! (bool) preg_match('/^\d{0,2}(\.\d{0,6})?$/', $valor)) {
132 1
            return false;
133
        }
134
135 7
        if (self::TIPO_FIJO === $this->tipo) {
136 5
            return $valor === $this->valor;
137
        }
138
139 2
        if (self::TIPO_RANGO === $this->tipo) {
140 2
            $delta = 1000000;
141 2
            $current = intval($delta * floatval($valor));
142 2
            $min = intval($delta * floatval($this->minimo));
143 2
            $max = intval($delta * floatval($this->valor));
144 2
            return ($current >= $min && $current <= $max);
145
        }
146
147
        /** @codeCoverageIgnore This is a safeguard since the object cannot be constructed with other type */
148
        throw new LogicException(
149
            "Don't know how to compare the current rule, it is not TIPO_FIJO or TIPO_RANGO"
150
        );
151
    }
152
}
153