Completed
Push — master ( 618bf6...a8a183 )
by Arjay
01:59
created

EloquentEngine::onlyTrashed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 = $this->prepareCountQuery();
55
56
        if ($this->isSoftDeleting()) {
57
            $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...
58
        }
59
60
        if ($this->isOnlyTrashed()) {
61
            $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...
62
        }
63
64
        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...
65
                                ->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...
66
    }
67
68
    /**
69
     * Check if engine uses soft deletes.
70
     *
71
     * @return bool
72
     */
73
    private function isSoftDeleting()
74
    {
75
        return !$this->withTrashed && !$this->onlyTrashed && $this->modelUseSoftDeletes();
76
    }
77
78
    /**
79
     * Check if model use SoftDeletes trait.
80
     *
81
     * @return boolean
82
     */
83
    private function modelUseSoftDeletes()
84
    {
85
        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...
86
    }
87
88
    /**
89
     * Check if engine uses only trashed.
90
     *
91
     * @return bool
92
     */
93
    private function isOnlyTrashed()
94
    {
95
        return $this->onlyTrashed && $this->modelUseSoftDeletes();
96
    }
97
98
    /**
99
     * Change withTrashed flag value.
100
     *
101
     * @param bool $withTrashed
102
     * @return $this
103
     */
104
    public function withTrashed($withTrashed = true)
105
    {
106
        $this->withTrashed = $withTrashed;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Change onlyTrashed flag value.
113
     *
114
     * @param bool $onlyTrashed
115
     * @return $this
116
     */
117
    public function onlyTrashed($onlyTrashed = true)
118
    {
119
        $this->onlyTrashed = $onlyTrashed;
120
121
        return $this;
122
    }
123
124
    /**
125
     * If column name could not be resolved then use primary key.
126
     *
127
     * @return string
128
     */
129
    protected function getPrimaryKeyName()
130
    {
131
        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...
132
    }
133
}
134