Passed
Push — master ( 4a73c0...85b254 )
by Stephen
12:07 queued 09:21
created

HasCompositePrimaryKey   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyForSaveQuery() 0 11 3
A getIncrementing() 0 3 1
A setKeysForSaveQuery() 0 12 3
1
<?php
2
3
namespace Sfneal\Models\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait HasCompositePrimaryKey
8
{
9
    /**
10
     * Get the value indicating whether the IDs are incrementing.
11
     *
12
     * @return bool
13
     */
14
    public function getIncrementing(): bool
15
    {
16
        return false;
17
    }
18
19
    /**
20
     * Set the keys for a save update query.
21
     *
22
     * @param  Builder  $query
23
     * @return Builder
24
     */
25
    protected function setKeysForSaveQuery($query): Builder
26
    {
27
        $keys = $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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

27
        /** @scrutinizer ignore-call */ 
28
        $keys = $this->getKeyName();
Loading history...
28
        if (! is_array($keys)) {
29
            return parent::setKeysForSaveQuery($query);
30
        }
31
32
        foreach ($keys as $keyName) {
33
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
34
        }
35
36
        return $query;
37
    }
38
39
    /**
40
     * Get the primary key value for a save query.
41
     *
42
     * @param  string|null  $keyName
43
     * @return mixed
44
     */
45
    protected function getKeyForSaveQuery(string $keyName = null): mixed
46
    {
47
        if (is_null($keyName)) {
48
            $keyName = $this->getKeyName();
49
        }
50
51
        if (isset($this->original[$keyName])) {
52
            return $this->original[$keyName];
53
        }
54
55
        return $this->getAttribute($keyName);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

55
        return $this->/** @scrutinizer ignore-call */ getAttribute($keyName);
Loading history...
56
    }
57
}
58