Completed
Push — master ( 09bb07...3577e6 )
by Gabriel
04:08 queued 10s
created

RecordsTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 47
ccs 10
cts 18
cp 0.5556
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKey() 0 7 2
A initPrimaryKey() 0 3 1
A generatePrimaryKey() 0 12 4
A setPrimaryKey() 0 3 1
1
<?php
2
3
namespace Nip\Records\Traits\HasPrimaryKey;
4
5
/**
6
 * Trait RecordsTrait
7
 * @package Nip\Records\Traits\HasPrimaryKey
8
 */
9
trait RecordsTrait
10
{
11
    /**
12
     * @var null|string
13
     */
14
    protected $primaryKey = null;
15
16
    /**
17
     * @return string
18
     */
19 20
    public function getPrimaryKey()
20
    {
21 20
        if ($this->primaryKey === null) {
22 3
            $this->initPrimaryKey();
23
        }
24
25 20
        return $this->primaryKey;
26
    }
27
28
    /**
29
     * @param null|string $primaryKey
30
     */
31 18
    public function setPrimaryKey($primaryKey)
32
    {
33 18
        $this->primaryKey = $primaryKey;
34 18
    }
35
36 3
    protected function initPrimaryKey()
37
    {
38 3
        $this->setPrimaryKey($this->generatePrimaryKey());
39 3
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function generatePrimaryKey()
45
    {
46
        $structure = $this->getTableStructure();
0 ignored issues
show
Bug introduced by
It seems like getTableStructure() 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

46
        /** @scrutinizer ignore-call */ 
47
        $structure = $this->getTableStructure();
Loading history...
47
        $primaryKey = false;
48
        if (is_array($structure) && isset($structure['indexes']['PRIMARY']['fields'])) {
49
            $primaryKey = $structure['indexes']['PRIMARY']['fields'];
50
            if (count($primaryKey) == 1) {
51
                $primaryKey = reset($primaryKey);
52
            }
53
        }
54
55
        return $primaryKey;
56
    }
57
}
58