Completed
Push — master ( 229567...0d1fb0 )
by Pol
01:46
created

DynamicObjectsTrait::getDynamicMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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
        if (is_callable($value)) {
34 1
            $instance = new static();
35 1
            static::$dynamicProperties[get_called_class()][$name] = \Closure::bind($value, $instance, $instance);
36 1
        } else {
37 3
            static::$dynamicProperties[get_called_class()][$name] = $value;
38
        }
39 4
    }
40
41
    /**
42
     * Add a dynamic method.
43
     *
44
     * @param $name
45
     *   The method name.
46
     * @param \Closure $func
47
     *   The method.
48
     */
49 3
    public static function addDynamicMethod($name, \Closure $func)
50
    {
51 3
        $instance = new static();
52 3
        static::$dynamicMethods[get_called_class()][$name] = \Closure::bind($func, $instance, $instance);
53 3
    }
54
55
    /**
56
     * Check if a dynamic property exists.
57
     *
58
     * @param string $name
59
     *   The property name.
60
     * @return bool
61
     *   True if the property exists, false otherwise.
62
     */
63 6
    public static function hasDynamicProperty($name)
64
    {
65 6
        return isset(static::$dynamicProperties[get_called_class()][$name]);
66
    }
67
68
    /**
69
     * Check if a dynamic method exists.
70
     *
71
     * @param string $name
72
     *   The property name.
73
     * @return bool
74
     *   True if the property exists, false otherwise.
75
     */
76 4
    public static function hasDynamicMethod($name)
77
    {
78 4
        return isset(static::$dynamicMethods[get_called_class()][$name]);
79
    }
80
81
    /**
82
     * Get a dynamic property.
83
     *
84
     * @param $name
85
     *   The property name.
86
     * @return mixed|null
87
     *   The property value if it exists, null otherwise.
88
     */
89 2
    public static function getDynamicProperty($name)
90
    {
91 2
        if (static::hasDynamicProperty($name)) {
92 2
            return static::$dynamicProperties[get_called_class()][$name];
93
        }
94
95
        return null;
96
    }
97
98
    /**
99
     * Get a dynamic method.
100
     *
101
     * @param $name
102
     *   The method name.
103
     * @return mixed|null
104
     *   The method if it exists, null otherwise.
105
     */
106 1
    public static function getDynamicMethod($name)
107
    {
108 1
        return ( static::hasDynamicMethod($name) ) ?
109 1
            static::$dynamicMethods[get_called_class()][$name] : null;
110
    }
111
112
    /**
113
     * Clear dynamic properties.
114
     */
115 1
    public static function clearDynamicProperties()
116
    {
117 1
        static::$dynamicProperties[get_called_class()] = array();
118 1
    }
119
120
    /**
121
     * Clear dynamic methods.
122
     */
123 1
    public static function clearDynamicMethods()
124
    {
125 1
        static::$dynamicMethods[get_called_class()] = array();
126 1
    }
127
128
    /**
129
     * Remove a dynamic property.
130
     *
131
     * @param string $name
132
     *   The property name.
133
     */
134 1
    public static function removeDynamicProperty($name)
135
    {
136 1
        unset(static::$dynamicProperties[get_called_class()][$name]);
137 1
    }
138
139
    /**
140
     * Remove a dynamic method.
141
     *
142
     * @param string $name
143
     *   The method name.
144
     */
145 1
    public static function removeDynamicMethod($name)
146
    {
147 1
        unset(static::$dynamicMethods[get_called_class()][$name]);
148 1
    }
149
150
    /**
151
     * {inheritdoc}
152
     */
153 1
    public function __call($method, array $parameters = array())
154
    {
155 1
        if (static::hasDynamicMethod($method)) {
156 1
            return call_user_func_array(
157 1
                static::getDynamicMethod($method)->bindTo($this, get_called_class()),
158
                $parameters
159 1
            );
160
        }
161
    }
162
163
    /**
164
     * {inheritdoc}
165
     */
166 3
    public function __set($property, $value)
167
    {
168 3
        if (static::hasDynamicProperty($property)) {
169 1
            static::addDynamicProperty($property, $value);
170 1
        }
171 3
    }
172
173
    /**
174
     * {inheritdoc}
175
     */
176 2
    public function __get($property)
177
    {
178 2
        if (static::hasDynamicProperty($property)) {
179 2
            $value = static::getDynamicProperty($property);
180
181 2
            if (is_callable($value)) {
182 1
                $value = call_user_func_array(
183 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...
184 1
                    []
185 1
                );
186 1
            }
187
188 2
            return $value;
189
        }
190
    }
191
}
192