Screen   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 9.17 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 3
cbo 4
dl 11
loc 120
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 11 11 1
A attributeLabels() 0 11 1
A allFlows() 0 25 5
A setModified() 0 5 1
A getDeviceHasScreens() 0 4 1
A getDevices() 0 4 1
A getTemplate() 0 4 1
A getScreenHasFlows() 0 4 1
A getFlows() 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
use yii\db\Expression;
7
8
/**
9
 * This is the model class for table "screen".
10
 *
11
 * @property int $id
12
 * @property string $name
13
 * @property string $description
14
 * @property int $template_id
15
 * @property int $duration
16
 * @property string $last_changes
17
 * @property DeviceHasScreen[] $deviceHasScreens
18
 * @property Device[] $devices
19
 * @property ScreenTemplate $template
20
 * @property ScreenHasFlow[] $screenHasFlows
21
 * @property Flow[] $flows
22
 */
23
class Screen extends \yii\db\ActiveRecord
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function tableName()
29
    {
30
        return 'screen';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 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...
37
    {
38
        return [
39
            [['name', 'template_id'], 'required'],
40
            [['duration', 'template_id'], 'integer'],
41
            [['last_changes', 'template'], 'safe'],
42
            [['name'], 'string', 'max' => 64],
43
            [['description'], 'string', 'max' => 1024],
44
            [['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => ScreenTemplate::class, 'targetAttribute' => ['template_id' => 'id']],
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function attributeLabels()
52
    {
53
        return [
54
            'id' => Yii::t('app', 'ID'),
55
            'name' => Yii::t('app', 'Name'),
56
            'description' => Yii::t('app', 'Description'),
57
            'template_id' => Yii::t('app', 'Template'),
58
            'duration' => Yii::t('app', 'Duration'),
59
            'last_changes' => Yii::t('app', 'Last Changes'),
60
        ];
61
    }
62
63
    /**
64
     * Loop through parent flows to build a flows tree.
65
     *
66
     * @return array flows with all parents
67
     */
68
    public function allFlows()
69
    {
70
        $ret = [];
71
72
        // Initialize main loop
73
        $newFlows = $this->flows;
74
        while (count($newFlows)) {
75
            // Append latest loop parents
76
            $ret = array_merge($ret, $newFlows);
77
78
            $parents = [];
79
            foreach ($newFlows as $flow) {
80
                if ($flow->parent_id != null) {
81
                    $f = $flow->parent;
82
                    if ($f) {
83
                        $parents[] = $f;
84
                    }
85
                }
86
            }
87
            // Prepare next loop, will merge if necessary later
88
            $newFlows = $parents;
89
        }
90
91
        return $ret;
92
    }
93
94
    /**
95
     * Update last_modified field to force screen reload.
96
     */
97
    public function setModified()
98
    {
99
        $this->last_changes = new Expression('NOW()');
100
        $this->save();
101
    }
102
103
    /**
104
     * @return \yii\db\ActiveQuery
105
     */
106
    public function getDeviceHasScreens()
107
    {
108
        return $this->hasMany(DeviceHasScreen::class, ['screen_id' => 'id']);
109
    }
110
111
    /**
112
     * @return \yii\db\ActiveQuery
113
     */
114
    public function getDevices()
115
    {
116
        return $this->hasMany(Device::class, ['id' => 'device_id'])->viaTable('device_has_screen', ['screen_id' => 'id']);
117
    }
118
119
    /**
120
     * @return \yii\db\ActiveQuery
121
     */
122
    public function getTemplate()
123
    {
124
        return $this->hasOne(ScreenTemplate::class, ['id' => 'template_id']);
125
    }
126
127
    /**
128
     * @return \yii\db\ActiveQuery
129
     */
130
    public function getScreenHasFlows()
131
    {
132
        return $this->hasMany(ScreenHasFlow::class, ['screen_id' => 'id']);
133
    }
134
135
    /**
136
     * @return \yii\db\ActiveQuery
137
     */
138
    public function getFlows()
139
    {
140
        return $this->hasMany(Flow::class, ['id' => 'flow_id'])->viaTable('screen_has_flow', ['screen_id' => 'id']);
141
    }
142
}
143