| Total Complexity | 67 |
| Total Lines | 454 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseModel 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 BaseModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 129 | abstract class BaseModel |
||
| 130 | { |
||
| 131 | /** |
||
| 132 | * Nom de la table |
||
| 133 | * |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $table; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Cle primaire. |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $primaryKey = 'id'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Primary Key value when inserting and useAutoIncrement is false. |
||
| 147 | * |
||
| 148 | * @var int|string|null |
||
| 149 | */ |
||
| 150 | private $primaryKeyValue; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Groupe de la base de données a utiliser |
||
| 154 | * |
||
| 155 | * @var string |
||
| 156 | */ |
||
| 157 | protected $group; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Doit-on utiliser l'auto increment. |
||
| 161 | * |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | protected $useAutoIncrement = true; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Le type de colonne que created_at et updated_at sont censés avoir. |
||
| 168 | * |
||
| 169 | * Autorisé: 'datetime', 'date', 'int' |
||
| 170 | * |
||
| 171 | * @var string |
||
| 172 | */ |
||
| 173 | protected $dateFormat = 'datetime'; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Connexion à la base de données |
||
| 177 | * |
||
| 178 | * @var BaseConnection |
||
| 179 | */ |
||
| 180 | protected $db; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Query Builder |
||
| 184 | * |
||
| 185 | * @var BaseBuilder|null |
||
| 186 | */ |
||
| 187 | protected $builder; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Contient les informations transmises via 'set' afin que nous puissions les capturer (pas le constructeur) et nous assurer qu'elles sont validées en premier. |
||
| 191 | * |
||
| 192 | * @var array |
||
| 193 | */ |
||
| 194 | protected $tempData = []; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Tableau d'échappement qui mappe l'utilisation de l'indicateur d'échappement pour chaque paramètre. |
||
| 198 | * |
||
| 199 | * @var array |
||
| 200 | */ |
||
| 201 | protected $escape = []; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Methodes du builder qui ne doivent pas etre utilisees dans le model. |
||
| 205 | * |
||
| 206 | * @var string[] method name |
||
| 207 | */ |
||
| 208 | private array $builderMethodsNotAvailable = [ |
||
| 209 | 'getCompiledInsert', |
||
| 210 | 'getCompiledSelect', |
||
| 211 | 'getCompiledUpdate', |
||
| 212 | ]; |
||
| 213 | |||
| 214 | public function __construct(?ConnectionInterface $db = null) |
||
| 215 | { |
||
| 216 | $db ??= Database::connect($this->group); |
||
| 217 | |||
| 218 | $this->db = $db; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Fourni une instance partagee du Query Builder. |
||
| 223 | * |
||
| 224 | * @throws ModelException |
||
| 225 | */ |
||
| 226 | public function builder(?string $table = null): BaseBuilder |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Insere les données dans la base de données. |
||
| 265 | * Si un objet est fourni, il tentera de le convertir en un tableau. |
||
| 266 | * |
||
| 267 | * @param bool $returnID Si l'ID de l'element inséré doit être retourné ou non. |
||
| 268 | * |
||
| 269 | * @return BaseResult|int |
||
| 270 | * |
||
| 271 | * @throws ReflectionException |
||
| 272 | */ |
||
| 273 | public function create(array|object|null $data = null, bool $returnID = true) |
||
| 274 | { |
||
| 275 | if (! empty($this->tempData['data'])) { |
||
| 276 | if (empty($data)) { |
||
| 277 | $data = $this->tempData['data']; |
||
| 278 | } else { |
||
| 279 | $data = $this->transformDataToArray($data, 'insert'); |
||
| 280 | $data = array_merge($this->tempData['data'], $data); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if ($this->useAutoIncrement === false) { |
||
| 285 | if (is_array($data) && isset($data[$this->primaryKey])) { |
||
| 286 | $this->primaryKeyValue = $data[$this->primaryKey]; |
||
| 287 | } elseif (is_object($data) && isset($data->{$this->primaryKey})) { |
||
| 288 | $this->primaryKeyValue = $data->{$this->primaryKey}; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->escape = $this->tempData['escape'] ?? []; |
||
| 293 | $this->tempData = []; |
||
| 294 | |||
| 295 | /** @var BaseResult $inserted */ |
||
| 296 | $inserted = $this->builder()->insert($data); |
||
| 297 | |||
| 298 | if ($returnID) { |
||
| 299 | return $inserted->lastId(); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $inserted; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Met à jour un seul enregistrement dans la base de données. |
||
| 307 | * Si un objet est fourni, il tentera de le convertir en tableau. |
||
| 308 | * |
||
| 309 | * @param array|int|string|null $id |
||
| 310 | * @param array|object|null $data |
||
| 311 | * |
||
| 312 | * @throws ReflectionException |
||
| 313 | */ |
||
| 314 | public function modify($id = null, $data = null): bool |
||
| 315 | { |
||
| 316 | $id = $id ?: $this->primaryKeyValue; |
||
| 317 | |||
| 318 | if (! empty($this->tempData['data'])) { |
||
| 319 | if (empty($data)) { |
||
| 320 | $data = $this->tempData['data']; |
||
| 321 | } else { |
||
| 322 | $data = $this->transformDataToArray($data, 'update'); |
||
| 323 | $data = array_merge($this->tempData['data'], $data); |
||
| 324 | } |
||
| 325 | |||
| 326 | $id = $id ?: $this->idValue($data); |
||
| 327 | } |
||
| 328 | |||
| 329 | $this->escape = $this->tempData['escape'] ?? []; |
||
| 330 | $this->tempData = []; |
||
| 331 | |||
| 332 | return $this->builder()->whereIn($this->primaryKey, (array) $id)->update($data); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Une méthode pratique qui tentera de déterminer si les données doivent être insérées ou mises à jour. |
||
| 337 | * Fonctionnera avec un tableau ou un objet. |
||
| 338 | * Lors de l'utilisation avec des objets de classe personnalisés, vous devez vous assurer que la classe fournira l'accès aux variables de classe, même via une méthode magique. |
||
| 339 | */ |
||
| 340 | public function save(array|object $data): bool |
||
| 341 | { |
||
| 342 | if (empty($data)) { |
||
| 343 | return true; |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($this->shouldUpdate($data)) { |
||
| 347 | $response = $this->modify($this->idValue($data), $data); |
||
| 348 | } else { |
||
| 349 | $response = $this->create($data, false); |
||
| 350 | |||
| 351 | if ($response !== false) { |
||
| 352 | $response = true; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | return $response; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Fournit/instancie la connexion builder/db et les noms de table/clé primaire du modèle et le type de retour. |
||
| 361 | * |
||
| 362 | * @return mixed |
||
| 363 | */ |
||
| 364 | public function __get(string $name) |
||
| 365 | { |
||
| 366 | if (property_exists($this, $name)) { |
||
| 367 | return $this->{$name}; |
||
| 368 | } |
||
| 369 | |||
| 370 | if (isset($this->db->{$name})) { |
||
| 371 | return $this->db->{$name}; |
||
| 372 | } |
||
| 373 | |||
| 374 | if (isset($this->builder()->{$name})) { |
||
| 375 | return $this->builder()->{$name}; |
||
| 376 | } |
||
| 377 | |||
| 378 | return null; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Verifie si une propriete existe dans le modele, le builder, et la db connection. |
||
| 383 | */ |
||
| 384 | public function __isset(string $name): bool |
||
| 385 | { |
||
| 386 | if (property_exists($this, $name)) { |
||
| 387 | return true; |
||
| 388 | } |
||
| 389 | |||
| 390 | if (isset($this->db->{$name})) { |
||
| 391 | return true; |
||
| 392 | } |
||
| 393 | |||
| 394 | return isset($this->builder()->{$name}); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Fourni un acces direct a une methode du builder (si disponible) |
||
| 399 | * et la database connection. |
||
| 400 | * |
||
| 401 | * @return mixed |
||
| 402 | */ |
||
| 403 | public function __call(string $name, array $params) |
||
| 404 | { |
||
| 405 | $builder = $this->builder(); |
||
| 406 | $result = null; |
||
| 407 | |||
| 408 | if (method_exists($this->db, $name)) { |
||
| 409 | $result = $this->db->{$name}(...$params); |
||
| 410 | } elseif (method_exists($builder, $name)) { |
||
| 411 | $this->checkBuilderMethod($name); |
||
| 412 | |||
| 413 | $result = $builder->{$name}(...$params); |
||
| 414 | } else { |
||
| 415 | throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name); |
||
| 416 | } |
||
| 417 | |||
| 418 | if ($result instanceof BaseBuilder) { |
||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | return $result; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Renvoie la valeur id pour le tableau de données ou l'objet. |
||
| 427 | * |
||
| 428 | * @return array|int|string|null |
||
| 429 | */ |
||
| 430 | protected function idValue(array|object $data) |
||
| 431 | { |
||
| 432 | if (is_object($data) && isset($data->{$this->primaryKey})) { |
||
| 433 | return $data->{$this->primaryKey}; |
||
| 434 | } |
||
| 435 | |||
| 436 | if (is_array($data) && ! empty($data[$this->primaryKey])) { |
||
| 437 | return $data[$this->primaryKey]; |
||
| 438 | } |
||
| 439 | |||
| 440 | return null; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Cette méthode est appelée lors de la sauvegarde pour déterminer si l'entrée doit être mise à jour. |
||
| 445 | * Si cette méthode renvoie une opération d'insertion fausse, elle sera exécutée |
||
| 446 | */ |
||
| 447 | protected function shouldUpdate(array|object $data): bool |
||
| 448 | { |
||
| 449 | return ! empty($this->idValue($data)); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Prend une classe et retourne un tableau de ses propriétés publiques et protégées sous la forme d'un tableau adapté à une utilisation dans les créations et les mises à jour. |
||
| 454 | * Cette méthode utilise objectToRawArray() en interne et effectue la conversion en chaîne sur toutes les instances Time |
||
| 455 | * |
||
| 456 | * @param bool $onlyChanged Propriété modifiée uniquement |
||
| 457 | * @param bool $recursive Si vrai, les entités internes seront également converties en tableau |
||
| 458 | * |
||
| 459 | * @throws ReflectionException |
||
| 460 | */ |
||
| 461 | protected function objectToArray(object|string $data, bool $onlyChanged = true, bool $recursive = false): array |
||
| 462 | { |
||
| 463 | $properties = $this->objectToRawArray($data, $onlyChanged, $recursive); |
||
| 464 | |||
| 465 | // Convertissez toutes les instances de Date en $dateFormat approprié |
||
| 466 | if ($properties) { |
||
| 467 | $properties = array_map(function ($value) { |
||
| 468 | if ($value instanceof Date) { |
||
| 469 | return $this->timeToDate($value); |
||
| 470 | } |
||
| 471 | |||
| 472 | return $value; |
||
| 473 | }, $properties); |
||
| 474 | } |
||
| 475 | |||
| 476 | return $properties; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Prend une classe et renvoie un tableau de ses propriétés publiques et protégées sous la forme d'un tableau avec des valeurs brutes. |
||
| 481 | * |
||
| 482 | * @param bool $onlyChanged Propriété modifiée uniquement |
||
| 483 | * @param bool $recursive Si vrai, les entités internes seront également converties en tableau |
||
| 484 | * |
||
| 485 | * @throws ReflectionException |
||
| 486 | */ |
||
| 487 | protected function objectToRawArray(object|string $data, bool $onlyChanged = true, bool $recursive = false): ?array |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Convertit la valeur Date en chaîne en utilisant $this->dateFormat. |
||
| 513 | * |
||
| 514 | * Les formats disponibles sont : |
||
| 515 | * - 'int' - Stocke la date sous la forme d'un horodatage entier |
||
| 516 | * - 'datetime' - Stocke les données au format datetime SQL |
||
| 517 | * - 'date' - Stocke la date (uniquement) au format de date SQL. |
||
| 518 | * |
||
| 519 | * @return int|string |
||
| 520 | */ |
||
| 521 | protected function timeToDate(Date $value) |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Transformer les données en tableau. |
||
| 540 | * |
||
| 541 | * @param string $type Type de donnees (insert|update) |
||
| 542 | * |
||
| 543 | * @throws DataException |
||
| 544 | * @throws InvalidArgumentException |
||
| 545 | * @throws ReflectionException |
||
| 546 | */ |
||
| 547 | protected function transformDataToArray(array|object|null $data, string $type): array |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Verifie si la methode du builder peut etre utilisee dans le modele. |
||
| 579 | */ |
||
| 580 | private function checkBuilderMethod(string $name): void |
||
| 583 | // throw ModelException::forMethodNotAvailable(static::class, $name . '()'); |
||
| 584 | } |
||
| 585 | } |
||
| 586 | } |
||
| 587 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths