ShouldNotifyTrait::setNotificationType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: xuan
6
 * Date: 10/17/15
7
 * Time: 4:36 PM.
8
 */
9
namespace PHPHub\Events;
10
11
use PHPHub\Reply;
12
use PHPHub\Topic;
13
14
/**
15
 * Class ShouldNotifyTrait.
16
 *
17
 * @method getBody
18
 * @method getUserId
19
 * @method getTopicId
20
 * @method getFromUserId
21
 * @method getType
22
 */
23
trait ShouldNotifyTrait
24
{
25
    protected $body = null;
26
    protected $topic_id = null;
27
    protected $reply_id = 0;
28
    protected $user_id = null;
29
    protected $from_user_id = null;
30
    protected $type = null;
31
32
    public function getReplyId()
33
    {
34
        return $this->reply_id ?: 0;
35
    }
36
37
    public function __call($method, $args)
38
    {
39
        $property = snake_case(str_replace('get', '', $method));
40
        if (! isset($this->$property) || $this->$property === null) {
41
            throw new \Exception('请在 Event 中设置 '.$property);
42
        }
43
44
        return $this->$property;
45
    }
46
47
    /**
48
     * 设置 Notification Type.
49
     *
50
     * @param $type
51
     */
52
    public function setNotificationType($type)
53
    {
54
        $this->type = $type;
55
    }
56
57
    /**
58
     * 从 Topic 对象初始化通知信息.
59
     *
60
     * @param Topic $topic
61
     * @param $from_user_id
62
     */
63
    public function setNotificationInfoFromTopic(Topic $topic, $from_user_id)
64
    {
65
        $this->body = $topic->body;
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<PHPHub\Topic>. 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...
66
        $this->topic_id = $topic->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<PHPHub\Topic>. 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...
67
        $this->user_id = $topic->user_id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<PHPHub\Topic>. 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...
68
        $this->from_user_id = $from_user_id;
69
70
        if (isset($this->notification_type)) {
71
            $this->type = $this->type ?: $this->notification_type;
0 ignored issues
show
Bug introduced by
The property notification_type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
        }
73
    }
74
75
    /**
76
     * 从 Reply 对象初始化通知信息.
77
     *
78
     * @param Reply $reply
79
     * @param $from_user_id
80
     * @param null $user_id
81
     */
82
    public function setNotificationInfoFromReply(Reply $reply, $from_user_id, $user_id = null)
83
    {
84
        $this->body = $reply->body;
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<PHPHub\Reply>. 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...
85
        $this->topic_id = $reply->topic_id;
0 ignored issues
show
Documentation introduced by
The property topic_id does not exist on object<PHPHub\Reply>. 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...
86
        $this->reply_id = $reply->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<PHPHub\Reply>. 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...
87
        $this->user_id = $user_id ?: $reply->topic()->pluck('user_id');
88
        $this->from_user_id = $from_user_id;
89
90
        if (isset($this->notification_type)) {
91
            $this->type = $this->type ?: $this->notification_type;
92
        }
93
    }
94
}
95