HasOne::getDictionaryKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Nip\Records\Relations;
4
5
use Nip\Database\Query\AbstractQuery;
6
use Nip\Records\AbstractModels\Record as Record;
7
use Nip\Records\Collections\Collection as RecordCollection;
8
9
/**
10
 * Class HasOneOrMany
11
 * @package Nip\Records\Relations
12
 */
13
class HasOne extends Relation
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $type = 'hasOne';
19
20
    /** @noinspection PhpMissingParentCallCommonInspection
21
     * @return string
22
     */
23 1
    public function generateFK()
24
    {
25 1
        return $this->getManager()->getPrimaryFK();
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31 1
    public function initResults()
32
    {
33 1
        $withManager = $this->getWith();
34 1
        $foreignKey = $this->getItem()->{$this->getPrimaryKey()};
35 1
        $results = $withManager->findByField($this->getFK(), $foreignKey);
36 1
        if (count($results) > 0) {
37 1
            $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 1
            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
     * @param $dictionary
46
     * @param $collection
47
     * @param $record
48
     * @return mixed
49
     */
50
    public function getResultsFromCollectionDictionary($dictionary, $collection, $record)
51
    {
52
        $primaryKey = $record->{$this->getPrimaryKey()};
53
        if (isset($dictionary[$primaryKey])) {
54
            return $dictionary[$primaryKey];
55
        }
56
57
        return null;
58
    }
59
60
    /**
61
     * Build model dictionary keyed by the relation's foreign key.
62
     *
63
     * @param RecordCollection $collection
64
     * @return array
65
     */
66
    protected function buildDictionary(RecordCollection $collection)
67
    {
68
        if ($collection->isEmpty()) {
69
            return [];
70
        }
71
        $dictionary = [];
72
        foreach ($collection as $record) {
73
            $dictionary[$this->getDictionaryKey($record)] = $record;
74
        }
75
76
        return $dictionary;
77
    }
78
79
    /**
80
     * @param Record $record
81
     * @return array
82
     * @throws \Exception
83
     */
84
    protected function getDictionaryKey(Record $record)
85
    {
86
        $withPK = $this->getPrimaryKey();
87
88
        return $record->{$withPK};
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function populateQuerySpecific(AbstractQuery $query)
95
    {
96
    }
97
}
98