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

HasPivotPrimaryKey   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 52
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkParamPivotPrimaryKey() 0 7 2
A getPivotPrimaryKey() 0 8 2
A setPivotPrimaryKey() 0 4 1
A initPivotPrimaryKey() 0 4 1
A generatePivotPrimaryKey() 0 4 1
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Nip\Records\AbstractModels\RecordManager;
6
7
/**
8
 * Trait HasPivotPrimaryKey
9
 * @package Nip\Records\Relations\Traits
10
 *
11
 * @method RecordManager getWith
12
 */
13
trait HasPivotPrimaryKey
14
{
15
    /**
16
     * @var null|string
17
     */
18
    protected $pivotPrimaryKey = null;
19
20
    /**
21
     * @param $params
22
     */
23
    public function checkParamPivotPrimaryKey($params)
24
    {
25
        if (isset($params['pivotPrimaryKey'])) {
26
            $this->setPivotPrimaryKey($params['pivotPrimaryKey']);
27
            unset($params['pivotPrimaryKey']);
28
        }
29
    }
30
31
32
    /**
33
     * @return string
34
     */
35 1
    public function getPivotPrimaryKey()
36
    {
37 1
        if ($this->pivotPrimaryKey == null) {
38 1
            $this->initPivotPrimaryKey();
39
        }
40
41 1
        return $this->pivotPrimaryKey;
42
    }
43
44
    /**
45
     * @param $name
46
     */
47 1
    public function setPivotPrimaryKey($name)
48
    {
49 1
        $this->pivotPrimaryKey = $name;
50 1
    }
51
52 1
    protected function initPivotPrimaryKey()
53
    {
54 1
        $this->setPivotPrimaryKey($this->generatePivotPrimaryKey());
55 1
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    protected function generatePivotPrimaryKey()
61
    {
62 1
        return $this->getWith()->getPrimaryKey();
0 ignored issues
show
Bug introduced by
It seems like getWith() 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...
63
    }
64
}
65