Completed
Push — master ( 83a9d9...63625d )
by Dmitry
15:17
created

ThreadDecorator::getSubject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
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');
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