WithAttributeValueAndOperatorCriterion   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A apply() 0 6 1
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