Completed
Push — master ( cdf0ff...478cf1 )
by Pol
01:12
created

DynamicObjectsTrait::removeDynamicProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\DynamicObjects;
4
5
/**
6
 * Trait DynamicObjectsTrait.
7
 *
8
 * @package drupol\DynamicObjects
9
 */
10
trait DynamicObjectsTrait
11
{
12
    /**
13
     * @var array
14
     */
15
    protected static $dynamicMethods = array();
16
17
    /**
18
     * @var array
19
     */
20
    protected static $dynamicProperties = array();
21
22
    /**
23
     * Add a dynamic property.
24
     *
25
     * @param string $name
26
     *   The property name.
27
     * @param mixed $value
28
     *   The property value.
29
     *
30
     */
31 4
    public static function addDynamicProperty($name, $value)
32
    {
33 4
        static::$dynamicProperties[get_called_class()][$name] = $value;
34 4
    }
35
36
    /**
37
     * Add a dynamic method.
38
     *
39
     * @param $name
40
     *   The method name.
41
     * @param \Closure $func
42
     *   The method.
43
     */
44 3
    public static function addDynamicMethod($name, \Closure $func)
45
    {
46 3
        static::$dynamicMethods[get_called_class()][$name] = $func;
47 3
    }
48
49
    /**
50
     * Check if a dynamic property exists.
51
     *
52
     * @param string $name
53
     *   The property name.
54
     * @return bool
55
     *   True if the property exists, false otherwise.
56
     */
57 7
    public static function hasDynamicProperty($name)
58
    {
59 7
        return isset(static::$dynamicProperties[get_called_class()][$name]);
60
    }
61
62
    /**
63
     * Check if a dynamic method exists.
64
     *
65
     * @param string $name
66
     *   The property name.
67
     * @return bool
68
     *   True if the property exists, false otherwise.
69
     */
70 5
    public static function hasDynamicMethod($name)
71
    {
72 5
        return isset(static::$dynamicMethods[get_called_class()][$name]);
73
    }
74
75
    /**
76
     * Get a dynamic property.
77
     *
78
     * @param $name
79
     *   The property name.
80
     * @return mixed|null
81
     *   The property value if it exists, null otherwise.
82
     */
83 3
    public static function getDynamicProperty($name)
84
    {
85 3
        if (static::hasDynamicProperty($name)) {
86 2
            return static::$dynamicProperties[get_called_class()][$name];
87
        }
88
89 1
        return null;
90
    }
91
92
    /**
93
     * Get a dynamic method.
94
     *
95
     * @param $name
96
     *   The method name.
97
     * @return mixed|null
98
     *   The method if it exists, null otherwise.
99
     */
100 1
    public static function getDynamicMethod($name)
101
    {
102 1
        return ( static::hasDynamicMethod($name) ) ?
103 1
            static::$dynamicMethods[get_called_class()][$name] : null;
104
    }
105
106
    /**
107
     * Clear dynamic properties.
108
     */
109 1
    public static function clearDynamicProperties()
110
    {
111 1
        static::$dynamicProperties[get_called_class()] = array();
112 1
    }
113
114
    /**
115
     * Clear dynamic methods.
116
     */
117 1
    public static function clearDynamicMethods()
118
    {
119 1
        static::$dynamicMethods[get_called_class()] = array();
120 1
    }
121
122
    /**
123
     * Remove a dynamic property.
124
     *
125
     * @param string $name
126
     *   The property name.
127
     */
128 1
    public static function removeDynamicProperty($name)
129
    {
130 1
        unset(static::$dynamicProperties[get_called_class()][$name]);
131 1
    }
132
133
    /**
134
     * Remove a dynamic method.
135
     *
136
     * @param string $name
137
     *   The method name.
138
     */
139 1
    public static function removeDynamicMethod($name)
140
    {
141 1
        unset(static::$dynamicMethods[get_called_class()][$name]);
142 1
    }
143
144
    /**
145
     * {inheritdoc}
146
     */
147 2
    public function __call($method, array $parameters = array())
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
148
    {
149 2
        if (static::hasDynamicMethod($method)) {
150 1
            return static::getDynamicMethod($method)->bindTo($this, get_called_class())->__invoke($parameters);
151
        }
152
153 1
        throw new \BadMethodCallException(sprintf('Undefined method: %s().', $method));
154
    }
155
156
    /**
157
     * {inheritdoc}
158
     */
159 3
    public function __set($property, $value)
160
    {
161 3
        if (static::hasDynamicProperty($property)) {
162 1
            static::addDynamicProperty($property, $value);
163
        }
164 3
    }
165
166
    /**
167
     * {inheritdoc}
168
     */
169 2
    public function __get($property)
170
    {
171 2
        if (static::hasDynamicProperty($property)) {
172 2
            $value = static::getDynamicProperty($property);
173
174 2
            if (is_callable($value)) {
175 1
                $value = call_user_func_array(
176 1
                    $value->bindTo($this, get_called_class()),
0 ignored issues
show
Bug introduced by
The method bindTo cannot be called on $value (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177 1
                    []
178
                );
179
            }
180
181 2
            return $value;
182
        }
183
    }
184
}
185