Issues (276)

src/Relations/BelongsTo.php (3 issues)

Labels
Severity
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
        if (empty($foreignKey)) {
36
            return $this->setResults(false);
0 ignored issues
show
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...
37
        }
38 2
        $results = $withManager->findByField($this->getWithPK(), $foreignKey);
39 2
        if (count($results) < 1) {
40
            return $this->setResults(false);
0 ignored issues
show
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...
41
        }
42
43 2
        $this->setResults($results->rewind());
0 ignored issues
show
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...
44 2
        return;
45
    }
46
47
    /**
48
     * @param $dictionary
49
     * @param $collection
50
     * @param $record
51
     * @return mixed
52
     */
53
    public function getResultsFromCollectionDictionary($dictionary, $collection, $record)
54
    {
55
        $primaryKey = $record->{$this->getFK()};
56
        if (isset($dictionary[$primaryKey])) {
57
            return $dictionary[$primaryKey];
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * Build model dictionary keyed by the relation's foreign key.
65
     *
66
     * @param RecordCollection $collection
67
     * @return array
68
     */
69
    protected function buildDictionary(RecordCollection $collection)
70
    {
71
        if ($collection->isEmpty()) {
72
            return [];
73
        }
74
        $dictionary = [];
75
        foreach ($collection as $record) {
76
            $dictionary[$this->getDictionaryKey($record)] = $record;
77
        }
78
79
        return $dictionary;
80
    }
81
82
    /**
83
     * @param Record $record
84
     * @return array
85
     * @throws \Exception
86
     */
87
    protected function getDictionaryKey(Record $record)
88
    {
89
        $withPK = $this->getWithPK();
90
91
        return $record->{$withPK};
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function populateQuerySpecific(AbstractQuery $query)
98
    {
99
    }
100
}
101