Test Failed
Push — master ( 50ebf9...195aca )
by Gabriel
12:00 queued 12s
created

FieldTrait::pdfXPosition()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Fields;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Fields\Types\AbstractType;
6
use ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait;
7
use Nip\Records\Traits\AbstractTrait\RecordTrait as Record;
8
use \ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordTrait as HasTypeRecordTrait;
9
use setasign\Fpdi\Fpdi;
10
11
/**
12
 * Class FieldTrait
13
 * @package ByTIC\DocumentGenerator\PdfLetters\Fields
14
 *
15
 * @property string $id_letter
16
 * @property string $field
17
 * @property string $size
18
 * @property string $color
19
 * @property string $align
20
 * @property string $x
21
 * @property string $y
22
 *
23
 * @method FieldsTrait getManager()
24
 * @method AbstractType getType()
25
 */
26
trait FieldTrait
27
{
28
    use HasTypeRecordTrait;
29
30
    /**
31
     * @return string
32
     */
33
    public function getName()
34
    {
35
        return translator()->translate($this->field);
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function getTypeValue()
42
    {
43
        return $this->getManager()->getFieldTypeFromMergeTag($this->field);
44
    }
45
46
    /**
47
     * @param PdfLetterTrait $letter
0 ignored issues
show
introduced by
The type PdfLetterTrait for parameter $letter is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
48
     */
49
    public function populateFromLetter($letter)
50
    {
51
        $this->id_letter = $letter->id;
52
    }
53
54
    /**
55
     * @param Fpdi $pdf
56
     * @param Record $model
0 ignored issues
show
introduced by
The type Record for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
57
     */
58
    public function addToPdf($pdf, $model)
59
    {
60
        $this->getType()->addToPdf($pdf, $model);
61
    }
62
63
    /**
64
     * @param Record $model
0 ignored issues
show
introduced by
The type Record for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
65
     * @return string
66
     */
67 1
    public function getValue($model)
68
    {
69 1
        if ($model->id > 0) {
70
            $valueType = $this->getType()->getValue($model);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $valueType is correct as $this->getType()->getValue($model) (which targets ByTIC\DocumentGenerator\...bstractType::getValue()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
72
            return $valueType;
73
        }
74
75 1
        return '<<'.$this->field.'>>';
76
    }
77
78
    /**
79
     * @return PdfLetterTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PdfLetterTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
80
     */
81
    abstract public function getPdfLetter();
82
83
    /**
84
     * @return bool
85
     */
86
    public function hasColor()
87
    {
88
        return substr_count($this->color, ',') == 2;
89
    }
90
91
    /**
92
     * @return array|null
93
     */
94
    public function getColorArray()
95
    {
96
        if ($this->hasColor()) {
97
            list ($red, $green, $blue) = explode(',', $this->color);
98
            if ($red && $green && $blue) {
99
                return [intval($red), intval($green), intval($blue)];
100
            }
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * @param Fpdi $pdf
108
     */
109
    protected function pdfPrepareFont($pdf)
110
    {
111
        $pdf->SetFont('freesans', '', $this->size, '', true);
112
    }
113
}
114