ThreadDecorator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\ticket\widgets;
4
5
use hipanel\modules\ticket\models\Thread;
6
use Yii;
7
use yii\base\InvalidParamException;
8
9
/**
10
 * Class ThreadDecorator
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 *
14
 * @property string $subject
15
 */
16
class ThreadDecorator
17
{
18
    /**
19
     * @var Thread
20
     */
21
    private $thread;
22
23
    function __construct(Thread $thread)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    {
25
        $this->thread = $thread;
26
    }
27
28
    public function __get($name)
29
    {
30
        if (method_exists($this, 'get' . ucfirst($name))) {
31
            return call_user_func([$this, 'get' . ucfirst($name)]);
32
        }
33
34
        return $this->thread->$name;
35
    }
36
37
    function __set($name, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39
        throw new InvalidParamException('Decorator should not be used for modifications');
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
40
    }
41
42
    public function getSubject()
43
    {
44
        $subject = $this->thread->subject;
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
45
        if (!empty($subject)) {
46
            return $subject;
47
        }
48
49
        return Yii::t('hipanel:ticket', '[No subject]');
50
    }
51
}
52