IdentifiesEloquentModels   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isModel() 0 3 2
A getModelClassNameAndId() 0 12 2
1
<?php
2
3
namespace Codefocus\ManagedCache\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
trait IdentifiesEloquentModels
8
{
9
    /**
10
     * Return whether the specified class name is an Eloquent Model.
11
     *
12
     * @param mixed $value
13
     *
14
     * @return bool
15
     */
16
    protected function isModel($value): bool
17
    {
18
        return is_object($value) && is_subclass_of($value, Model::class);
19
    }
20
21
    /**
22
     * Returns an array containing the Model class name, and the model id.
23
     * The id is null if $model is a string.
24
     *
25
     * @param Model|string $model
26
     * @param int|null $modelId (default: null)
27
     *
28
     * @return array
29
     */
30
    protected function getModelClassNameAndId($model, ?int $modelId = null): array
31
    {
32
        if ($this->isModel($model)) {
33
            return [
34
                get_class($model),
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type string; however, parameter $object of get_class() does only seem to accept object, 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

34
                get_class(/** @scrutinizer ignore-type */ $model),
Loading history...
35
                $modelId ?? $model->getKey(),
36
            ];
37
        }
38
39
        return [
40
            $model,
41
            $modelId,
42
        ];
43
    }
44
}
45