__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Napp\Core\Dbal\Criteria;
4
5
class WithAttributeValueAndOperatorCriterion implements CriterionInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $attribute;
11
12
    /**
13
     * @var string
14
     */
15
    protected $value;
16
17
    /**
18
     * @var string
19
     */
20
    protected $operator;
21
22
    /**
23
     * @param string $attribute
24
     * @param string $value
25
     * @param string $operator
26
     */
27
    public function __construct($attribute, $value, $operator = '=')
28
    {
29
        $this->attribute = $attribute;
30
        $this->value = $value;
31
        $this->operator = $operator;
32
    }
33
34
    /**
35
     * @param \Illuminate\Database\Eloquent\Builder $query
36
     *
37
     * @return \Illuminate\Database\Eloquent\Builder
38
     */
39
    public function apply($query)
40
    {
41
        $query->getQuery()->where($query->getModel()->getTable().'.'.$this->attribute, $this->operator, $this->value);
0 ignored issues
show
Bug introduced by
The method getTable does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
43
        return $query;
44
    }
45
}
46