| Total Complexity | 46 |
| Total Lines | 323 |
| 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 |
||
| 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 |
||
| 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 |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Eager Load relation variable. |
||
| 184 | * |
||
| 185 | * @param array<string> $relations |
||
| 186 | */ |
||
| 187 | public function with(array $relations): Model |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Refresh the current model from database. |
||
| 198 | */ |
||
| 199 | public function refresh(): void{ |
||
| 200 | $model = $this->recordManager->getById($this->getName(), $this->getId()); |
||
| 201 | $this->cleanModel(); |
||
| 202 | $this->setLoadedProperties($model->getSelfProperties()); |
||
| 203 | $this->setLoaded(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Unset a property from model. |
||
| 208 | */ |
||
| 209 | public function __unset(string $key): void |
||
| 210 | { |
||
| 211 | $this->unset($key); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Unset a property from model. |
||
| 216 | */ |
||
| 217 | public function unset(string $key): void |
||
| 218 | { |
||
| 219 | unset($this->__properties['self'][$key]); |
||
| 220 | unset($this->__properties['all'][$key]); |
||
| 221 | unset($this->__properties['type'][$key]); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Check if property exists. |
||
| 226 | */ |
||
| 227 | public function __isset(string $key): bool |
||
| 228 | { |
||
| 229 | return $this->isset($key); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check if property exists. |
||
| 234 | */ |
||
| 235 | public function isset(string $key): bool |
||
| 236 | { |
||
| 237 | return array_key_exists($key, $this->__properties['all']); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * check if model loaded from db. |
||
| 242 | */ |
||
| 243 | public function isLoaded(): bool |
||
| 244 | { |
||
| 245 | return $this->__meta['is_loaded']; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Check if model has id error. |
||
| 250 | */ |
||
| 251 | public function hasIdError(): bool |
||
| 252 | { |
||
| 253 | return $this->__meta['id_error']; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Save model to database. |
||
| 258 | */ |
||
| 259 | public function save(): mixed |
||
| 260 | { |
||
| 261 | $this->id = $this->writeManager->save($this); |
||
| 262 | |||
| 263 | return $this->getId(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Cleans up model internal state to be consistent after save. |
||
| 268 | */ |
||
| 269 | public function cleanModel(): void |
||
| 270 | { |
||
| 271 | $this->__properties['all'] = $this->__properties['self']; |
||
| 272 | $this->__meta['id_error'] = false; |
||
| 273 | $this->__meta['foreign_models']['otm'] = null; |
||
| 274 | $this->__meta['foreign_models']['oto'] = null; |
||
| 275 | $this->__meta['foreign_models']['mtm'] = null; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Delete model data. |
||
| 280 | */ |
||
| 281 | public function delete(): void |
||
| 282 | { |
||
| 283 | $this->recordManager->delete($this); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Function used to compare to models. |
||
| 288 | */ |
||
| 289 | public function equals(self $other): bool |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Check if model has any relations. |
||
| 296 | */ |
||
| 297 | public function hasForeign(string $type): bool |
||
| 298 | { |
||
| 299 | return !is_null($this->__meta['foreign_models'][$type]); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the type of value. |
||
| 304 | */ |
||
| 305 | private function getDataType(mixed $value): string |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Check if array passed is instance of model. |
||
| 322 | * |
||
| 323 | * @param array<mixed>|Collection $models |
||
| 324 | * |
||
| 325 | * @throws Exception\InvalidModelException |
||
| 326 | */ |
||
| 327 | private function createCollection(?Collection $collection, array|Collection $models): Collection |
||
| 328 | { |
||
| 329 | if (is_null($collection)) { |
||
| 330 | $collection = Collection::fromIterable([]); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ($models instanceof Collection) { |
||
|
|
|||
| 334 | return $collection->merge($models); |
||
| 335 | } |
||
| 336 | |||
| 337 | if ([] !== array_filter($models, fn ($d): bool => !$d instanceof Model)) { |
||
| 338 | throw new Exception\InvalidModelException(); |
||
| 339 | } |
||
| 340 | |||
| 341 | return $collection->merge(Collection::fromIterable($models)); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the database value from PHP value. |
||
| 346 | */ |
||
| 347 | private function getDbValue(mixed $val, string $type): mixed |
||
| 354 | } |
||
| 355 | } |
||
| 356 |