Page   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 35.82 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 24
loc 67
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 12 12 1
A tableName() 0 4 1
A rules() 0 10 1
A attributeLabels() 12 12 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
namespace app\modules\page\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * This is the model class for table "pages".
11
 *
12
 * @property integer $id
13
 * @property string $key
14
 * @property string $title
15
 * @property string $content
16
 * @property string $description
17
 * @property string $created_at
18
 * @property string $updated_at
19
 */
20
class Page extends ActiveRecord
21
{
22
    /**
23
     * @inheritdoc
24
     */
25 4 View Code Duplication
    public function behaviors()
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...
26
    {
27
        return [
28
            'timestamp' => [
29 4
                'class' => TimestampBehavior::className(),
30
                'attributes' => [
31 4
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
32
                ],
33 4
                'value' => date('Y-m-d H:i:s'),
34 4
            ],
35
        ];
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41 6
    public static function tableName()
42
    {
43 6
        return 'pages';
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 1
    public function rules()
50
    {
51
        return [
52 1
            [['created_at', 'updated_at'], 'safe'],
53
            [['content'], 'string'],
54
            ['title', 'string', 'max' => 64],
55
            [['key', 'description'], 'string', 'max' => 255],
56
            [['key'], 'unique'],
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 3 View Code Duplication
    public function attributeLabels()
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...
64
    {
65
        return [
66 3
            'id' => Yii::t('page', 'ID'),
67 3
            'key' => Yii::t('page', 'Key'),
68 3
            'title' => Yii::t('page', 'Title'),
69 3
            'content' => Yii::t('page', 'Content'),
70 3
            'description' => Yii::t('page', 'Description'),
71 3
            'created_at' => Yii::t('page', 'Created At'),
72 3
            'updated_at' => Yii::t('page', 'Updated At'),
73
        ];
74
    }
75
76
    /**
77
     * Find page by key
78
     *
79
     * @param $key
80
     * @return $this|null
81
     */
82 3
    public static function findByKey($key)
83
    {
84 3
        return static::findOne(['key' => $key]);
85
    }
86
}
87