Total Complexity | 46 |
Total Lines | 199 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 |
||
51 | trait MagicPropertiesTrait |
||
52 | { |
||
53 | /** @psalm-var array<string, mixed> $attributes */ |
||
54 | private array $attributes = []; |
||
55 | |||
56 | /** |
||
57 | * PHP getter magic method. |
||
58 | * |
||
59 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
60 | * |
||
61 | * @param string $name property name. |
||
62 | * |
||
63 | * @throws InvalidArgumentException|InvalidCallException|InvalidConfigException|ReflectionException|Throwable |
||
64 | * @throws UnknownPropertyException |
||
65 | * |
||
66 | * @throws Exception |
||
67 | * @return mixed property value. |
||
68 | * |
||
69 | * {@see getAttribute()} |
||
70 | */ |
||
71 | public function __get(string $name) |
||
72 | { |
||
73 | if ($this->hasAttribute($name)) { |
||
74 | return $this->getAttribute($name); |
||
|
|||
75 | } |
||
76 | |||
77 | if ($this->isRelationPopulated($name)) { |
||
78 | return $this->getRelatedRecords()[$name]; |
||
79 | } |
||
80 | |||
81 | if (method_exists($this, $getter = 'get' . ucfirst($name))) { |
||
82 | /** read getter, e.g. getName() */ |
||
83 | return $this->$getter(); |
||
84 | } |
||
85 | |||
86 | if (method_exists($this, 'get' . ucfirst($name) . 'Query')) { |
||
87 | /** read relation query getter, e.g. getUserQuery() */ |
||
88 | return $this->retrieveRelation($name); |
||
89 | } |
||
90 | |||
91 | if (method_exists($this, 'set' . ucfirst($name))) { |
||
92 | throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); |
||
93 | } |
||
94 | |||
95 | throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Checks if a property value is null. |
||
100 | * |
||
101 | * This method overrides the parent implementation by checking if the named attribute is `null` or not. |
||
102 | * |
||
103 | * @param string $name the property name or the event name. |
||
104 | * |
||
105 | * @return bool whether the property value is null. |
||
106 | */ |
||
107 | public function __isset(string $name): bool |
||
108 | { |
||
109 | try { |
||
110 | return $this->__get($name) !== null; |
||
111 | } catch (InvalidCallException|UnknownPropertyException) { |
||
112 | return false; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Sets a component property to be null. |
||
118 | * |
||
119 | * This method overrides the parent implementation by clearing the specified attribute value. |
||
120 | * |
||
121 | * @param string $name the property name or the event name. |
||
122 | */ |
||
123 | public function __unset(string $name): void |
||
124 | { |
||
125 | if ($this->hasAttribute($name)) { |
||
126 | unset($this->attributes[$name]); |
||
127 | |||
128 | if ($name !== 'attributes' && isset(get_object_vars($this)[$name])) { |
||
129 | $this->$name = null; |
||
130 | } |
||
131 | |||
132 | if ($this->hasDependentRelations($name)) { |
||
133 | $this->resetDependentRelations($name); |
||
134 | } |
||
135 | } elseif ($this->isRelationPopulated($name)) { |
||
136 | $this->resetRelation($name); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * PHP setter magic method. |
||
142 | * |
||
143 | * This method is overridden so that AR attributes can be accessed like properties. |
||
144 | * |
||
145 | * @param string $name property name. |
||
146 | * |
||
147 | * @throws InvalidCallException|UnknownPropertyException |
||
148 | */ |
||
149 | public function __set(string $name, mixed $value): void |
||
150 | { |
||
151 | if ($this->hasAttribute($name)) { |
||
152 | $this->setAttributeInternal($name, $value); |
||
153 | return; |
||
154 | } |
||
155 | |||
156 | if (method_exists($this, $setter = 'set' . ucfirst($name))) { |
||
157 | $this->$setter($value); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | if ( |
||
162 | method_exists($this, 'get' . ucfirst($name)) |
||
163 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
164 | ) { |
||
165 | throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); |
||
166 | } |
||
167 | |||
168 | throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name); |
||
169 | } |
||
170 | |||
171 | public function hasAttribute(string $name): bool |
||
172 | { |
||
173 | return isset($this->attributes[$name]) || in_array($name, $this->attributes(), true); |
||
174 | } |
||
175 | |||
176 | public function setAttribute(string $name, mixed $value): void |
||
177 | { |
||
178 | if ($this->hasAttribute($name)) { |
||
179 | $this->setAttributeInternal($name, $value); |
||
180 | } else { |
||
181 | throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".'); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns a value indicating whether a property is defined for this component. |
||
187 | * |
||
188 | * A property is defined if: |
||
189 | * |
||
190 | * - the class has a getter or setter method associated with the specified name (in this case, property name is |
||
191 | * case-insensitive). |
||
192 | * - the class has a member variable with the specified name (when `$checkVars` is true). |
||
193 | * |
||
194 | * @param string $name the property name. |
||
195 | * @param bool $checkVars whether to treat member variables as properties. |
||
196 | * |
||
197 | * @return bool whether the property is defined. |
||
198 | * |
||
199 | * {@see canGetProperty()} |
||
200 | * {@see canSetProperty()} |
||
201 | */ |
||
202 | public function hasProperty(string $name, bool $checkVars = true): bool |
||
203 | { |
||
204 | return method_exists($this, 'get' . ucfirst($name)) |
||
205 | || method_exists($this, 'set' . ucfirst($name)) |
||
206 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
207 | || ($checkVars && property_exists($this, $name)) |
||
208 | || $this->hasAttribute($name); |
||
209 | } |
||
210 | |||
211 | public function canGetProperty(string $name, bool $checkVars = true): bool |
||
212 | { |
||
213 | return method_exists($this, 'get' . ucfirst($name)) |
||
214 | || method_exists($this, 'get' . ucfirst($name) . 'Query') |
||
215 | || ($checkVars && property_exists($this, $name)) |
||
216 | || $this->hasAttribute($name); |
||
217 | } |
||
218 | |||
219 | public function canSetProperty(string $name, bool $checkVars = true): bool |
||
224 | } |
||
225 | |||
226 | /** @psalm-return array<string, mixed> */ |
||
227 | protected function getAttributesInternal(): array |
||
228 | { |
||
229 | return array_merge($this->attributes, parent::getAttributesInternal()); |
||
230 | } |
||
231 | |||
232 | protected function populateAttribute(string $name, mixed $value): void |
||
233 | { |
||
234 | if ($name !== 'attributes' && property_exists($this, $name)) { |
||
235 | $this->$name = $value; |
||
236 | } else { |
||
237 | $this->attributes[$name] = $value; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | private function setAttributeInternal(string $name, mixed $value): void |
||
250 | } |
||
251 | } |
||
252 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.