Passed
Push — master ( 151829...e135ef )
by Francis
02:02
created

Resource::set_called_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LiveStream\Resources;
4
5
use BadMethodCallException;
6
use stdClass;
7
8
class Resource
9
{
10
    /**
11
     * Resource Payload
12
     *
13
     * @var object
14
     */
15
    private $data;
16
17
    /**
18
     * Class Constructor.
19
     *
20
     * @param boolean $init
21
     */
22
    public function __construct(bool $init = true)
23
    {
24
        if ($init) $this->data = new stdClass();
25
    }
26
27
    /**
28
     * Factory Method.
29
     *
30
     * @param  object $object
31
     * @return \LiveStream\Resources\Resource
32
     */
33
    public static function fromObject(object $object): Resource
34
    {
35
        $instance = new static(false);
36
        $instance->data = $object;
37
        return $instance;
38
    }
39
40
    /**
41
     * Magic Setter Method
42
     *
43
     * @param  string $key
44
     * @param  mixed  $value
45
     * @return void
46
     */
47
    public function __set(string $key, $value): void
48
    {
49
        $this->data->$key = $value;
50
    }
51
52
    /**
53
     * Magic Getter Method
54
     *
55
     * @param  string $key
56
     * @return void
57
     */
58
    public function __get(string $key)
59
    {
60
        return $this->data->$key ?? null;
61
    }
62
63
    /**
64
     * Magic Method Isset.
65
     *
66
     * @param  string  $key
67
     * @return boolean
68
     */
69
    public function __isset(string $key)
70
    {
71
        return isset($this->data->$key);
72
    }
73
74
    /**
75
     * Undocumented function
76
     *
77
     * @param string $name
78
     * @param array $arguments
79
     * @return void
80
     */
81
    public function __call(string $name, array $arguments)
82
    {
83
        if (substr($name, 0, 3) == 'get') {
84
            return $this->get_called_value($name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->get_called_value($name) targeting LiveStream\Resources\Resource::get_called_value() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        } elseif (substr($name, 0, 3) == 'set') {
86
            $this->set_called_value($name, $arguments[0]);
87
            return;
88
        }
89
90
        throw new BadMethodCallException("Function '$name' does not exist.");
91
    }
92
93
    /**
94
     * Get called value from __call by function name.
95
     *
96
     * @param  string $name
97
     * @param  string $function
98
     * @return void
99
     */
100
    private function get_called_value(string $function)
101
    {
102
        if (!isset($this->data->{lcfirst(substr($function, 3, strlen($function) - 3))}))
103
            throw new BadMethodCallException("Function '$function' does not exist.");
104
105
        return $this->data->{lcfirst(substr($function, 3, strlen($function) - 3))};
106
    }
107
108
    /**
109
     * Undocumented function
110
     *
111
     * @param string $function
112
     * @param [type] $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
113
     * @return void
114
     */
115
    private function set_called_value(string $function, $value): void
116
    {
117
        if (!isset($this->data->{lcfirst(substr($function, 3, strlen($function) - 3))}))
118
            throw new BadMethodCallException("Function '$function' does not exist.");
119
120
        $this->data->{lcfirst(substr($function, 3, strlen($function) - 3))} = $value;
121
    }
122
}
123