Total Complexity | 45 |
Total Lines | 311 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 3 | Features | 0 |
Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Model implements \Stringable, \IteratorAggregate, \ArrayAccess |
||
32 | { |
||
33 | use Iterator; |
||
34 | use Stringable; |
||
35 | use ArrayAccess; |
||
36 | use Getter; |
||
37 | use Setter; |
||
38 | |||
39 | /** |
||
40 | * Store all properties of model. |
||
41 | * |
||
42 | * @var array<string,array<string,mixed>> |
||
43 | */ |
||
44 | private array $__properties = []; |
||
45 | |||
46 | /** |
||
47 | * Store the table name of model. |
||
48 | */ |
||
49 | private string $table; |
||
50 | /** |
||
51 | * Store the metadata of model. |
||
52 | * |
||
53 | * @var array<string,mixed> |
||
54 | */ |
||
55 | private array $__meta = []; |
||
56 | |||
57 | /** |
||
58 | * Create a new model. |
||
59 | */ |
||
60 | public function __construct( |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * adds the key to properties. |
||
80 | */ |
||
81 | public function __set(string $key, mixed $val): void |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Adds the key to properties. |
||
88 | */ |
||
89 | public function set(string $key, mixed $val): void |
||
90 | { |
||
91 | if ('id' === $key) { |
||
92 | $this->__meta['id'] = $val; |
||
93 | $this->__meta['id_error'] = true; |
||
94 | } |
||
95 | |||
96 | if (\Safe\preg_match('/[A-Z]/', $key) !== 0) { |
||
97 | $parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
||
98 | if ('own' === strtolower((string) $parts[0])) { |
||
99 | $this->__meta['foreign_models']['otm'] = $this->createCollection($this->__meta['foreign_models']['otm'], $val); |
||
100 | $this->__properties['all'][$key] = $val; |
||
101 | |||
102 | return; |
||
103 | } |
||
104 | if ('shared' === strtolower((string) $parts[0])) { |
||
105 | $this->__meta['foreign_models']['mtm'] = $this->createCollection($this->__meta['foreign_models']['mtm'], $val); |
||
106 | $this->__properties['all'][$key] = $val; |
||
107 | |||
108 | return; |
||
109 | } |
||
110 | } |
||
111 | if ($val instanceof Model) { |
||
112 | if (isset($this->__properties['all'][$key.'_id'])) { |
||
113 | unset($this->__properties['all'][$key.'_id']); |
||
114 | } |
||
115 | $this->__meta['foreign_models']['oto'] = $this->createCollection($this->__meta['foreign_models']['oto'], Collection::fromIterable([$val])); |
||
116 | $this->__properties['all'][$key] = $val; |
||
117 | |||
118 | return; |
||
119 | } |
||
120 | |||
121 | $type = $this->getDataType($val); |
||
122 | |||
123 | $this->__properties['self'][$key] = $this->getDbValue($val,$type); |
||
124 | $this->__properties['all'][$key] = $val; |
||
125 | $this->__properties['type'][$key] = $type; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get a key from properties, keys can be relational |
||
130 | * like sharedList,ownList or foreign table. |
||
131 | */ |
||
132 | public function __get(string $key): mixed |
||
133 | { |
||
134 | return $this->get($key); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get a key from properties, keys can be relational |
||
139 | * like sharedList,ownList or foreign table. |
||
140 | */ |
||
141 | public function get(string $key): mixed |
||
142 | { |
||
143 | // retrun if alraedy loaded |
||
144 | if (array_key_exists($key, $this->__properties['all'])) { |
||
145 | return $this->__properties['all'][$key]; |
||
146 | } |
||
147 | |||
148 | if (\Safe\preg_match('/[A-Z]/', $key) !== 0) { |
||
149 | $parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
||
150 | if ('own' === strtolower((string) $parts[0]) && 'list' === strtolower((string) $parts[2])) { |
||
151 | $result = $this->recordManager->find(strtolower((string) $parts[1]))->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get(); |
||
152 | $this->set($key, $result); |
||
153 | return $result; |
||
154 | } |
||
155 | if ('shared' === strtolower((string) $parts[0]) && 'list' === strtolower((string) $parts[2])) { |
||
156 | $rel_table = $this->tableManager->tableExists($this->table.'_'.strtolower((string) $parts[1])) ? $this->table.'_'.strtolower((string) $parts[1]) : strtolower((string) $parts[1]).'_'.$this->table; |
||
157 | $relations = $this->recordManager->find($rel_table)->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get(); |
||
158 | $rel_ids = ''; |
||
159 | foreach ($relations as $relation) { |
||
160 | $key = strtolower((string) $parts[1]).'_id'; |
||
161 | $rel_ids .= "'".$relation->$key."',"; |
||
162 | } |
||
163 | $rel_ids = substr($rel_ids, 0, -1); |
||
164 | $result = $this->recordManager->find(strtolower((string) $parts[1]))->where('id IN ('.$rel_ids.')')->get(); |
||
165 | $this->set($key, $result); |
||
166 | return $result; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if (array_key_exists($key.'_id', $this->__properties['self'])) { |
||
171 | $result = $this->recordManager->getById($key, $this->__properties['self'][$key.'_id']); |
||
172 | $this->set($key, $result); |
||
173 | |||
174 | return $result; |
||
175 | } |
||
176 | |||
177 | throw new Exception\KeyNotFoundException(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Eager Load relation variable. |
||
182 | * |
||
183 | * @param array<string> $relations |
||
184 | */ |
||
185 | public function with(array $relations): Model |
||
186 | { |
||
187 | foreach ($relations as $relation) { |
||
188 | $this->get($relation); |
||
189 | } |
||
190 | |||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Unset a property from model. |
||
196 | */ |
||
197 | public function __unset(string $key): void |
||
198 | { |
||
199 | $this->unset($key); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Unset a property from model. |
||
204 | */ |
||
205 | public function unset(string $key): void |
||
206 | { |
||
207 | unset($this->__properties['self'][$key]); |
||
208 | unset($this->__properties['all'][$key]); |
||
209 | unset($this->__properties['type'][$key]); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Check if property exists. |
||
214 | */ |
||
215 | public function __isset(string $key): bool |
||
216 | { |
||
217 | return $this->isset($key); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Check if property exists. |
||
222 | */ |
||
223 | public function isset(string $key): bool |
||
224 | { |
||
225 | return array_key_exists($key, $this->__properties['all']); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * check if model loaded from db. |
||
230 | */ |
||
231 | public function isLoaded(): bool |
||
232 | { |
||
233 | return $this->__meta['is_loaded']; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Check if model has id error. |
||
238 | */ |
||
239 | public function hasIdError(): bool |
||
240 | { |
||
241 | return $this->__meta['id_error']; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Save model to database. |
||
246 | */ |
||
247 | public function save(): mixed |
||
248 | { |
||
249 | $this->id = $this->writeManager->save($this); |
||
250 | |||
251 | return $this->getId(); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Cleans up model internal state to be consistent after save. |
||
256 | */ |
||
257 | public function cleanModel(): void |
||
258 | { |
||
259 | $this->__properties['all'] = $this->__properties['self']; |
||
260 | $this->__meta['id_error'] = false; |
||
261 | $this->__meta['foreign_models']['otm'] = null; |
||
262 | $this->__meta['foreign_models']['oto'] = null; |
||
263 | $this->__meta['foreign_models']['mtm'] = null; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Delete model data. |
||
268 | */ |
||
269 | public function delete(): void |
||
270 | { |
||
271 | $this->recordManager->delete($this); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Function used to compare to models. |
||
276 | */ |
||
277 | public function equals(self $other): bool |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Check if model has any relations. |
||
284 | */ |
||
285 | public function hasForeign(string $type): bool |
||
286 | { |
||
287 | return !is_null($this->__meta['foreign_models'][$type]); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get the type of value. |
||
292 | */ |
||
293 | private function getDataType(mixed $value): string |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Check if array passed is instance of model. |
||
310 | * |
||
311 | * @param array<mixed>|Collection $models |
||
312 | * |
||
313 | * @throws Exception\InvalidModelException |
||
314 | */ |
||
315 | private function createCollection(?Collection $collection, array|Collection $models): Collection |
||
316 | { |
||
317 | if (is_null($collection)) { |
||
318 | $collection = Collection::fromIterable([]); |
||
319 | } |
||
320 | |||
321 | if ($models instanceof Collection) { |
||
|
|||
322 | return $collection->merge($models); |
||
323 | } |
||
324 | |||
325 | if (array_filter($models, fn ($d): bool => !$d instanceof Model) !== []) { |
||
326 | throw new Exception\InvalidModelException(); |
||
327 | } |
||
328 | |||
329 | return $collection->merge(Collection::fromIterable($models)); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Get the database value from PHP value. |
||
334 | */ |
||
335 | private function getDbValue(mixed $val,string $type): mixed |
||
342 | |||
343 | |||
344 | } |
||
345 | } |
||
346 |