Completed
Push — master ( 61b75c...99e722 )
by Jeff
03:37
created

ScreenTemplate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 13.25 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 5
dl 11
loc 83
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 11 11 1
A attributeLabels() 0 10 1
A afterSave() 0 7 2
A getFields() 0 4 1
A getScreens() 0 4 1
A getBackground() 0 4 1
A setBackground() 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
namespace app\models;
4
5
use Yii;
6
7
/**
8
 * This is the model class for table "screen_template".
9
 *
10
 * @property int $id
11
 * @property string $name
12
 * @property int $background_id
13
 * @property string $css
14
 * @property Field[] $fields
15
 * @property Screen[] $screens
16
 * @property TemplateBackground $background
17
 */
18
class ScreenTemplate extends \yii\db\ActiveRecord
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public static function tableName()
24
    {
25
        return 'screen_template';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 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...
32
    {
33
        return [
34
            [['name'], 'required'],
35
            [['background_id'], 'integer'],
36
            [['css'], 'string'],
37
            [['name'], 'string', 'max' => 64],
38
            [['background'], 'safe'],
39
            [['background_id'], 'exist', 'skipOnError' => true, 'targetClass' => TemplateBackground::className(), 'targetAttribute' => ['background_id' => 'id']],
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function attributeLabels()
47
    {
48
        return [
49
            'id' => Yii::t('app', 'ID'),
50
            'name' => Yii::t('app', 'Name'),
51
            'background_id' => Yii::t('app', 'Background'),
52
            'background' => Yii::t('app', 'Background'),
53
            'css' => Yii::t('app', 'CSS'),
54
        ];
55
    }
56
57
    /**
58
     * After save event
59
     * Set screen last_modified field on changes to force screen reload.
60
     *
61
     * @param bool  $insert            is model inserted
62
     * @param array $changedAttributes
63
     */
64
    public function afterSave($insert, $changedAttributes)
65
    {
66
        parent::afterSave($insert, $changedAttributes);
67
        foreach ($this->screens as $screen) {
68
            $screen->setModified();
69
        }
70
    }
71
72
    /**
73
     * @return \yii\db\ActiveQuery
74
     */
75
    public function getFields()
76
    {
77
        return $this->hasMany(Field::className(), ['template_id' => 'id']);
78
    }
79
80
    /**
81
     * @return \yii\db\ActiveQuery
82
     */
83
    public function getScreens()
84
    {
85
        return $this->hasMany(Screen::className(), ['template_id' => 'id']);
86
    }
87
88
    /**
89
     * @return \yii\db\ActiveQuery
90
     */
91
    public function getBackground()
92
    {
93
        return $this->hasOne(TemplateBackground::className(), ['id' => 'background_id']);
94
    }
95
96
    public function setBackground($backgroundId)
97
    {
98
        $this->background_id = $backgroundId;
99
    }
100
}
101