Passed
Push — master ( d65fb2...07d9b9 )
by Roberto
05:56 queued 03:13
created

TraitBlocoV::blocoV()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 58
ccs 0
cts 52
cp 0
crap 20
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method   

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\DA\NFe\Traits;
4
5
/**
6
 * Bloco forma de pagamento
7
 */
8
trait TraitBlocoV
9
{
10
    protected function blocoV($y)
11
    {
12
        $this->bloco5H = $this->calculateHeightPag();
0 ignored issues
show
Bug introduced by
The property bloco5H does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
        
14
        $aFont = ['font'=> $this->fontePadrao, 'size' => 7, 'style' => ''];
0 ignored issues
show
Bug introduced by
The property fontePadrao does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$aFont is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
15
        //$this->pdf->textBox($this->margem, $y, $this->wPrint, $this->bloco5H, '', $aFont, 'T', 'C', true, '', false);
16
        $arpgto = [];
17
        if ($this->pag->length > 0) {
18
            foreach ($this->pag as $pgto) {
0 ignored issues
show
Bug introduced by
The property pag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
                $tipo = $this->pagType((int) $this->getTagValue($pgto, 'tPag'));
0 ignored issues
show
Bug introduced by
It seems like getTagValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
                $valor = number_format((float) $this->getTagValue($pgto, 'vPag'), 2, ',', '.');
0 ignored issues
show
Bug introduced by
It seems like getTagValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
21
                $arpgto[] = [
22
                    'tipo' => $tipo,
23
                    'valor' => $valor
24
                ];
25
            }
26
        } else {
27
            $tipo = $this->pagType((int) $this->getTagValue($this->pag, 'tPag'));
0 ignored issues
show
Bug introduced by
It seems like getTagValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
            $valor = number_format((float) $this->getTagValue($pgto, 'vPag'), 2, ',', '.');
0 ignored issues
show
Bug introduced by
The variable $pgto seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
It seems like getTagValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
            $arpgto[] = [
30
                'tipo' => $tipo,
31
                'valor' => $valor
32
            ];
33
        }
34
        $aFont = ['font'=> $this->fontePadrao, 'size' => 7, 'style' => ''];
35
        $texto = "FORMA PAGAMENTO";
36
        $this->pdf->textBox($this->margem, $y, $this->wPrint, 4, $texto, $aFont, 'T', 'L', false, '', false);
0 ignored issues
show
Bug introduced by
The property pdf does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property margem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property wPrint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
        $texto = "VALOR PAGO R$";
38
        $y1 = $this->pdf->textBox($this->margem, $y, $this->wPrint, 4, $texto, $aFont, 'T', 'R', false, '', false);
39
        
40
        $z = $y + $y1;
41
        foreach ($arpgto as $p) {
42
            $this->pdf->textBox($this->margem, $z, $this->wPrint, 3, $p['tipo'], $aFont, 'T', 'L', false, '', false);
43
            $y2 = $this->pdf->textBox(
44
                $this->margem,
45
                $z,
46
                $this->wPrint,
47
                3,
48
                $p['valor'],
49
                $aFont,
50
                'T',
51
                'R',
52
                false,
53
                '',
54
                false
55
            );
56
            $z += $y2;
57
        }
58
        
59
        $texto = "Troco R$";
60
        $this->pdf->textBox($this->margem, $z, $this->wPrint, 3, $texto, $aFont, 'T', 'L', false, '', false);
61
        $texto = number_format($this->vTroco, 2, ',', '.');
0 ignored issues
show
Bug introduced by
The property vTroco does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
        $y1 = $this->pdf->textBox($this->margem, $z, $this->wPrint, 3, $texto, $aFont, 'T', 'R', false, '', false);
0 ignored issues
show
Unused Code introduced by
$y1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
        
64
        
65
        $this->pdf->dashedHLine($this->margem, $this->bloco5H+$y, $this->wPrint, 0.1, 30);
66
        return $this->bloco5H + $y;
67
    }
68
    
69
    protected function pagType($type)
70
    {
71
        $lista = [
72
            1 => 'Dinheiro',
73
            2 => 'Cheque',
74
            3 => 'Cartão de Crédito',
75
            4 => 'Cartão de Débito',
76
            5 => 'Crédito Loja',
77
            10 => 'Vale Alimentação',
78
            11 => 'Vale Refeição',
79
            12 => 'Vale Presente',
80
            13 => 'Vale Combustível',
81
            15 => 'Boleto Bancário',
82
            90 => 'Sem pagamento',
83
            99 => 'Outros'
84
        ];
85
        return $lista[$type];
86
    }
87
    
88
    protected function calculateHeightPag()
89
    {
90
        $n = $this->pag->length > 0 ? $this->pag->length : 1;
91
        $height = 4 + (2.4 * $n) + 3;
92
        return $height;
93
    }
94
}
95