Total Complexity | 53 |
Total Lines | 230 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like MagicPropertiesTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MagicPropertiesTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | trait MagicPropertiesTrait |
||
56 | { |
||
57 | private array $attributes = []; |
||
58 | |||
59 | /** |
||
60 | * PHP getter magic method. |
||
61 | * |
||
62 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
63 | * |
||
64 | * @param string $name property name. |
||
65 | * |
||
66 | * @throws InvalidArgumentException|InvalidCallException|InvalidConfigException|ReflectionException|Throwable |
||
67 | * @throws UnknownPropertyException |
||
68 | * |
||
69 | * @throws Exception |
||
70 | * @return mixed property value. |
||
71 | * |
||
72 | * {@see getAttribute()} |
||
73 | */ |
||
74 | public function __get(string $name) |
||
75 | { |
||
76 | if ($this->hasAttribute($name)) { |
||
77 | return $this->getAttribute($name); |
||
78 | } |
||
79 | |||
80 | if ($this->isRelationPopulated($name)) { |
||
81 | return $this->getRelatedRecords()[$name]; |
||
82 | } |
||
83 | |||
84 | if (method_exists($this, $getter = 'get' . ucfirst($name))) { |
||
85 | /** read getter, e.g. getName() */ |
||
86 | return $this->$getter(); |
||
87 | } |
||
88 | |||
89 | if (method_exists($this, 'get' . ucfirst($name) . 'Query')) { |
||
90 | /** read relation query getter, e.g. getUserQuery() */ |
||
91 | return $this->retrieveRelation($name); |
||
92 | } |
||
93 | |||
94 | if (method_exists($this, 'set' . ucfirst($name))) { |
||
95 | throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); |
||
96 | } |
||
97 | |||
98 | throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Checks if a property value is null. |
||
103 | * |
||
104 | * This method overrides the parent implementation by checking if the named attribute is `null` or not. |
||
105 | * |
||
106 | * @param string $name the property name or the event name. |
||
107 | * |
||
108 | * @return bool whether the property value is null. |
||
109 | */ |
||
110 | public function __isset(string $name): bool |
||
111 | { |
||
112 | try { |
||
113 | return $this->__get($name) !== null; |
||
114 | } catch (InvalidCallException|UnknownPropertyException) { |
||
115 | return false; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Sets a component property to be null. |
||
121 | * |
||
122 | * This method overrides the parent implementation by clearing the specified attribute value. |
||
123 | * |
||
124 | * @param string $name the property name or the event name. |
||
125 | */ |
||
126 | public function __unset(string $name): void |
||
127 | { |
||
128 | if ($this->hasAttribute($name)) { |
||
129 | unset($this->attributes[$name]); |
||
130 | |||
131 | if (property_exists($this, $name)) { |
||
132 | $this->$name = null; |
||
133 | } |
||
134 | |||
135 | if ($this->hasDependentRelations($name)) { |
||
136 | $this->resetDependentRelations($name); |
||
137 | } |
||
138 | } elseif ($this->isRelationPopulated($name)) { |
||
139 | $this->resetRelation($name); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * PHP setter magic method. |
||
145 | * |
||
146 | * This method is overridden so that AR attributes can be accessed like properties. |
||
147 | * |
||
148 | * @param string $name property name. |
||
149 | * |
||
150 | * @throws InvalidCallException|UnknownPropertyException |
||
151 | */ |
||
152 | public function __set(string $name, mixed $value): void |
||
153 | { |
||
154 | if ($this->hasAttribute($name)) { |
||
155 | $this->setAttributeInternal($name, $value); |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | if (method_exists($this, $setter = 'set' . ucfirst($name))) { |
||
160 | $this->$setter($value); |
||
161 | return; |
||
162 | } |
||
163 | |||
164 | if ( |
||
165 | method_exists($this, 'get' . ucfirst($name)) |
||
166 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
167 | ) { |
||
168 | throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); |
||
169 | } |
||
170 | |||
171 | throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name); |
||
172 | } |
||
173 | |||
174 | public function getAttribute(string $name): mixed |
||
175 | { |
||
176 | if (property_exists($this, $name)) { |
||
177 | return get_object_vars($this)[$name] ?? null; |
||
178 | } |
||
179 | |||
180 | return $this->attributes[$name] ?? null; |
||
181 | } |
||
182 | |||
183 | public function getAttributes(array $names = null, array $except = []): array |
||
184 | { |
||
185 | $names ??= $this->attributes(); |
||
186 | $attributes = array_merge($this->attributes, get_object_vars($this)); |
||
187 | |||
188 | if ($except !== []) { |
||
189 | $names = array_diff($names, $except); |
||
190 | } |
||
191 | |||
192 | return array_intersect_key($attributes, array_flip($names)); |
||
193 | } |
||
194 | |||
195 | public function hasAttribute(string $name): bool |
||
196 | { |
||
197 | return isset($this->attributes[$name]) || in_array($name, $this->attributes(), true); |
||
198 | } |
||
199 | |||
200 | public function isAttributeChanged(string $name, bool $identical = true): bool |
||
201 | { |
||
202 | $hasOldAttribute = array_key_exists($name, $this->getOldAttributes()); |
||
203 | |||
204 | if (!$hasOldAttribute) { |
||
205 | return property_exists($this, $name) && array_key_exists($name, get_object_vars($this)) |
||
206 | || array_key_exists($name, $this->attributes); |
||
207 | } |
||
208 | |||
209 | if (property_exists($this, $name)) { |
||
210 | return $this->getOldAttribute($name) !== $this->$name; |
||
211 | } |
||
212 | |||
213 | return !array_key_exists($name, $this->attributes) |
||
214 | || $this->getOldAttribute($name) !== $this->attributes[$name]; |
||
215 | } |
||
216 | |||
217 | public function setAttribute(string $name, mixed $value): void |
||
218 | { |
||
219 | if ($this->hasAttribute($name)) { |
||
220 | $this->setAttributeInternal($name, $value); |
||
221 | } else { |
||
222 | throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".'); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns a value indicating whether a property is defined for this component. |
||
228 | * |
||
229 | * A property is defined if: |
||
230 | * |
||
231 | * - the class has a getter or setter method associated with the specified name (in this case, property name is |
||
232 | * case-insensitive). |
||
233 | * - the class has a member variable with the specified name (when `$checkVars` is true). |
||
234 | * |
||
235 | * @param string $name the property name. |
||
236 | * @param bool $checkVars whether to treat member variables as properties. |
||
237 | * |
||
238 | * @return bool whether the property is defined. |
||
239 | * |
||
240 | * {@see canGetProperty()} |
||
241 | * {@see canSetProperty()} |
||
242 | */ |
||
243 | public function hasProperty(string $name, bool $checkVars = true): bool |
||
244 | { |
||
245 | return method_exists($this, 'get' . ucfirst($name)) |
||
246 | || method_exists($this, 'set' . ucfirst($name)) |
||
247 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
248 | || ($checkVars && property_exists($this, $name)) |
||
249 | || $this->hasAttribute($name); |
||
250 | } |
||
251 | |||
252 | public function canGetProperty(string $name, bool $checkVars = true): bool |
||
253 | { |
||
254 | return method_exists($this, 'get' . ucfirst($name)) |
||
255 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
256 | || ($checkVars && property_exists($this, $name)) |
||
257 | || $this->hasAttribute($name); |
||
258 | } |
||
259 | |||
260 | public function canSetProperty(string $name, bool $checkVars = true): bool |
||
265 | } |
||
266 | |||
267 | protected function populateAttribute(string $name, mixed $value): void |
||
268 | { |
||
269 | if (property_exists($this, $name)) { |
||
270 | $this->$name = $value; |
||
271 | } else { |
||
272 | $this->attributes[$name] = $value; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | private function setAttributeInternal(string $name, mixed $value): void |
||
285 | } |
||
286 | } |
||
287 |