Term   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 8
c 4
b 1
f 0
lcom 1
cbo 7
dl 33
loc 88
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A behaviors() 10 10 1
A rules() 11 11 1
A attributeLabels() 12 12 1
A getTaxonomy() 0 4 1
A getTermRelationships() 0 4 1
A getPosts() 0 5 1
A getUrl() 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
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace common\models;
9
10
use Yii;
11
use yii\behaviors\SluggableBehavior;
12
use yii\db\ActiveRecord;
13
14
/**
15
 * This is the model class for table "{{%term}}".
16
 *
17
 * @property integer $id
18
 * @property integer $taxonomy_id
19
 * @property string $name
20
 * @property string $slug
21
 * @property string $description
22
 * @property integer $parent
23
 * @property integer $count
24
 * @property string $url
25
 *
26
 * @property Taxonomy $taxonomy
27
 * @property TermRelationship[] $termRelationships
28
 * @property Post[] $posts
29
 *
30
 * @author Agiel K. Saputra <[email protected]>
31
 * @since 0.1.0
32
 */
33
class Term extends ActiveRecord
34
{
35
    /**
36
     * @inheritdoc
37
     */
38
    public static function tableName()
39
    {
40
        return '{{%term}}';
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 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...
47
    {
48
        return [
49
            [
50
                'class' => SluggableBehavior::className(),
51
                'attribute' => 'name',
52
                'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['slug']],
53
            ],
54
        ];
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 View Code Duplication
    public function rules()
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...
61
    {
62
        return [
63
            [['taxonomy_id', 'name'], 'required'],
64
            [['taxonomy_id', 'parent', 'count'], 'integer'],
65
            ['description', 'string'],
66
            [['name', 'slug'], 'string', 'max' => 200],
67
            ['name', 'unique'],
68
            ['slug', 'unique'],
69
        ];
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 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...
76
    {
77
        return [
78
            'id' => Yii::t('writesdown', 'ID'),
79
            'taxonomy_id' => Yii::t('writesdown', 'Taxonomy ID'),
80
            'name' => Yii::t('writesdown', 'Name'),
81
            'slug' => Yii::t('writesdown', 'Slug'),
82
            'description' => Yii::t('writesdown', 'Description'),
83
            'parent' => Yii::t('writesdown', 'Parent'),
84
            'count' => Yii::t('writesdown', 'Count'),
85
        ];
86
    }
87
88
    /**
89
     * @return \yii\db\ActiveQuery
90
     */
91
    public function getTaxonomy()
92
    {
93
        return $this->hasOne(Taxonomy::className(), ['id' => 'taxonomy_id']);
94
    }
95
96
    /**
97
     * @return \yii\db\ActiveQuery
98
     */
99
    public function getTermRelationships()
100
    {
101
        return $this->hasMany(TermRelationship::className(), ['term_id' => 'id']);
102
    }
103
104
    /**
105
     * @return \yii\db\ActiveQuery
106
     */
107
    public function getPosts()
108
    {
109
        return $this->hasMany(Post::className(), ['id' => 'post_id'])
110
            ->viaTable('{{%term_relationship}}', ['term_id' => 'id']);
111
    }
112
113
    /**
114
     * Get URL of current term
115
     */
116
    public function getUrl()
117
    {
118
        return Yii::$app->urlManagerFront->createAbsoluteUrl(['/term/view', 'id' => $this->id]);
119
    }
120
}
121