Completed
Push — master ( 2a9bf3...618bf6 )
by Arjay
01:57
created

EloquentEngine::isSoftDeleting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Yajra\Datatables\Engines;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Str;
7
use Yajra\Datatables\Request;
8
9
/**
10
 * Class EloquentEngine.
11
 *
12
 * @package Yajra\Datatables\Engines
13
 * @author  Arjay Angeles <[email protected]>
14
 */
15
class EloquentEngine extends QueryBuilderEngine
16
{
17
    /**
18
     * Select trashed records in count function for models with soft deletes trait.
19
     * By default we do not select soft deleted records.
20
     *
21
     * @var bool
22
     */
23
    protected $withTrashed = false;
24
25
    /**
26
     * Select only trashed records in count function for models with soft deletes trait.
27
     * By default we do not select soft deleted records.
28
     *
29
     * @var bool
30
     */
31
    protected $onlyTrashed = false;
32
33
    /**
34
     * EloquentEngine constructor.
35
     *
36
     * @param mixed                     $model
37
     * @param \Yajra\Datatables\Request $request
38
     */
39
    public function __construct($model, Request $request)
40
    {
41
        $builder = $model instanceof Builder ? $model : $model->getQuery();
42
        parent::__construct($builder->getQuery(), $request);
43
44
        $this->query = $builder;
45
    }
46
47
    /**
48
     * Counts current query.
49
     *
50
     * @return int
51
     */
52
    public function count()
53
    {
54
        $builder = clone $this->query;
55
56
        if ($this->isComplexQuery($builder)) {
0 ignored issues
show
Bug introduced by
It seems like $builder defined by clone $this->query on line 54 can also be of type object<Illuminate\Database\Query\Builder>; however, Yajra\Datatables\Engines...ngine::isComplexQuery() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
            $row_count = $this->wrap('row_count');
58
            $builder->select($this->connection->raw("'1' as {$row_count}"));
0 ignored issues
show
Bug introduced by
The method select does only exist in Illuminate\Database\Query\Builder, 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...
59
        }
60
61
        if ($this->isSoftDeleting()) {
62
            $builder->whereNull($builder->getModel()->getQualifiedDeletedAtColumn());
0 ignored issues
show
Bug introduced by
The method getModel does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Query\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...
Bug introduced by
The method whereNull does only exist in Illuminate\Database\Query\Builder, 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...
63
        }
64
65
        if ($this->isOnlyTrashed()) {
66
            $builder->whereNotNull($builder->getModel()->getQualifiedDeletedAtColumn());
0 ignored issues
show
Bug introduced by
The method whereNotNull does only exist in Illuminate\Database\Query\Builder, 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...
67
        }
68
69
        return $this->connection->table($this->connection->raw('(' . $builder->toSql() . ') count_row_table'))
0 ignored issues
show
Bug introduced by
The method toSql does only exist in Illuminate\Database\Query\Builder, 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...
70
                                ->setBindings($builder->getBindings())->count();
0 ignored issues
show
Bug introduced by
The method getBindings does only exist in Illuminate\Database\Query\Builder, 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...
71
    }
72
73
    /**
74
     * Check if builder query uses complex sql.
75
     *
76
     * @param \Illuminate\Database\Eloquent\Builder $builder
77
     * @return bool
78
     */
79
    private function isComplexQuery($builder)
80
    {
81
        return !Str::contains(Str::lower($builder->toSql()), ['union', 'having', 'distinct', 'order by', 'group by']);
82
    }
83
84
    /**
85
     * Check if engine uses soft deletes.
86
     *
87
     * @return bool
88
     */
89
    private function isSoftDeleting()
90
    {
91
        return !$this->withTrashed && !$this->onlyTrashed && $this->modelUseSoftDeletes();
92
    }
93
94
    /**
95
     * Check if model use SoftDeletes trait.
96
     *
97
     * @return boolean
98
     */
99
    private function modelUseSoftDeletes()
100
    {
101
        return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->query->getModel()));
0 ignored issues
show
Bug introduced by
The method getModel does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Query\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...
102
    }
103
104
    /**
105
     * Check if engine uses only trashed.
106
     *
107
     * @return bool
108
     */
109
    private function isOnlyTrashed()
110
    {
111
        return $this->onlyTrashed && $this->modelUseSoftDeletes();
112
    }
113
114
    /**
115
     * Change withTrashed flag value.
116
     *
117
     * @param bool $withTrashed
118
     * @return $this
119
     */
120
    public function withTrashed($withTrashed = true)
121
    {
122
        $this->withTrashed = $withTrashed;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Change onlyTrashed flag value.
129
     *
130
     * @param bool $onlyTrashed
131
     * @return $this
132
     */
133
    public function onlyTrashed($onlyTrashed = true)
134
    {
135
        $this->onlyTrashed = $onlyTrashed;
136
137
        return $this;
138
    }
139
140
    /**
141
     * If column name could not be resolved then use primary key.
142
     *
143
     * @return string
144
     */
145
    protected function getPrimaryKeyName()
146
    {
147
        return $this->query->getModel()->getKeyName();
0 ignored issues
show
Bug introduced by
The method getModel does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Query\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...
148
    }
149
}
150