Completed
Push — master ( dd341e...ba6dcb )
by Gabriel
07:12
created

BelongsTo   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 15.58 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 29.17%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 12
loc 77
ccs 7
cts 24
cp 0.2917
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateFK() 0 4 1
A initResults() 0 6 1
A getResultsFromCollectionDictionary() 0 9 2
A buildDictionary() 12 12 3
A getDictionaryKey() 0 6 1
A populateQuerySpecific() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nip\Records\Relations;
4
5
use Nip\Database\Query\AbstractQuery;
6
use Nip\Records\AbstractModels\Record;
7
use Nip\Records\Collections\Collection as RecordCollection;
8
9
/**
10
 * Class BelongsTo
11
 * @package Nip\Records\Relations
12
 */
13
class BelongsTo extends Relation
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $type = 'belongsTo';
20
21
    /** @noinspection PhpMissingParentCallCommonInspection
22
     * @return string
23
     */
24 2
    public function generateFK()
25
    {
26 2
        return $this->getWith()->getPrimaryFK();
27
    }
28
29 2
    public function initResults()
30
    {
31 2
        $manager = $this->getWith();
32 2
        $foreignKey = $this->getItem()->{$this->getFK()};
33 2
        $this->setResults($manager->findOne($foreignKey));
34 2
    }
35
36
    /**
37
     * @param $dictionary
38
     * @param $collection
39
     * @param $record
40
     * @return mixed
41
     */
42
    public function getResultsFromCollectionDictionary($dictionary, $collection, $record)
43
    {
44
        $primaryKey = $record->{$this->getFK()};
45
        if (isset($dictionary[$primaryKey])) {
46
            return $dictionary[$primaryKey];
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * Build model dictionary keyed by the relation's foreign key.
54
     *
55
     * @param RecordCollection $collection
56
     * @return array
57
     */
58 View Code Duplication
    protected function buildDictionary(RecordCollection $collection)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if ($collection->isEmpty()) {
61
            return [];
62
        }
63
        $dictionary = [];
64
        foreach ($collection as $record) {
65
            $dictionary[$this->getDictionaryKey($record)] = $record;
66
        }
67
68
        return $dictionary;
69
    }
70
71
    /**
72
     * @param Record $record
73
     * @return array
74
     * @throws \Exception
75
     */
76
    protected function getDictionaryKey(Record $record)
77
    {
78
        $withPK = $this->getWithPK();
79
80
        return $record->{$withPK};
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function populateQuerySpecific(AbstractQuery $query)
87
    {
88
    }
89
}
90