Passed
Push — master ( a208f8...8e4667 )
by João
01:39
created

IPI::calcular()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 59
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 31
nc 15
nop 2
dl 0
loc 59
ccs 31
cts 31
cp 1
crap 15
rs 5.9166
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A adValoremIPI() 0 8 1
A calcIsentoIPI() 0 8 1
A calcularIPI() 0 15 4

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
declare(strict_types=1);
4
5
namespace Gbbs\NfeCalculos;
6
7
use Exception;
8
use Gbbs\NfeCalculos\Exception\InvalidCSTException;
9
use Gbbs\NfeCalculos\Exception\NotImplementedCSTException;
10
11
class IPI
12
{
13
    public $CNPJProd;
14
    public $cSelo;
15
    public $qSelo;
16
    public $cEnq;
17
    public $CST;
18
    public $vBC;
19
    public $pIPI;
20
    public $vIPI;
21
    public $qUnid;
22
    public $vUnid;
23
}
24
25
/**
26
 * @param IPI $IPI
27
 * @throws NotImplementedCSTException|InvalidCSTException
28
 */
29
function calcularIPI(IPI $IPI): IPI
30
{
31 15
    $adValorem = ['50'];
32 15
    $isento = ['51'];
33
    $notImplemented = [
34 15
        '00', '01', '02', '03', '04', '05', '49', '52', '53', '54', '55', '99'
35
    ];
36 15
    if (in_array($IPI->CST, $adValorem, true)) {
37 1
        return adValoremIPI($IPI);
38 14
    } elseif (in_array($IPI->CST, $isento, true)) {
39 1
        return calcIsentoIPI($IPI);
40 13
    } elseif (in_array($IPI->CST, $notImplemented, true)) {
41 12
        throw new NotImplementedCSTException($IPI->CST);
42
    }
43 1
    throw new InvalidCSTException($IPI->CST);
44
}
45
46
/**
47
 * ISENTO
48
 * @param IPI $IPI
49
 */
50
function calcIsentoIPI(IPI $IPI): IPI
51
{
52 1
    $calculado = new IPI();
53 1
    $calculado->CST = $IPI->CST;
54 1
    $calculado->vBC = 0.0;
55 1
    $calculado->pIPI = 0.0;
56 1
    $calculado->vIPI = 0.0;
57 1
    return $calculado;
58
}
59
60
/**
61
 * Calcula o Valor IPI Ad Valoren
62
 * @param IPI $IPI
63
 */
64
function adValoremIPI(IPI $IPI): IPI
65
{
66 1
    $calculado = new IPI();
67 1
    $calculado->CST = $IPI->CST;
68 1
    $calculado->vBC = $IPI->vBC;
69 1
    $calculado->vIPI = $IPI->vBC * ($IPI->pIPI / 100);
70 1
    $calculado->pIPI = $IPI->pIPI;
71 1
    return $calculado;
72
}
73
74
/**
75
 * @param string $ncm
76
 */
77
function pIPIFromNCM(string $ncm): float
78
{
79 15
    $path = realpath(__DIR__ . '/../storage') . '/';
80 15
    $tipiFile = file_get_contents($path . 'tipi.json');
81 15
    $tipiList = json_decode($tipiFile, true);
82 15
    foreach ($tipiList as $tipi) {
83 15
        if ($tipi['NCMNum'] === $ncm) {
84 15
            return (float) ($tipi['NCMAli'] === 'NT'? 0 : $tipi['NCMAli']);
85
        }
86
    }
87 1
    throw new Exception('NCM inexistente: ' . $ncm);
88
}
89