Completed
Push — master ( 631589...ae2f10 )
by Song
02:39
created

Selectable   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
make() 0 1 ?
A render() 0 20 2
A renderFormGrid() 0 26 2
A appendRemoveBtn() 0 13 2
A initGrid() 0 15 4
A __call() 0 4 1
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 bool
33
     */
34
    protected $multiple = false;
35
36
    /**
37
     * @var int
38
     */
39
    protected $perPage = 10;
40
41
    /**
42
     * Selectable constructor.
43
     * @param $key
44
     * @param $multiple
45
     */
46
    public function __construct($multiple = false, $key = '')
47
    {
48
        $this->key      = $key;
49
        $this->multiple = $multiple;
50
51
        $this->initGrid();
52
    }
53
54
    /**
55
     * @return Grid
56
     */
57
    abstract public function make();
58
59
    /**
60
     * @param bool $multiple
0 ignored issues
show
Bug introduced by
There is no parameter named $multiple. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     *
62
     * @return string
63
     */
64
    public function render()
65
    {
66
        $this->make();
67
68
        $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...
69
            ->expandFilter()
70
            ->disableExport()
71
            ->disableActions()
72
            ->disableBatchActions()
73
            ->disableCreateButton()
74
            ->disableColumnSelector()
75
            ->disablePerPageSelector();
76
77
        $displayer = $this->multiple ? Checkbox::class : Radio::class;
78
79
        $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...
80
            ->displayUsing($displayer, [$this->key]);
81
82
        return $this->grid->render();
83
    }
84
85
    public function renderFormGrid($values)
86
    {
87
        $this->make();
88
89
        $this->appendRemoveBtn(false);
90
91
        $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...
92
93
        $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...
94
            ->disableExport()
95
            ->disableActions()
96
            ->disableBatchActions()
97
            ->disableCreateButton()
98
            ->disableColumnSelector()
99
            ->disablePerPageSelector();
100
101
        if (!$this->multiple) {
102
            $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...
103
        }
104
105
        $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...
106
            $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...
107
        });
108
109
        return $this->grid;
110
    }
111
112
    protected function appendRemoveBtn($hide = true)
113
    {
114
        $hide = $hide ? 'hide' : '';
115
        $key = $this->key;
116
117
        $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...
118
            return <<<BTN
119
<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...
120
    <i class="fa fa-trash"></i>
121
</a>
122
BTN;
123
        });
124
    }
125
126
    protected function initGrid()
127
    {
128
        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...
129
            throw new \InvalidArgumentException("Invalid model [{$this->model}]");
130
        }
131
132
        /** @var Model $model */
133
        $model = new $this->model();
134
135
        $this->grid = new Grid(new $model());
136
137
        if (!$this->key) {
138
            $this->key = $model->getKeyName();
139
        }
140
    }
141
142
    /**
143
     * @param string $method
144
     * @param array  $arguments
145
     *
146
     * @return mixed
147
     */
148
    public function __call(string $method, array $arguments = [])
149
    {
150
        return $this->grid->{$method}(...$arguments);
151
    }
152
}
153