Completed
Push — master ( 524df7...75795a )
by Pol
01:08
created

DynamicObjectsTrait::clearDynamicProperties()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
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 3
    public static function addDynamicProperty($name, $value)
32
    {
33 3
        static::$dynamicProperties[get_called_class()][$name] = $value;
34 3
    }
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 5
    public static function hasDynamicProperty($name)
58
    {
59 5
        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 1
    public static function getDynamicProperty($name)
84
    {
85 1
        return ( static::hasDynamicProperty($name) ) ?
86 1
            static::$dynamicProperties[get_called_class()][$name] : null;
87
    }
88
89
    /**
90
     * Get a dynamic method.
91
     *
92
     * @param $name
93
     *   The method name.
94
     * @return mixed|null
95
     *   The method if it exists, null otherwise.
96
     */
97 1
    public static function getDynamicMethod($name)
98
    {
99 1
        return ( static::hasDynamicMethod($name) ) ?
100 1
            static::$dynamicMethods[get_called_class()][$name] : null;
101
    }
102
103
    /**
104
     * Clear dynamic properties.
105
     */
106 1
    public static function clearDynamicProperties()
107
    {
108 1
        static::$dynamicProperties[get_called_class()] = array();
109 1
    }
110
111
    /**
112
     * Clear dynamic methods.
113
     */
114 1
    public static function clearDynamicMethods()
115
    {
116 1
        static::$dynamicMethods[get_called_class()] = array();
117 1
    }
118
119
    /**
120
     * Remove a dynamic property.
121
     *
122
     * @param string $name
123
     *   The property name.
124
     */
125 1
    public static function removeDynamicProperty($name)
126
    {
127 1
        unset(static::$dynamicProperties[get_called_class()][$name]);
128 1
    }
129
130
    /**
131
     * Remove a dynamic method.
132
     *
133
     * @param string $name
134
     *   The method name.
135
     */
136 1
    public static function removeDynamicMethod($name)
137
    {
138 1
        unset(static::$dynamicMethods[get_called_class()][$name]);
139 1
    }
140
141
    /**
142
     * {inheritdoc}
143
     */
144 1
    public function __call($method, $parameters)
145
    {
146 1
        if (static::hasDynamicMethod($method)) {
147 1
            array_unshift($parameters, $this);
148 1
            return call_user_func_array(static::getDynamicMethod($method), $parameters);
149
        }
150
151
        if ((bool)class_parents($this) !== false) {
152
            return parent::__call($method, $parameters);
153
        }
154
    }
155
156
    /**
157
     * {inheritdoc}
158
     */
159 2
    public function __set($property, $value)
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...
160
    {
161 2
        if (static::hasDynamicProperty($property)) {
162
            static::addDynamicProperty($property, $value);
163 2
        } elseif ((bool)class_parents($this) !== false) {
164
            return parent::__set($property, $value);
165
        } else {
166 2
            $this->{$property} = $value;
167
        }
168 2
    }
169
170
    /**
171
     * {inheritdoc}
172
     */
173 1
    public function __get($property)
174
    {
175 1
        if (static::hasDynamicProperty($property)) {
176 1
            return static::getDynamicProperty($property);
177
        } elseif ((bool)class_parents($this) !== false) {
178
            return parent::__get($property);
179
        } else {
180
            return $this->{$property};
181
        }
182
    }
183
}
184