Completed
Push — master ( 4ac004...470d43 )
by Gabriel
06:41
created

HasPrimaryKeyTrait::getPrimaryKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 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 3
    public function checkParamPrimaryKey($params)
24
    {
25 3
        if (isset($params['primaryKey'])) {
26
            $this->setPrimaryKey($params['primaryKey']);
27
            unset($params['primaryKey']);
28
        }
29 3
    }
30
31
    /**
32
     * @return string
33
     */
34 5
    public function getPrimaryKey()
35
    {
36 5
        if ($this->primaryKey == null) {
37 5
            $this->initPrimaryKey();
38
        }
39
40 5
        return $this->primaryKey;
41
    }
42
43
    /**
44
     * @param $name
45
     */
46 5
    public function setPrimaryKey($name)
47
    {
48 5
        $this->primaryKey = $name;
49 5
    }
50
51 5
    protected function initPrimaryKey()
52
    {
53 5
        $this->setPrimaryKey($this->generatePrimaryKey());
54 5
    }
55
56
    /**
57
     * @return string
58
     */
59 5
    protected function generatePrimaryKey()
60
    {
61 5
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
    }
63
}
64