Completed
Push — master ( 1f1519...e513b7 )
by Arjay
02:02
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
     * @param mixed $model
35
     * @param \Yajra\Datatables\Request $request
36
     */
37
    public function __construct($model, Request $request)
38
    {
39
        $builder = $model instanceof Builder ? $model : $model->getQuery();
40
        parent::__construct($builder->getQuery(), $request);
41
42
        $this->query      = $builder;
43
        $this->query_type = 'eloquent';
44
    }
45
46
    /**
47
     * Counts current query.
48
     *
49
     * @return int
50
     */
51
    public function count()
52
    {
53
        $myQuery = clone $this->query;
54
        // if its a normal query ( no union, having and distinct word )
55
        // replace the select with static text to improve performance
56 View Code Duplication
        if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) {
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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $row_count = $this->wrap('row_count');
58
            $myQuery->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
        // check for select soft deleted records
62
        if (! $this->withTrashed && ! $this->onlyTrashed && $this->modelUseSoftDeletes()) {
63
            $myQuery->whereNull($myQuery->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...
64
        }
65
66
        if ($this->onlyTrashed && $this->modelUseSoftDeletes()) {
67
            $myQuery->whereNotNull($myQuery->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...
68
        }
69
70
        return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table'))
71
                                ->setBindings($myQuery->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...
72
    }
73
74
    /**
75
     * Check if model use SoftDeletes trait
76
     *
77
     * @return boolean
78
     */
79
    protected function modelUseSoftDeletes()
80
    {
81
        if ($this->query_type == 'eloquent') {
82
            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...
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Change withTrashed flag value.
90
     *
91
     * @param bool $withTrashed
92
     * @return $this
93
     */
94
    public function withTrashed($withTrashed = true)
95
    {
96
        $this->withTrashed = $withTrashed;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Change onlyTrashed flag value.
103
     *
104
     * @param bool $onlyTrashed
105
     * @return $this
106
     */
107
    public function onlyTrashed($onlyTrashed = true)
108
    {
109
        $this->onlyTrashed = $onlyTrashed;
110
111
        return $this;
112
    }
113
}
114