Completed
Push — master ( 0d1fb0...768aca )
by Pol
01:07
created

DynamicObjectsTrait::clearDynamicMethods()   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 0
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 6
    public static function hasDynamicProperty($name)
58
    {
59 6
        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 4
    public static function hasDynamicMethod($name)
71
    {
72 4
        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 2
    public static function getDynamicProperty($name)
84
    {
85 2
        if (static::hasDynamicProperty($name)) {
86 2
            return static::$dynamicProperties[get_called_class()][$name];
87
        }
88
89
        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 1
    public function __call($method, array $parameters = array())
148
    {
149 1
        if (static::hasDynamicMethod($method)) {
150 1
            return call_user_func_array(
151 1
                static::getDynamicMethod($method)->bindTo($this, get_called_class()),
152 1
                $parameters
153
            );
154
        }
155
    }
156
157
    /**
158
     * {inheritdoc}
159
     */
160 3
    public function __set($property, $value)
161
    {
162 3
        if (static::hasDynamicProperty($property)) {
163 1
            static::addDynamicProperty($property, $value);
164
        }
165 3
    }
166
167
    /**
168
     * {inheritdoc}
169
     */
170 2
    public function __get($property)
171
    {
172 2
        if (static::hasDynamicProperty($property)) {
173 2
            $value = static::getDynamicProperty($property);
174
175 2
            if (is_callable($value)) {
176 1
                $value = call_user_func_array(
177 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...
178 1
                    []
179
                );
180
            }
181
182 2
            return $value;
183
        }
184
    }
185
}
186