Searchable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 10 3
A resolveSearch() 0 6 1
1
<?php
2
3
namespace Koch\Filters\Behavior;
4
5
trait Searchable
6
{
7
    /**
8
     * Searches given columns.
9
     *
10
     * @param  string  $pattern
11
     * @return void
12
     */
13
    public function search($pattern = null)
14
    {
15
        if (! $pattern) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pattern of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
16
            return;
17
        }
18
19
        foreach ($this->searchable as $column) {
0 ignored issues
show
Bug introduced by
The property searchable 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...
20
            $this->resolveSearch($column, $pattern);
21
        }
22
    }
23
24
    /**
25
     * Recursively build up the search query.
26
     *
27
     * @param  string  $column
28
     * @param  string  $key
29
     * @return void
30
     */
31
    protected function resolveSearch($column, $key)
32
    {
33
        $this->resolve($column, $key, $this->getTableName(), function ($query, $pattern) {
0 ignored issues
show
Bug introduced by
It seems like getTableName() 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...
Bug introduced by
The method resolve() does not exist on Koch\Filters\Behavior\Searchable. Did you maybe mean resolveSearch()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
            $this->builder->orWhere($query, 'LIKE', "%{$pattern}%");
0 ignored issues
show
Bug introduced by
The property builder 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...
35
        });
36
    }
37
}
38