| Total Complexity | 42 |
| Total Lines | 288 |
| 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 |
||
| 119 | } |
||
| 120 | |||
| 121 | public function unset(string $key): void |
||
| 122 | { |
||
| 123 | unset($this->properties[$key]); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Check if property exists |
||
| 128 | * |
||
| 129 | * @param string $key |
||
| 130 | * @return boolean |
||
| 131 | */ |
||
| 132 | public function __isset(string $key): bool |
||
| 133 | { |
||
| 134 | return $this->isset($key); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Check if property exists |
||
| 139 | * |
||
| 140 | * @param string $key |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | public function isset(string $key): bool |
||
| 144 | { |
||
| 145 | return array_key_exists($key, $this->properties); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set all properties of model via array |
||
| 150 | * |
||
| 151 | * @param array $properties |
||
| 152 | * @return Model |
||
| 153 | */ |
||
| 154 | public function setProperties(array $properties) : Model |
||
| 155 | { |
||
| 156 | $this->properties = array_merge($this->properties, $properties); |
||
| 157 | if (isset($properties['id'])) { |
||
| 158 | $this->_id = $properties['id']; |
||
| 159 | } |
||
| 160 | return $this; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get all properties in array form |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function getProperties() : array |
||
| 169 | { |
||
| 170 | return $this->properties; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get all properties in array form |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function toArray() : array |
||
| 179 | { |
||
| 180 | return $this->getProperties(); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * check if model loaded from db |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function isLoaded() : bool |
||
| 188 | { |
||
| 189 | return $this->__meta['is_loaded']; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * call when model is loaded from database |
||
| 194 | * |
||
| 195 | * @return Model |
||
| 196 | */ |
||
| 197 | public function setLoaded() : Model |
||
| 198 | { |
||
| 199 | $this->__meta['is_loaded'] = true; |
||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get current table name of model |
||
| 205 | * |
||
| 206 | * @return String |
||
| 207 | */ |
||
| 208 | public function getName() : String |
||
| 209 | { |
||
| 210 | return $this->table; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get current model Id or UUID |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | public function getId() : mixed |
||
| 219 | { |
||
| 220 | return $this->_id; |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Save model to database |
||
| 226 | * |
||
| 227 | * @return mixed returns int when id is used else returns string for uuid |
||
| 228 | */ |
||
| 229 | public function save() : mixed |
||
| 230 | { |
||
| 231 | $id = $this->db->save($this); |
||
| 232 | $this->id = $id; |
||
|
|
|||
| 233 | $this->_id = $id; |
||
| 234 | return $id; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Delete model data |
||
| 239 | */ |
||
| 240 | public function delete() : void |
||
| 241 | { |
||
| 242 | $this->db->delete($this); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * converts model into json object |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function toString() : string |
||
| 250 | { |
||
| 251 | return \json_encode($this->properties); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function __toString() : string |
||
| 259 | { |
||
| 260 | return $this->toString(); |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Function used to compare to models |
||
| 266 | * |
||
| 267 | * @param self $other |
||
| 268 | * @return boolean |
||
| 269 | */ |
||
| 270 | public function equals(self $other): bool |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Check if model has any relations |
||
| 277 | * |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | public function hasForeign($type) : bool |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * returns all relational models |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getForeignModels($type): array |
||
| 291 | { |
||
| 292 | return $this->__meta['foreign_models'][$type]; |
||
| 293 | } |
||
| 294 | } |
||
| 295 |