PostComment::search()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 51
Code Lines 32

Duplication

Lines 51
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 51
loc 51
rs 9.4109
cc 3
eloc 32
nc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace common\models\search;
9
10
use common\models\PostComment as PostCommentModel;
11
use Yii;
12
use yii\base\Model;
13
use yii\data\ActiveDataProvider;
14
15
/**
16
 * PostComment represents the model behind the search form about `common\models\PostComment`.
17
 *
18
 * @author Agiel K. Saputra <[email protected]>
19
 * @since 0.1.0
20
 */
21 View Code Duplication
class PostComment extends PostCommentModel
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...
22
{
23
    /**
24
     * @inheritdoc
25
     */
26
    public function rules()
27
    {
28
        return [
29
            [['id', 'post_id', 'parent', 'user_id'], 'integer'],
30
            [['author', 'email', 'url', 'ip', 'date', 'content', 'status', 'agent', 'post_title'], 'safe'],
31
        ];
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function scenarios()
38
    {
39
        // bypass scenarios() implementation in the parent class
40
        return Model::scenarios();
41
    }
42
43
    /**
44
     * Creates data provider instance with search query applied
45
     *
46
     * @param array $params
47
     *
48
     * @param int $posttype Post type ID
49
     * @param int|null $post Post ID
50
     * @return ActiveDataProvider
51
     */
52
    public function search($params, $posttype, $post = null)
53
    {
54
        $query = PostCommentModel::find();
55
56
        $query->innerJoinWith([
57
            'commentPost' => function ($query) {
58
                /* @var $query \yii\db\ActiveQuery */
59
                return $query->from(['post' => Post::tableName()]);
60
            },
61
        ])->from(['postComment' => PostComment::tableName()]);
62
63
        $query->andWhere(['post.type' => $posttype]);
64
65
        if ($post) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
66
            $query->andWhere(['post.id' => $post]);
67
        }
68
69
        $dataProvider = new ActiveDataProvider([
70
            'query' => $query,
71
            'sort' => [
72
                'defaultOrder' => [
73
                    'id' => SORT_DESC,
74
                ],
75
            ],
76
        ]);
77
78
        $this->load($params);
79
80
        if (!$this->validate()) {
81
            return $dataProvider;
82
        }
83
84
        $query->andFilterWhere([
85
            'id' => $this->id,
86
            'post_id' => $this->post_id,
87
            'parent' => $this->parent,
88
            'user_id' => $this->user_id,
89
        ]);
90
91
        $query->andFilterWhere(['like', 'postComment.author', $this->author])
92
            ->andFilterWhere(['like', 'email', $this->email])
93
            ->andFilterWhere(['like', 'url', $this->url])
94
            ->andFilterWhere(['like', 'ip', $this->ip])
95
            ->andFilterWhere(['like', 'postComment.content', $this->content])
96
            ->andFilterWhere(['like', 'postComment.status', $this->status])
97
            ->andFilterWhere(['like', 'agent', $this->agent])
98
            ->andFilterWhere(['like', 'postComment.date', $this->date])
99
            ->andFilterWhere(['like', 'post.title', $this->post_title]);
100
101
        return $dataProvider;
102
    }
103
}
104