Completed
Push — master ( f706eb...84b009 )
by Antonio
01:40
created

RuleSearch::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
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\Model\Rule;
15
use Da\User\Traits\ContainerAwareTrait;
16
use yii\base\Model;
17
use yii\data\ActiveDataProvider;
18
use yii\db\Query;
19
20
class RuleSearch extends Rule
21
{
22
    use ContainerAwareTrait;
23
24
    /**
25
     * @var string
26
     */
27
    public $created_at;
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function scenarios()
33
    {
34
        return Model::scenarios();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \yii\base\Model::scenarios(); (array<string,array>) is incompatible with the return type of the parent method Da\User\Model\Rule::scenarios of type array<string,string[]>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function rules()
41
    {
42
        return [
43
            ['name', 'string'],
44
        ];
45
    }
46
47
    /**
48
     * @param array $params
49
     *
50
     * @return ActiveDataProvider
51
     */
52
    public function search(array $params = [])
53
    {
54
        $query = (new Query())
55
            ->select(['name', 'data', 'created_at', 'updated_at'])
56
            ->from($this->getAuthManager()->ruleTable)
0 ignored issues
show
Bug introduced by
Accessing ruleTable 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...
57
            ->orderBy(['name' => SORT_ASC]);
58
59
        if ($this->load($params)) {
60
            $query->andFilterWhere(['name' => $this->name]);
61
        }
62
63
        if (!$this->validate()) {
64
            $query->where('0=1');
65
            var_dump($this->load($params));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->load($params)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
66
            die();
67
        }
68
69
        return $this->make(
70
            ActiveDataProvider::class,
71
            [],
72
            [
73
                'query' => $query,
74
                'db' => $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...
75
                'sort' => [
76
                    'attributes' => ['name', 'created_at', 'updated_at']
77
                ]
78
            ]
79
        );
80
    }
81
}
82