ReglaTasaCuota::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 5

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
c 1
b 0
f 0
nc 5
nop 9
dl 0
loc 35
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
    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
        if (
68 15
            ReglasTasaCuota::IMPUESTO_IEPS !== $impuesto
69 15
            && ReglasTasaCuota::IMPUESTO_IVA !== $impuesto
70 15
            && ReglasTasaCuota::IMPUESTO_ISR !== $impuesto
71
        ) {
72 2
            throw new SatCatalogosLogicException('El campo impuesto no tiene uno de los valores permitidos');
73
        }
74 13
        if (ReglasTasaCuota::FACTOR_CUOTA !== $factor && ReglasTasaCuota::FACTOR_TASA !== $factor) {
75 2
            throw new SatCatalogosLogicException('El campo factor no tiene uno de los valores permitidos');
76
        }
77 11
        if (! $traslado && ! $retencion) {
78 1
            throw new SatCatalogosLogicException('Los campos retención y traslado no pueden ser falsos ambos');
79
        }
80 10
        $this->tipo = $tipo;
81 10
        $this->impuesto = $impuesto;
82 10
        $this->factor = $factor;
83 10
        $this->traslado = $traslado;
84 10
        $this->retencion = $retencion;
85 10
        $this->minimo = $minimo;
86 10
        $this->valor = $valor;
87 10
        $this->setUpVigencias($vigenteDesde, $vigenteHasta);
88
    }
89
90 2
    public function tipo(): string
91
    {
92 2
        return $this->tipo;
93
    }
94
95 1
    public function impuesto(): string
96
    {
97 1
        return $this->impuesto;
98
    }
99
100 1
    public function factor(): string
101
    {
102 1
        return $this->factor;
103
    }
104
105 3
    public function traslado(): bool
106
    {
107 3
        return $this->traslado;
108
    }
109
110 3
    public function retencion(): bool
111
    {
112 3
        return $this->retencion;
113
    }
114
115 1
    public function minimo(): string
116
    {
117 1
        return $this->minimo;
118
    }
119
120 1
    public function maximo(): string
121
    {
122 1
        return $this->valor;
123
    }
124
125 1
    public function valor(): string
126
    {
127 1
        return $this->valor;
128
    }
129
130 7
    public function valorIsValid(string $valor): bool
131
    {
132 7
        if (! (bool) preg_match('/^\d{0,2}(\.\d{0,6})?$/', $valor)) {
133 1
            return false;
134
        }
135
136 7
        if (self::TIPO_FIJO === $this->tipo) {
137 5
            return $valor === $this->valor;
138
        }
139
140 2
        if (self::TIPO_RANGO === $this->tipo) {
141 2
            $delta = 1000000;
142 2
            $current = intval($delta * floatval($valor));
143 2
            $min = intval($delta * floatval($this->minimo));
144 2
            $max = intval($delta * floatval($this->valor));
145 2
            return ($current >= $min && $current <= $max);
146
        }
147
148
        /** @codeCoverageIgnore This is a safeguard since the object cannot be constructed with other type */
149
        throw new LogicException(
150
            "Don't know how to compare the current rule, it is not TIPO_FIJO or TIPO_RANGO",
151
        );
152
    }
153
}
154