Completed
Push — device-groups ( 54870b...cccab4 )
by Tony
03:45
created

DeviceGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\DataTables\Scopes;
4
5
use Yajra\Datatables\Contracts\DataTableScopeContract;
6
7
class DeviceGroup implements DataTableScopeContract {
8
9
    private $group_id;
10
11
12
    function __construct($group_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        $this->group_id = $group_id;
14
    }
15
16
    /**
17
     * Apply a query scope.
18
     *
19
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
20
     * @return mixed
21
     */
22
    public function apply($query)
23
    {
24
        return $query->whereHas('groups', function ($q) {
0 ignored issues
show
Bug introduced by
The method whereHas 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...
25
            $q->where('id', '=', $this->group_id);
26
        });
27
    }
28
}