Completed
Push — develop ( b53258...702238 )
by Tony
15:27
created

DeviceGroup   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 6 1
1
<?php
2
/**
3
 * DeviceGroup.php
4
 *
5
 * Scope that only includes devices with the specified group.
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\DataTables\Scopes;
27
28
use Yajra\Datatables\Contracts\DataTableScopeContract;
29
30
class DeviceGroup implements DataTableScopeContract
31
{
32
33
    private $group_id;
34
35
36
    /**
37
     * DeviceGroup constructor.
38
     *
39
     * @param integer $group_id
40
     */
41
    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...
42
    {
43
        $this->group_id = $group_id;
44
    }
45
46
    /**
47
     * Apply a query scope.
48
     *
49
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
50
     * @return mixed
51
     */
52
    public function apply($query)
53
    {
54
        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...
55
            $q->where('id', '=', $this->group_id);
56
        });
57
    }
58
}