Passed
Push — master ( 82c6b4...8a83ac )
by Gabriel
03:56 queued 12s
created

RecordTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCompositePrimaryKey() 0 8 3
A hasPrimaryKey() 0 7 2
A getPrimaryKey() 0 9 2
1
<?php
2
3
namespace Nip\Records\Traits\HasPrimaryKey;
4
5
/**
6
 * Trait RecordTrait
7
 * @package Nip\Records\Traits\HasPrimaryKey
8
 */
9
trait RecordTrait
10
{
11
    /**
12
     * @return mixed
13
     */
14 10
    public function getPrimaryKey()
15
    {
16 10
        $primaryKey = $this->getManager()->getPrimaryKey();
0 ignored issues
show
Bug introduced by
It seems like getManager() 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

16
        $primaryKey = $this->/** @scrutinizer ignore-call */ getManager()->getPrimaryKey();
Loading history...
17
18 10
        if (is_array($primaryKey)) {
19 2
            return $this->generateCompositePrimaryKey($primaryKey);
20
        }
21
22 8
        return $this->{$primaryKey};
23
    }
24
25
    /**
26
     * @param null $primaryKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $primaryKey is correct as it would always require null to be passed?
Loading history...
27
     * @return array
28
     */
29 2
    protected function generateCompositePrimaryKey($primaryKey = null)
30
    {
31 2
        $primaryKey = $primaryKey ? $primaryKey : $this->getManager()->getPrimaryKey();
0 ignored issues
show
introduced by
$primaryKey is of type null, thus it always evaluated to false.
Loading history...
32 2
        $return = [];
33 2
        foreach ($primaryKey as $key) {
34 2
            $return[$key] = $this->{$key};
35
        }
36 2
        return $return;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42 1
    public function hasPrimaryKey()
43
    {
44 1
        $key = $this->getPrimaryKey();
45 1
        if (is_array($key)) {
46 1
            $key = array_filter($key);
47
        }
48 1
        return !empty($key);
49
    }
50
}
51