Total Complexity | 41 |
Total Lines | 283 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | 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 |
||
5 | class Model |
||
6 | { |
||
7 | private array $properties = array(); |
||
8 | private String $table; |
||
9 | private $_id = 0; |
||
10 | private array $__meta = []; |
||
11 | private \Scrawler\Arca\Database $db; |
||
12 | |||
13 | |||
14 | public function __construct(String $name, Database $db) |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * adds the key to properties |
||
29 | * |
||
30 | * @param String $key |
||
31 | * @param Mixed $val |
||
32 | */ |
||
33 | public function __set(string $key, mixed $val): void |
||
34 | { |
||
35 | $this->set($key,$val); |
||
36 | } |
||
37 | |||
38 | public function set(string $key,mixed $val): void |
||
39 | { |
||
40 | //bug: fix issue with bool storage |
||
41 | if (gettype($val) == 'boolean') { |
||
42 | ($val) ? $val = 1 : $val = 0; |
||
43 | } |
||
44 | |||
45 | if (preg_match('/[A-Z]/', $key)) { |
||
46 | $parts = preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
||
47 | if (strtolower($parts[0]) == 'own') { |
||
48 | if (gettype($val) == 'array') { |
||
49 | array_push($this->__meta['foreign_models']['otm'], $val); |
||
50 | $this->__meta['has_foreign']['otm'] = true; |
||
51 | } |
||
52 | return; |
||
53 | } |
||
54 | if (strtolower($parts[0]) == 'shared') { |
||
55 | if (gettype($val) == 'array') { |
||
56 | array_push($this->__meta['foreign_models']['mtm'], $val); |
||
57 | $this->__meta['has_foreign']['mtm'] = true; |
||
58 | } |
||
59 | return; |
||
60 | } |
||
61 | } |
||
62 | if ($val instanceof Model) { |
||
63 | $this->__meta['has_foreign']['oto'] = true; |
||
64 | array_push($this->__meta['foreign_models']['oto'], $val); |
||
65 | return; |
||
66 | } |
||
67 | |||
68 | $this->properties[$key] = $val; |
||
69 | } |
||
70 | |||
71 | public function __get(string $key): mixed |
||
72 | { |
||
73 | return $this->get($key); |
||
74 | } |
||
75 | |||
76 | public function get(string $key){ |
||
77 | |||
78 | if (preg_match('/[A-Z]/', $key)) { |
||
79 | $parts = preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY); |
||
80 | if (strtolower($parts[0]) == 'own') { |
||
81 | if (strtolower($parts[2]) == 'list') { |
||
82 | return $this->db->find(strtolower($parts[1]))->where($this->getName() . '_id = "' . $this->_id.'"')->get(); |
||
83 | } |
||
84 | } |
||
85 | if (strtolower($parts[0]) == 'shared') { |
||
86 | if (strtolower($parts[2]) == 'list') { |
||
87 | $rel_table = $this->db->getTableManager()->tableExists($this->table.'_'.strtolower($parts[1])) ? $this->table.'_'.strtolower($parts[1]) : strtolower($parts[1]).'_'.$this->table; |
||
88 | $relations = $this->db->find($rel_table)->where($this->getName() . '_id = "' . $this->_id.'"')->get(); |
||
89 | $rel_ids = ''; |
||
90 | foreach ($relations as $relation) { |
||
91 | $key = strtolower($parts[1]) . '_id'; |
||
92 | $rel_ids .= "'".$relation->$key . "',"; |
||
93 | } |
||
94 | $rel_ids = substr($rel_ids, 0, -1); |
||
95 | return $this->db->find(strtolower($parts[1]))->where('id IN ('.$rel_ids.')')->get(); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if (array_key_exists($key, $this->properties)) { |
||
101 | return $this->properties[$key]; |
||
102 | } |
||
103 | |||
104 | if (array_key_exists($key.'_id', $this->properties)) { |
||
105 | return $this->db->get($key, $this->properties[$key.'_id']); |
||
106 | } |
||
107 | |||
108 | throw new Exception\KeyNotFoundException(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Unset a property from model |
||
113 | * |
||
114 | * @param string $key |
||
115 | */ |
||
116 | public function __unset(string $key): void |
||
117 | { |
||
118 | unset($this->properties[$key]); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Check if property exists |
||
123 | * |
||
124 | * @param string $key |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public function __isset(string $key): bool |
||
128 | { |
||
129 | return $this->isset($key); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Check if property exists |
||
134 | * |
||
135 | * @param string $key |
||
136 | * @return boolean |
||
137 | */ |
||
138 | public function isset(string $key): bool |
||
139 | { |
||
140 | return array_key_exists($key, $this->properties); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set all properties of model via array |
||
145 | * |
||
146 | * @param array $properties |
||
147 | * @return Model |
||
148 | */ |
||
149 | public function setProperties(array $properties) : Model |
||
150 | { |
||
151 | $this->properties = array_merge($this->properties, $properties); |
||
152 | if (isset($properties['id'])) { |
||
153 | $this->_id = $properties['id']; |
||
154 | } |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get all properties in array form |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getProperties() : array |
||
164 | { |
||
165 | return $this->properties; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get all properties in array form |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function toArray() : array |
||
174 | { |
||
175 | return $this->getProperties(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * check if model loaded from db |
||
180 | * @return array |
||
181 | */ |
||
182 | public function isLoaded() : bool |
||
183 | { |
||
184 | return $this->__meta['is_loaded']; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * call when model is loaded from database |
||
189 | * |
||
190 | * @return Model |
||
191 | */ |
||
192 | public function setLoaded() : Model |
||
193 | { |
||
194 | $this->__meta['is_loaded'] = true; |
||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get current table name of model |
||
200 | * |
||
201 | * @return String |
||
202 | */ |
||
203 | public function getName() : String |
||
204 | { |
||
205 | return $this->table; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Get current model Id or UUID |
||
210 | * |
||
211 | * @return mixed |
||
212 | */ |
||
213 | public function getId() : mixed |
||
214 | { |
||
215 | return $this->_id; |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Save model to database |
||
221 | * |
||
222 | * @return mixed returns int when id is used else returns string for uuid |
||
223 | */ |
||
224 | public function save() : mixed |
||
225 | { |
||
226 | $id = $this->db->save($this); |
||
227 | $this->id = $id; |
||
|
|||
228 | $this->_id = $id; |
||
229 | return $id; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Delete model data |
||
234 | */ |
||
235 | public function delete() : void |
||
236 | { |
||
237 | $this->db->delete($this); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * converts model into json object |
||
242 | * @return string |
||
243 | */ |
||
244 | public function toString() : string |
||
245 | { |
||
246 | return \json_encode($this->properties); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function __toString() : string |
||
254 | { |
||
255 | return $this->toString(); |
||
256 | } |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Function used to compare to models |
||
261 | * |
||
262 | * @param self $other |
||
263 | * @return boolean |
||
264 | */ |
||
265 | public function equals(self $other): bool |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Check if model has any relations |
||
272 | * |
||
273 | * @return boolean |
||
274 | */ |
||
275 | public function hasForeign($type) : bool |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * returns all relational models |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getForeignModels($type): array |
||
286 | { |
||
287 | return $this->__meta['foreign_models'][$type]; |
||
288 | } |
||
289 | } |
||
290 |