Button::setLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ZfcDatagrid\Column\Action;
4
5
use ZfcDatagrid\Column\AbstractColumn;
6
7
class Button extends AbstractAction
8
{
9
    protected $label = '';
10
11
    public function __construct()
12
    {
13
        parent::__construct();
14
15
        $this->addClass('btn');
16
        $this->addClass('btn-default');
17
    }
18
19
    /**
20
     * @param string|AbstractColumn $name
21
     */
22
    public function setLabel($name)
23
    {
24
        $this->label = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name can also be of type object<ZfcDatagrid\Column\AbstractColumn>. However, the property $label is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
25
    }
26
27
    /**
28
     * @return string|AbstractColumn
29
     */
30
    public function getLabel()
31
    {
32
        return $this->label;
33
    }
34
35
    /**
36
     * @return string
37
     *
38
     * @throws \Exception
39
     */
40
    protected function getHtmlType()
41
    {
42
        throw new \Exception('not needed...since we have toHtml() here directly!');
43
    }
44
45
    /**
46
     * @param array $row
47
     *
48
     * @return string
49
     */
50
    public function toHtml(array $row)
51
    {
52
        if ($this->getLabel() == '') {
53
            throw new \InvalidArgumentException('A label is required for this action type, please call $action->setLabel()!');
54
        }
55
56
        $label = $this->getLabel();
57
        if ($label instanceof AbstractColumn) {
58
            $label = $row[$label->getUniqueId()];
59
        }
60
61
        return '<a '.$this->getAttributesString($row).'>'.$label.'</a>';
62
    }
63
}
64