Completed
Push — master ( e41cc1...feed29 )
by Andrey
08:18
created

ArticleSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 79
loc 79
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 16 16 1
A scenarios() 5 5 1
A search() 35 35 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\{ActiveDataProvider, Pagination};
8
9
/**
10
 * ArticleSearch represents the model behind the search form of `app\models\Article`.
11
 *
12
 * @package app\models
13
 */
14 View Code Duplication
class ArticleSearch extends Article
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    /**
17
     * @var string
18
     */
19
    public $title;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function rules()
25
    {
26
        return [
27
            [
28
                [
29
                    'pageId',
30
                    'active',
31
                ],
32
                'integer',
33
            ],
34
            [
35
                'title',
36
                'string',
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function scenarios()
45
    {
46
        // bypass scenarios() implementation in the parent class
47
        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 app\models\ActiveRecord::scenarios of type array<string,integer[]>.

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...
48
    }
49
50
    /**
51
     * Creates data provider instance with search query applied
52
     *
53
     * @param array $params
54
     *
55
     * @return ActiveDataProvider
56
     */
57
    public function search($params)
58
    {
59
        $query = Article::find();
60
61
        // add conditions that should always apply here
62
63
        $dataProvider = new ActiveDataProvider([
64
            'query' => $query,
65
        ]);
66
67
        $this->load($params);
68
69
        if (!$this->validate()) {
70
            // uncomment the following line if you do not want to return any records when validation fails
71
            // $query->where('0=1');
72
            return $dataProvider;
73
        }
74
75
        // grid filtering conditions
76
        $query->andFilterWhere([
77
            'pageId' => $this->pageId,
78
            'active' => $this->active,
79
        ]);
80
81
        $query->andFilterWhere(['like', 'title', $this->title]);
82
83
        $pagination = new Pagination([
84
            'defaultPageSize' => Yii::$app->params['defaultPageSize'],
85
            'totalCount' => $query->count(),
86
        ]);
87
88
        $dataProvider->setPagination($pagination);
89
90
        return $dataProvider;
91
    }
92
}
93