Issues (9)

src/DTOs/CommandData.php (3 issues)

1
<?php
2
3
namespace Mostafaznv\LaraCache\DTOs;
4
5
use Mostafaznv\LaraCache\Exceptions\EntityIsNotAllowed;
6
use Mostafaznv\LaraCache\Exceptions\ModelDoesntUseLaraCacheTrait;
7
use Mostafaznv\LaraCache\Exceptions\ModelDoestNotExist;
8
9
class CommandData
10
{
11
    public function __construct(
12
        /**
13
         * @var \Mostafaznv\LaraCache\Traits\LaraCache[] $models
14
         */
15
        public array $models,
16
        public array $entities
17
    ) {}
18
19
    public static function make(array $models, array $entities = []): self
20
    {
21
        if (count($models) > 1 and count($entities)) {
22
            throw EntityIsNotAllowed::make();
23
        }
24
25
        $m = [];
26
27
        foreach ($models as $model) {
28
            $m[] = self::model($model);
29
        }
30
31
        return new static($m, $entities);
32
    }
33
34
35
    private static function model(?string $model): string
36
    {
37
        if ($model) {
38
            if (self::modelExists($model)) {
39
                return $model;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model returns the type object which is incompatible with the type-hinted return string.
Loading history...
40
            }
41
42
            // @codeCoverageIgnoreStart
43
            $defaultPath = "App\\Models\\$model";
44
45
            if (self::modelExists($defaultPath)) {
46
                return $defaultPath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $defaultPath returns the type object which is incompatible with the type-hinted return string.
Loading history...
47
            }
48
            // @codeCoverageIgnoreEnd
49
        }
50
51
        throw ModelDoestNotExist::make($model);
0 ignored issues
show
It seems like $model can also be of type null and object; however, parameter $model of Mostafaznv\LaraCache\Exc...elDoestNotExist::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        throw ModelDoestNotExist::make(/** @scrutinizer ignore-type */ $model);
Loading history...
52
    }
53
54
    private static function modelExists(string $model): bool
55
    {
56
        if (class_exists($model)) {
57
            if (method_exists($model, 'cache')) {
58
                return true;
59
            }
60
61
            throw ModelDoesntUseLaraCacheTrait::make($model);
62
        }
63
64
        return false;
65
    }
66
}
67