Completed
Push — master ( 69a7c6...5199b6 )
by Song
02:55
created

HasSearchBar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 16 3
B applySearch() 0 22 6
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
use Encore\Admin\Grid\Model;
6
use Encore\Admin\Grid\Tools\SearchBar;
7
8
trait HasSearchBar
9
{
10
    /**
11
     * @var string
12
     */
13
    public static $searchKey = '__search__';
14
15
    /**
16
     * @var array|string|\Closure
17
     */
18
    protected $search;
19
20
    /**
21
     * @param array|string|\Closure
22
     * @return $this
23
     */
24
    public function search($search)
25
    {
26
        if (func_num_args() > 1) {
27
            $this->search = func_get_args();
28
        } else {
29
            $this->search = $search;
30
        }
31
32
        if ($query = request()->get(static::$searchKey)) {
33
            $this->applySearch($query);
34
        }
35
36
        $this->tools->append(new SearchBar());
0 ignored issues
show
Bug introduced by
The property tools does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
        return $this;
39
    }
40
41
    /**
42
     * Apply the search query to the query.
43
     *
44
     * @param string $query
45
     * @return mixed
46
     */
47
    protected function applySearch($query = '')
48
    {
49
        /** @var Model $model */
50
        $model = $this->model();
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
52
        if ($this->search instanceof \Closure) {
53
            return call_user_func($this->search, $model, $query);
54
        }
55
56
        if (is_string($this->search)) {
57
            $this->search = [$this->search];
58
        }
59
60
        if (is_array($this->search)) {
61
            $connectionType = $model->eloquent()->getConnection()->getDriverName();
62
            $likeOperator   = $connectionType == 'pgsql' ? 'ilike' : 'like';
63
64
            foreach ($this->search as $column) {
65
                $model->orWhere($column, $likeOperator, '%'.$query.'%');
0 ignored issues
show
Documentation Bug introduced by
The method orWhere does not exist on object<Encore\Admin\Grid\Model>? 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...
66
            }
67
        }
68
    }
69
}