MediaComment   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 5
dl 78
loc 78
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 7 7 1
A scenarios() 5 5 1
A search() 48 48 3

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
 * @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\MediaComment as MediaCommentModel;
11
use Yii;
12
use yii\base\Model;
13
use yii\data\ActiveDataProvider;
14
15
/**
16
 * MediaComment represents the model behind the search form about `common\models\MediaComment`.
17
 *
18
 * @author Agiel K. Saputra <[email protected]>
19
 * @since 0.1.0
20
 */
21 View Code Duplication
class MediaComment extends MediaCommentModel
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', 'media_id', 'parent', 'user_id'], 'integer'],
30
            [['author', 'email', 'url', 'ip', 'date', 'content', 'status', 'agent', 'media_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
     * @param int|null $mediaId
48
     * @return ActiveDataProvider
49
     */
50
    public function search($params, $mediaId = null)
51
    {
52
        $query = MediaCommentModel::find();
53
        $query->innerJoinWith([
54
            'commentMedia' => function ($query) {
55
                /* @var $query \yii\db\ActiveQuery */
56
                return $query->from(['media' => Media::tableName()]);
57
            },
58
        ])->from(['mediaComment' => static::tableName()]);
59
60
        if ($mediaId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mediaId 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...
61
            $query->andWhere(['media.id' => $mediaId]);
62
        }
63
64
        $dataProvider = new ActiveDataProvider([
65
            'query' => $query,
66
            'sort' => [
67
                'defaultOrder' => [
68
                    'id' => SORT_DESC,
69
                ],
70
            ],
71
        ]);
72
73
        $this->load($params);
74
75
        if (!$this->validate()) {
76
            return $dataProvider;
77
        }
78
79
        $query->andFilterWhere([
80
            'id' => $this->id,
81
            'media_id' => $this->media_id,
82
            'parent' => $this->parent,
83
            'user_id' => $this->user_id,
84
        ]);
85
86
        $query->andFilterWhere(['like', 'mediaComment.author', $this->author])
87
            ->andFilterWhere(['like', 'email', $this->email])
88
            ->andFilterWhere(['like', 'url', $this->url])
89
            ->andFilterWhere(['like', 'ip', $this->ip])
90
            ->andFilterWhere(['like', 'mediaComment.content', $this->content])
91
            ->andFilterWhere(['like', 'status', $this->status])
92
            ->andFilterWhere(['like', 'agent', $this->agent])
93
            ->andFilterWhere(['like', 'mediaComment.date', $this->date])
94
            ->andFilterWhere(['like', 'media.title', $this->media_title]);
95
96
        return $dataProvider;
97
    }
98
}
99