Passed
Push — master ( cdda17...81dac7 )
by Gabriel
02:11
created

BelongsTo::initResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 2.0078
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
     * @var string
17
     */
18
    protected $type = 'belongsTo';
19
20
    /** @noinspection PhpMissingParentCallCommonInspection
21
     * @return string
22
     */
23 2
    public function generateFK()
24
    {
25 2
        return $this->getWith()->getPrimaryFK();
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31 2
    public function initResults()
32
    {
33 2
        $withManager = $this->getWith();
34 2
        $foreignKey = $this->getItem()->{$this->getFK()};
35 2
        $results = $withManager->findByField($this->getWithPK(), $foreignKey);
36 2
        if (count($results) > 0) {
37 2
            $this->setResults($results->rewind());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $results->rewind() targeting Nip\Collections\AbstractCollection::rewind() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38 2
            return;
39
        }
40
41
        return $this->setResults(false);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setResults(false) targeting Nip\Records\Relations\Relation::setResults() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
43
    }
44
45
    /**
46
     * @param $dictionary
47
     * @param $collection
48
     * @param $record
49
     * @return mixed
50
     */
51
    public function getResultsFromCollectionDictionary($dictionary, $collection, $record)
52
    {
53
        $primaryKey = $record->{$this->getFK()};
54
        if (isset($dictionary[$primaryKey])) {
55
            return $dictionary[$primaryKey];
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Build model dictionary keyed by the relation's foreign key.
63
     *
64
     * @param RecordCollection $collection
65
     * @return array
66
     */
67
    protected function buildDictionary(RecordCollection $collection)
68
    {
69
        if ($collection->isEmpty()) {
70
            return [];
71
        }
72
        $dictionary = [];
73
        foreach ($collection as $record) {
74
            $dictionary[$this->getDictionaryKey($record)] = $record;
75
        }
76
77
        return $dictionary;
78
    }
79
80
    /**
81
     * @param Record $record
82
     * @return array
83
     * @throws \Exception
84
     */
85
    protected function getDictionaryKey(Record $record)
86
    {
87
        $withPK = $this->getWithPK();
88
89
        return $record->{$withPK};
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function populateQuerySpecific(AbstractQuery $query)
96
    {
97
    }
98
}
99