AbstractAuthItemSearch::search()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Search;
13
14
use Da\User\Traits\AuthManagerAwareTrait;
15
use Da\User\Traits\ContainerAwareTrait;
16
use yii\base\Model;
17
use yii\data\ArrayDataProvider;
18
use yii\db\Query;
19
20
abstract class AbstractAuthItemSearch extends Model
21
{
22
    use AuthManagerAwareTrait;
23
    use ContainerAwareTrait;
24
25
    /**
26
     * @var string
27
     */
28
    public $name;
29
    /**
30
     * @var string
31
     */
32
    public $description;
33
    /**
34
     * @var string
35
     */
36
    public $rule_name;
37
38
    /**
39
     * @return int
40
     */
41
    abstract public function getType();
42
43
    /**
44
     * @return array
45
     */
46
    public function scenarios()
47
    {
48
        return [
49
            'default' => ['name', 'description', 'rule_name'],
50
        ];
51
    }
52
53
    public function search($params = [])
54
    {
55
        /** @var ArrayDataProvider $dataProvider */
56
        $dataProvider = $this->make(ArrayDataProvider::class);
57
58
        $query = (new Query())
59
            ->select(['name', 'description', 'rule_name'])
60
            ->andWhere(['type' => $this->getType()])
61
            ->from($this->getAuthManager()->itemTable);
0 ignored issues
show
Bug introduced by
Accessing itemTable on the interface yii\rbac\ManagerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
62
63
        if ($this->load($params) && $this->validate()) {
64
            $query
65
                ->andFilterWhere(['like', 'name', $this->name])
66
                ->andFilterWhere(['like', 'description', $this->description])
67
                ->andFilterWhere(['like', 'rule_name', $this->rule_name]);
68
        }
69
70
        $dataProvider->allModels = $query->all($this->getAuthManager()->db);
0 ignored issues
show
Bug introduced by
Accessing db on the interface yii\rbac\ManagerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
72
        return $dataProvider;
73
    }
74
}
75