Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

Selectable::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid;
4
5
use Encore\Admin\Grid;
6
use Encore\Admin\Grid\Selectable\Checkbox;
7
use Encore\Admin\Grid\Selectable\Radio;
8
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Encore\Admin\Grid\Model.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Illuminate\Support\Arr;
10
11
/**
12
 * @mixin Grid
13
 */
14
abstract class Selectable
15
{
16
    /**
17
     * @var string
18
     */
19
    public $model;
20
21
    /**
22
     * @var Grid
23
     */
24
    protected $grid;
25
26
    /**
27
     * @var string
28
     */
29
    protected $key = '';
30
31
    /**
32
     * @var int
33
     */
34
    protected $perPage = 10;
35
36
    /**
37
     * @return Grid
38
     */
39
    abstract public function make();
40
41
    /**
42
     * @param bool $multiple
43
     * @return string
44
     */
45
    public function render($multiple = false)
46
    {
47
        $this->make();
48
49
        $this->appendRemoveBtn();
50
51
        $this->paginate($this->perPage)
0 ignored issues
show
Documentation Bug introduced by
The method paginate does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
52
            ->expandFilter()
53
            ->disableExport()
54
            ->disableActions()
55
            ->disableBatchActions()
56
            ->disableCreateButton()
57
            ->disableColumnSelector()
58
            ->disablePerPageSelector();
59
60
        $displayer = $multiple ? Checkbox::class : Radio::class;
61
62
        $this->prependColumn('__modal_selector__', ' ')
0 ignored issues
show
Documentation Bug introduced by
The method prependColumn does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
            ->displayUsing($displayer, [$this->key]);
64
65
        return $this->grid->render();
66
    }
67
68
    public function renderFormGrid($values, $multiple = false)
69
    {
70
        $this->make();
71
72
        $this->appendRemoveBtn(false);
73
74
        $this->model()->whereKey(Arr::wrap($values));
0 ignored issues
show
Documentation Bug introduced by
The method model does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
76
        $this->disableFilter()
0 ignored issues
show
Documentation Bug introduced by
The method disableFilter does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
            ->disableExport()
78
            ->disableActions()
79
            ->disableBatchActions()
80
            ->disableCreateButton()
81
            ->disableColumnSelector()
82
            ->disablePerPageSelector()
83
        ;
84
85
        if (!$multiple) {
86
            $this->disablePagination();
0 ignored issues
show
Documentation Bug introduced by
The method disablePagination does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
87
        }
88
89
        $this->tools(function (Tools $tools) {
0 ignored issues
show
Documentation Bug introduced by
The method tools does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
            $tools->append(new Grid\Selectable\BrowserBtn());
0 ignored issues
show
Documentation introduced by
new \Encore\Admin\Grid\Selectable\BrowserBtn() is of type object<Encore\Admin\Grid\Selectable\BrowserBtn>, but the function expects a object<Encore\Admin\Grid...ls\AbstractTool>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        });
92
93
        return $this->grid;
94
    }
95
96
    protected function appendRemoveBtn($hide = true)
97
    {
98
        $hide = $hide ? 'hide' : '';
99
        $key = $this->key;
100
101
        $this->column('__remove__', ' ')->display(function () use ($hide, $key) {
0 ignored issues
show
Documentation Bug introduced by
The method column does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
102
            return <<<BTN
103
<a href="javascript:void(0);" class="grid-row-remove {$hide}" data-key="{$this->getAttribute($key)}">
0 ignored issues
show
Documentation Bug introduced by
The method getAttribute does not exist on object<Encore\Admin\Grid\Selectable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
    <i class="fa fa-trash"></i>
105
</a>
106
BTN;
107
        });
108
    }
109
110
    protected function initGrid()
111
    {
112
        if (!class_exists($this->model) || !is_subclass_of($this->model, Model::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Database\Eloquent\Model::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
113
            throw new \InvalidArgumentException("Invalid model [{$this->model}]");
114
        }
115
116
        /** @var Model $model */
117
        $model = new $this->model;
118
119
        $this->grid = new Grid(new $model);
120
121
        if (!$this->key) {
122
            $this->key = $model->getKeyName();
123
        }
124
    }
125
126
    /**
127
     * @param string $method
128
     * @param array $arguments
129
     * @return mixed
130
     */
131
    public function __call(string $method, array $arguments = [])
132
    {
133
        if (is_null($this->grid)) {
134
            $this->initGrid();
135
        }
136
137
        return $this->grid->{$method}(...$arguments);
138
    }
139
}
140