HasPrimaryKeyTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 49
ccs 15
cts 17
cp 0.8824
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setPrimaryKey() 0 3 1
A getPrimaryKey() 0 7 2
A initPrimaryKey() 0 3 1
A checkParamPrimaryKey() 0 5 2
A generatePrimaryKey() 0 3 2
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Nip\Records\AbstractModels\RecordManager;
6
7
/**
8
 * Trait HasForeignKeyTrait
9
 * @package Nip\Records\Relations\Traits
10
 *
11
 * @method RecordManager getManager()
12
 */
13
trait HasPrimaryKeyTrait
14
{
15
    /**
16
     * @var null|string
17
     */
18
    protected $primaryKey = null;
19
20
    /**
21
     * @param $params
22
     */
23 6
    public function checkParamPrimaryKey($params)
24
    {
25 6
        if (isset($params['primaryKey'])) {
26
            $this->setPrimaryKey($params['primaryKey']);
27
            unset($params['primaryKey']);
28
        }
29 6
    }
30
31
    /**
32
     * @return string
33
     */
34 6
    public function getPrimaryKey()
35
    {
36 6
        if ($this->primaryKey == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->primaryKey of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
37 6
            $this->initPrimaryKey();
38
        }
39
40 6
        return $this->primaryKey;
41
    }
42
43
    /**
44
     * @param $name
45
     */
46 6
    public function setPrimaryKey($name)
47
    {
48 6
        $this->primaryKey = $name;
49 6
    }
50
51 6
    protected function initPrimaryKey()
52
    {
53 6
        $this->setPrimaryKey($this->generatePrimaryKey());
54 6
    }
55
56
    /**
57
     * @return string
58
     */
59 6
    protected function generatePrimaryKey()
60
    {
61 6
        return $this->hasManager() ? $this->getManager()->getPrimaryKey() : 'id';
0 ignored issues
show
Bug introduced by
It seems like hasManager() 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

61
        return $this->/** @scrutinizer ignore-call */ hasManager() ? $this->getManager()->getPrimaryKey() : 'id';
Loading history...
62
    }
63
}
64