Answer   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 5.88 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 4
dl 11
loc 187
ccs 0
cts 148
cp 0
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A init() 0 5 1
A load() 0 7 1
A beforeChange() 0 6 1
A switchId() 0 6 2
A behaviors() 11 11 1
A attributes() 0 24 1
A rules() 0 28 1
A attributeLabels() 0 17 1
A getClient() 0 4 1
A getClient_id() 0 4 1
A getSeller() 0 4 1
A getSeller_id() 0 4 1
A prepareSpentTime() 0 4 1
A getThread() 0 4 1
A getFiles() 0 4 1
A scenarioActions() 0 6 1
A canSetSpent() 0 4 1
A isOpen() 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
 * HiPanel tickets module
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-ticket
6
 * @package   hipanel-module-ticket
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\ticket\models;
12
13
use hipanel\models\File;
14
use Yii;
15
16
/**
17
 * Class Answer.
18
 *
19
 * @property Thread $thread
20
 */
21
class Answer extends \hipanel\base\Model
22
{
23
    use \hipanel\base\ModelTrait;
24
25
    public static $i18nDictionary = 'hipanel:ticket';
26
27
    public static function tableName()
28
    {
29
        return 'thread';
30
    }
31
32
    public function init()
33
    {
34
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
35
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
36
    }
37
38
    public function load($data, $formName = null)
39
    {
40
        $result = parent::load($data, $formName);
41
        $this->prepareSpentTime();
42
43
        return $result;
44
    }
45
46
    public function beforeChange($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        $this->switchId();
49
50
        return true;
51
    }
52
53
    private function switchId()
54
    {
55
        if ($this->scenario === 'update') {
56
            $this->id = $this->answer_id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property answer_id does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
        }
58
    }
59
60
    /**
61
     * @return array
62
     */
63 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...
64
    {
65
        return [
66
            [
67
                'class' => 'hipanel\behaviors\File',
68
                'attribute' => 'file',
69
                'targetAttribute' => 'file_ids',
70
                'scenarios' => ['create', 'answer'],
71
            ],
72
        ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function attributes()
79
    {
80
        return [
81
            'id',
82
            'answer_id',
83
            'message',
84
            'private',
85
            'is_private',
86
            'is_answer',
87
            'changed_num',
88
            'author',
89
            'author_id',
90
            'author_seller',
91
            'seller_id',
92
            'account',
93
            'email_hash',
94
            'create_time',
95
            'spent',
96
            'spent_hours',
97
            'is_moved',
98
            'ip',
99
            'file',
100
        ];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function rules()
107
    {
108
        return [
109
            [
110
                ['id', 'answer_id', 'spent', 'spent_hours'],
111
                'integer',
112
                'enableClientValidation' => false,
113
            ],
114
            [
115
                ['message', 'id', 'answer_id'],
116
                'required',
117
                'on' => 'update',
118
            ],
119
            [
120
                ['spent', '!spent_hours'],
121
                'safe',
122
                'on' => 'update',
123
            ],
124
            [
125
                ['is_private'],
126
                'boolean',
127
                'on' => 'update',
128
                'when' => function () {
129
                    return Yii::$app->user->can('support');
130
                },
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function attributeLabels()
139
    {
140
        return $this->mergeAttributeLabels([
141
            'author' => Yii::t('hipanel:ticket', 'Author'),
142
            'author_id' => Yii::t('hipanel:ticket', 'Author'),
143
            'recipient' => Yii::t('hipanel:ticket', 'Recipient'),
144
            'is_private' => Yii::t('hipanel:ticket', 'Make private'),
145
            'responsible' => Yii::t('hipanel:ticket', 'Assignee'),
146
            'responsible_id' => Yii::t('hipanel:ticket', 'Assignee'),
147
            'spent' => Yii::t('hipanel:ticket', 'Spent time'),
148
            'create_time' => Yii::t('hipanel:ticket', 'Created'),
149
            'a_reply_time' => Yii::t('hipanel:ticket', 'a_reply_time'),
150
            'file' => Yii::t('hipanel:ticket', 'Files'),
151
            'lastanswer' => Yii::t('hipanel:ticket', 'Last answer'),
152
            'author_seller' => Yii::t('hipanel:ticket', 'Seller'),
153
        ]);
154
    }
155
156
    public function getClient()
157
    {
158
        return $this->author;
0 ignored issues
show
Documentation introduced by
The property author does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
    }
160
161
    public function getClient_id()
162
    {
163
        return $this->author_id;
0 ignored issues
show
Documentation introduced by
The property author_id does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
164
    }
165
166
    public function getSeller()
167
    {
168
        return $this->author_seller;
0 ignored issues
show
Documentation introduced by
The property author_seller does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
169
    }
170
171
    public function getSeller_id()
172
    {
173
        return $this->author_seller_id;
0 ignored issues
show
Documentation introduced by
The property author_seller_id does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
    }
175
176
    public function prepareSpentTime()
177
    {
178
        list($this->spent_hours, $this->spent) = explode(':', $this->spent, 2);
0 ignored issues
show
Documentation introduced by
The property spent_hours does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property spent does not exist on object<hipanel\modules\ticket\models\Answer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
179
    }
180
181
    public function getThread()
182
    {
183
        return $this->hasOne(Thread::class, ['id' => 'id']);
184
    }
185
186
    public function getFiles()
187
    {
188
        return $this->hasMany(File::class, ['object_id' => 'id']);
189
    }
190
191
    public function scenarioActions()
192
    {
193
        return [
194
            'update' => 'update-answer',
195
        ];
196
    }
197
198
    public function canSetSpent()
199
    {
200
        return $this->thread->canSetSpent();
201
    }
202
203
    public function isOpen()
204
    {
205
        return $this->thread->isOpen();
206
    }
207
}
208