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

HasPrimaryKeyTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 51
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkParamPrimaryKey() 0 7 2
A getPrimaryKey() 0 8 2
A setPrimaryKey() 0 4 1
A initPrimaryKey() 0 4 1
A generatePrimaryKey() 0 4 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