Passed
Push — master ( d57bd3...3dbf58 )
by Churakov
03:47
created

ActionColumn::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ChurakovMike\EasyGrid\Columns;
4
5
use ChurakovMike\EasyGrid\Columns\Actions\BaseButton;
6
use ChurakovMike\EasyGrid\Columns\Actions\Destroy;
7
use ChurakovMike\EasyGrid\Columns\Actions\Show;
8
use ChurakovMike\EasyGrid\Columns\Actions\Update;
9
use ChurakovMike\EasyGrid\Filters\StubFilter;
10
use ChurakovMike\EasyGrid\Traits\Configurable;
11
12
/**
13
 * Class ActionColumn.
14
 * @package ChurakovMike\EasyGrid\Columns
15
 *
16
 * @property StubFilter $filter
17
 * @property BaseButton[] $buttons
18
 * @property string $value
19
 */
20
class ActionColumn extends BaseColumn
21
{
22
    use Configurable;
23
24
    const
25
        ACTION_SHOW = 'show',
26
        ACTION_UPDATE = 'update',
27
        ACTION_DESTROY = 'destroy',
28
        ACTIONS = [
29
            self::ACTION_SHOW,
30
            self::ACTION_UPDATE,
31
            self::ACTION_DESTROY,
32
        ];
33
34
    const
35
        BASE_ACTIONS = [
36
            self::ACTION_SHOW => Show::class,
37
            self::ACTION_UPDATE => Update::class,
38
            self::ACTION_DESTROY => Destroy::class,
39
    ];
40
41
    /**
42
     * @var StubFilter $filter
43
     */
44
    public $filter;
45
46
    /**
47
     * @var array $buttons
48
     */
49
    public $buttons;
50
51
    /**
52
     * @var string $value
53
     */
54
    public $value;
55
56
    /**
57
     * ActionColumn constructor.
58
     * @param array $config
59
     */
60
    public function __construct(array $config)
61
    {
62
        $this->loadConfig($config);
63
        $this->buildButtons($config);
64
        $this->filter = new StubFilter(['name' => '']);
65
    }
66
67
    /**
68
     * Build action column buttons.
69
     *
70
     * @param $config
71
     */
72
    public function buildButtons($config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

72
    public function buildButtons(/** @scrutinizer ignore-unused */ $config)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        foreach ($this->buttons as $key => &$button) {
75
            if(is_string($button)) {
76
                if (in_array($button, self::ACTIONS)) {
77
                    $class = self::BASE_ACTIONS[$button];
78
                    $button = new $class;
79
                    continue;
80
                }
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getLabel(): string
89
    {
90
        return '';
91
    }
92
93
    /**
94
     * @param $row
95
     * @return string
96
     */
97
    public function render($row)
98
    {
99
        if (empty($this->value)) {
100
            foreach ($this->buttons as $button) {
101
                $this->value .= $button->render();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $button->render() targeting ChurakovMike\EasyGrid\Co...ns\BaseButton::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
102
            }
103
        }
104
105
        return '<div class="row">' . $this->value . '</div>';
106
        return $this->value;
0 ignored issues
show
Unused Code introduced by
return $this->value is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
107
    }
108
}
109