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

RecordsTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryFK() 0 8 2
A initPrimaryFK() 0 4 1
A setForeignKey() 0 4 1
A generatePrimaryFK() 0 6 1
A setPrimaryFK() 0 4 1
1
<?php
2
3
namespace Nip\Records\Traits\HasForeignKey;
4
5
/**
6
 * Trait RecordsTrait
7
 * @package Nip\Records\Traits\HasForeignKey
8
 */
9
trait RecordsTrait
10
{
11
    /**
12
     * @var null|string
13
     */
14
    protected $foreignKey = null;
15
16
    /**
17
     * The name of the field used as a foreign key in other tables
18
     * @return string
19
     */
20 8
    public function getPrimaryFK()
21
    {
22 8
        if ($this->foreignKey == null) {
23 6
            $this->initPrimaryFK();
24
        }
25
26 8
        return $this->foreignKey;
27
    }
28
29 6
    public function initPrimaryFK()
30
    {
31 6
        $this->setForeignKey($this->generatePrimaryFK());
32 6
    }
33
34
    /**
35
     * @param string $foreignKey
36
     */
37 6
    public function setForeignKey($foreignKey)
38
    {
39 6
        $this->foreignKey = $foreignKey;
40 6
    }
41
42
    /**
43
     * @return string
44
     */
45 5
    public function generatePrimaryFK()
46
    {
47 5
        $singularize = inflector()->singularize($this->getController());
0 ignored issues
show
Bug introduced by
It seems like getController() 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...
48
49 5
        return $this->getPrimaryKey() . "_" . inflector()->underscore($singularize);
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() 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...
50
    }
51
52
    /**
53
     * @param $fk
54
     */
55 3
    public function setPrimaryFK($fk)
56
    {
57 3
        $this->foreignKey = $fk;
58 3
    }
59
}
60