Comment   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 133
Duplicated Lines 12.03 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 19
c 8
b 2
f 1
lcom 3
cbo 5
dl 16
loc 133
ccs 0
cts 50
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 7 1
A rules() 0 10 1
A attributeLabels() 0 13 1
A isEdited() 0 4 1
A isDeleted() 0 4 1
A canCreate() 0 6 2
A canUpdate() 8 8 4
A canDelete() 8 8 4
A getAuthor() 0 4 1
A getLastUpdateAuthor() 0 4 1
A find() 0 7 1
A tableName() 0 4 1

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
 * Comment.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\module\Comments\models;
9
10
use rmrevin\yii\module\Comments;
11
use yii\behaviors\BlameableBehavior;
12
use yii\behaviors\TimestampBehavior;
13
14
/**
15
 * Class Comment
16
 * @package rmrevin\yii\module\Comments\models
17
 *
18
 * @property integer $id
19
 * @property string $entity
20
 * @property string $from
21
 * @property string $text
22
 * @property integer $deleted
23
 * @property integer $created_by
24
 * @property integer $updated_by
25
 * @property integer $created_at
26
 * @property integer $updated_at
27
 *
28
 * @property \yii\db\ActiveRecord $author
29
 * @property \yii\db\ActiveRecord $lastUpdateAuthor
30
 *
31
 * @method queries\CommentQuery hasMany(string $class, array $link) see BaseActiveRecord::hasMany() for more info
32
 * @method queries\CommentQuery hasOne(string $class, array $link) see BaseActiveRecord::hasOne() for more info
33
 */
34
class Comment extends \yii\db\ActiveRecord
35
{
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function behaviors()
41
    {
42
        return [
43
            'blameable' => BlameableBehavior::className(),
44
            'timestamp' => TimestampBehavior::className(),
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function rules()
52
    {
53
        return [
54
            [['text'], 'required'],
55
            [['from', 'text'], 'string'],
56
            [['created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'],
57
            [['deleted'], 'boolean'],
58
            [['deleted'], 'default', 'value' => self::NOT_DELETED],
59
        ];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function attributeLabels()
66
    {
67
        return [
68
            'id' => \Yii::t('app', 'ID'),
69
            'entity' => \Yii::t('app', 'Entity'),
70
            'from' => \Yii::t('app', 'Comment author'),
71
            'text' => \Yii::t('app', 'Text'),
72
            'created_by' => \Yii::t('app', 'Created by'),
73
            'updated_by' => \Yii::t('app', 'Updated by'),
74
            'created_at' => \Yii::t('app', 'Created at'),
75
            'updated_at' => \Yii::t('app', 'Updated at'),
76
        ];
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isEdited()
83
    {
84
        return $this->created_at !== $this->updated_at;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function isDeleted()
91
    {
92
        return $this->deleted === self::DELETED;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public static function canCreate()
99
    {
100
        return Comments\Module::instance()->useRbac === true
101
            ? \Yii::$app->getUser()->can(Comments\Permission::CREATE)
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
            : true;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 View Code Duplication
    public function canUpdate()
0 ignored issues
show
Duplication introduced by
This method 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...
109
    {
110
        $User = \Yii::$app->getUser();
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
112
        return Comments\Module::instance()->useRbac === true
113
            ? (\Yii::$app->getUser()->can(Comments\Permission::UPDATE) || \Yii::$app->getUser()->can(Comments\Permission::UPDATE_OWN, ['Comment' => $this]))
114
            : ($User->isGuest ? false : ($this->created_by === $User->id));
115
    }
116
117
    /**
118
     * @return bool
119
     */
120 View Code Duplication
    public function canDelete()
0 ignored issues
show
Duplication introduced by
This method 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...
121
    {
122
        $User = \Yii::$app->getUser();
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
124
        return Comments\Module::instance()->useRbac === true
125
            ? (\Yii::$app->getUser()->can(Comments\Permission::DELETE) || \Yii::$app->getUser()->can(Comments\Permission::DELETE_OWN, ['Comment' => $this]))
126
            : ($User->isGuest ? false : ($this->created_by === $User->id));
127
    }
128
129
    /**
130
     * @return queries\CommentQuery
131
     */
132
    public function getAuthor()
133
    {
134
        return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'created_by']);
135
    }
136
137
    /**
138
     * @return queries\CommentQuery
139
     */
140
    public function getLastUpdateAuthor()
141
    {
142
        return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'updated_by']);
143
    }
144
145
    /**
146
     * @return queries\CommentQuery
147
     */
148
    public static function find()
149
    {
150
        return \Yii::createObject(
151
            Comments\Module::instance()->model('commentQuery'),
152
            [get_called_class()]
153
        );
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public static function tableName()
160
    {
161
        return '{{%comment}}';
162
    }
163
164
    const NOT_DELETED = 0;
165
    const DELETED = 1;
166
}