Completed
Push — master ( 193986...cdf399 )
by Andrey
11:47
created

Contact   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 161
Duplicated Lines 20.5 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 33
loc 161
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
B rules() 0 66 2
A attributeLabels() 17 17 1
A getDefaultContacts() 0 8 1
A beforeSave() 16 16 3
A getContactSocial() 0 6 1
A getSocial() 0 8 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
/**
6
 * This is the model class for table "contacts".
7
 *
8
 * @property int $id
9
 * @property int $default
10
 * @property string $title
11
 * @property string $address
12
 * @property string $email
13
 * @property string $phone
14
 * @property string $metaKeys
15
 * @property string $metaDescription
16
 * @property string $created_at
17
 * @property string $updated_at
18
 * @property string $mapQ
19
 * @property int $mapZoom
20
 *
21
 * @property ContactSocial[] $contactSocial
22
 * @property Social[] $social
23
 *
24
 * @package app\models
25
 */
26
class Contact extends ActiveRecord
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public static function tableName()
32
    {
33
        return 'contacts';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function rules()
40
    {
41
        return [
42
            [
43
                [
44
                    'title',
45
                ],
46
                'required'
47
            ],
48
            [
49
                [
50
                    'title',
51
                    'metaKeys',
52
                    'metaDescription'
53
                ],
54
                'string',
55
                'max' => 255
56
            ],
57
            [
58
                [
59
                    'address'
60
                ],
61
                'string',
62
                'max' => 128
63
            ],
64
            [
65
                [
66
                    'email'
67
                ],
68
                'string',
69
                'max' => 64
70
            ],
71
            [
72
                [
73
                    'phone'
74
                ],
75
                'string',
76
                'max' => 32
77
            ],
78
            [
79
                [
80
                    'default',
81
                    'mapZoom'
82
                ],
83
                'integer'
84
            ],
85
            [
86
                'mapQ',
87
                'string',
88
                'max' => 255
89
            ],
90
            [
91
                'title',
92
                'unique',
93
                'skipOnError'     => true,
94
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
95
            ],
96
            [
97
                [
98
                    'created_at',
99
                    'updated_at'
100
                ],
101
                'safe'
102
            ],
103
        ];
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 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...
110
    {
111
        return [
112
            'id' => 'ID',
113
            'default' => 'Default',
114
            'title' => 'Title',
115
            'address' => 'Address',
116
            'email' => 'Email',
117
            'phone' => 'Phone',
118
            'metaKeys' => 'Meta Keys',
119
            'metaDescription' => 'Meta Description',
120
            'created_at' => 'Created At',
121
            'updated_at' => 'Updated At',
122
            'mapQ' => 'Map place',
123
            'mapZoom' => 'Map zoom',
124
        ];
125
    }
126
127
    /**
128
     * Returns the default contacts record.
129
     *
130
     * @return array|null|\yii\db\ActiveRecord
131
     */
132
    public static function getDefaultContacts()
133
    {
134
        return static::find()
135
            ->where([
136
                'default' => 1
137
            ])
138
            ->one();
139
    }
140
141
    /**
142
     * Reset the default contacts record.
143
     *
144
     * @param boolean $insert
145
     *
146
     * @return mixed
147
     */
148 View Code Duplication
    public function beforeSave($insert)
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...
149
    {
150
        if ($this->default == 1) {
151
152
            $default = static::findOne([
153
                'default' => 1,
154
            ]);
155
156
            if (null !== $default) {
157
                $default->default = 0;
158
                $default->save();
159
            }
160
        }
161
162
        return parent::beforeSave($insert);
163
    }
164
165
    /**
166
     * @return \yii\db\ActiveQuery
167
     */
168
    public function getContactSocial()
169
    {
170
        return $this->hasMany(ContactSocial::class, [
171
            'contacts_id' => 'id'
172
        ]);
173
    }
174
175
    /**
176
     * @return \yii\db\ActiveQuery
177
     */
178
    public function getSocial()
179
    {
180
        return $this->hasMany(Social::class, [
181
            'id' => 'social_id'
182
        ])->viaTable('contacts_social', [
183
            'contacts_id' => 'id'
184
        ]);
185
    }
186
}
187