Passed
Push — master ( 671b4b...06a963 )
by Gabriel
04:41
created

FieldTrait::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Models\Fields;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Types\AbstractType;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\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\Models\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);
0 ignored issues
show
Deprecated Code introduced by
The function Nip\I18n\Translator::translate() has been deprecated: Use new trans() method ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
        return /** @scrutinizer ignore-deprecated */ translator()->translate($this->field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
48
     */
49
    public function populateFromLetter($letter)
50
    {
51
        $this->id_letter = $letter->id;
52
    }
53
54
    /**
55
     * @param Fpdi $pdf
56
     * @param Record $model
57
     */
58
    public function addToPdf($pdf, $model)
59
    {
60
        $this->getType()->addToPdf($pdf, $model);
0 ignored issues
show
Bug introduced by
$pdf of type setasign\Fpdi\Fpdi is incompatible with the type setasign\Fpdi\Tcpdf\Fpdi expected by parameter $pdf of ByTIC\DocumentGenerator\...bstractType::addToPdf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $this->getType()->addToPdf(/** @scrutinizer ignore-type */ $pdf, $model);
Loading history...
61
    }
62
63
    /**
64
     * @param Record $model
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) targeting 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
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);
0 ignored issues
show
Unused Code introduced by
The call to setasign\Fpdi\FpdfTpl::SetFont() has too many arguments starting with ''. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $pdf->/** @scrutinizer ignore-call */ 
112
              SetFont('freesans', '', $this->size, '', true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
    }
113
}
114