HasAttributes::getRawAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Support\Models;
4
5
use DomainException;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Illuminate\Support\Arr;
10
use InvalidArgumentException;
11
12
/**
13
 * @property int $id
14
 * @property \Illuminate\Support\Carbon $created_at
15
 * @property \Illuminate\Support\Carbon $updated_at
16
 */
17
trait HasAttributes
18
{
19
    /**
20
     * Determine if the model relation value has been modified.
21
     *
22
     * @param  array|string|null  $relations
23
     * @return bool
24
     *
25
     * @throws \InvalidArgumentException
26
     */
27
    public function isRelationValueDirty($relations = null): bool
28
    {
29
        $attributes = array_map(function ($name) {
30
            $relation = $this->$name();
31
32
            if (!$relation instanceof Relation || !method_exists($relation, 'getForeignKeyName')) {
33
                throw new InvalidArgumentException(sprintf(
34
                    'The given relation name %s on model class %s must return a relationship instance and has getForeignKeyName() method.',
35
                    $name, static::class
36
                ));
37
            }
38
39
            return $relation->getForeignKeyName();
40
        }, is_array($relations) ? $relations : func_get_args());
41
42
        return $this->isDirty($attributes);
0 ignored issues
show
Bug introduced by
It seems like isDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
        return $this->/** @scrutinizer ignore-call */ isDirty($attributes);
Loading history...
43
    }
44
45
    /**
46
     * Determine whether the given model collection is valid with the given class name.
47
     *
48
     * @param  \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>  $models
49
     * @param  string  $className
50
     * @param  bool  $throwExceptionWhenNotValid
51
     * @return bool
52
     *
53
     * @throws \DomainException
54
     */
55
    protected function isCollectionValid(Collection $models, string $className, bool $throwExceptionWhenNotValid = true): bool
56
    {
57
        return $models->every(function (Model $model) use ($className, $throwExceptionWhenNotValid) {
58
            $isValid = $model instanceof $className;
59
60
            if (!$isValid && $throwExceptionWhenNotValid) {
61
                throw new DomainException(sprintf(
62
                    'Invalid %s model type, %s given', $className, get_class($model)
63
                ));
64
            }
65
66
            return $isValid;
67
        });
68
    }
69
70
    /**
71
     * Return collection value from specified model relation.
72
     *
73
     * @param  string  $key
74
     * @param  string  $className
75
     * @return \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>
76
     *
77
     * @throws \DomainException
78
     */
79
    public function getCollectionValue(string $key, string $className): Collection
80
    {
81
        $this->isCollectionValid($models = $this->getRelationValue($key), $className);
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
        $this->isCollectionValid($models = $this->/** @scrutinizer ignore-call */ getRelationValue($key), $className);
Loading history...
82
83
        return $models;
84
    }
85
86
    /**
87
     * Get the model's raw values from $attributes.
88
     *
89
     * @param  string|null  $key
90
     * @param  mixed  $default
91
     * @return mixed|array
92
     */
93
    public function getRawAttribute($key = null, $default = null)
94
    {
95
        return Arr::get($this->getAttributes(), $key, $default);
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on App\Support\Models\HasAttributes. Did you maybe mean getRawAttribute()? ( Ignorable by Annotation )

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

95
        return Arr::get($this->/** @scrutinizer ignore-call */ getAttributes(), $key, $default);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
    }
97
}
98