GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( c5ade0...e039e4 )
by Gabriel
05:51
created

HasRelationsRecordTrait::initRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Records\Traits\Relations;
4
5
use Nip\Records\Relations\HasMany;
6
use Nip\Records\Relations\Relation;
7
use Nip\Records\Traits\AbstractTrait\RecordTrait;
8
9
/**
10
 * Trait HasRelationsRecordTrait
11
 * @package Nip\Records\Traits\Relations
12
 *
13
 * @method HasRelationsRecordsTrait getManager
14
 */
15
trait HasRelationsRecordTrait
16
{
17
    use RecordTrait;
18
19
    /**
20
     * The loaded relationships for the model.
21
     * @var array
22
     */
23
    protected $relations = [];
24
25
    public function saveRelations()
26
    {
27
        $relations = $this->getRelations();
28
        foreach ($relations as $relation) {
29
            /** @var Relation $relation */
30
            $relation->save();
31
        }
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getRelations()
38
    {
39
        return $this->relations;
40
    }
41
42
    /**
43
     * @param $name
44
     * @param $arguments
45
     * @return \Nip\Records\AbstractModels\Record|\Nip\Records\Collections\Collection
46
     */
47
    protected function isCallRelationOperation($name, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        if (substr($name, 0, 3) == "get") {
50
            $relation = $this->getRelation(substr($name, 3));
51
            if ($relation) {
52
                return $relation->getResults();
53
            }
54
        }
55
        return null;
56
    }
57
58
    /**
59
     * @param $relationName
60
     * @return Relation|HasMany|null
61
     */
62
    public function getRelation($relationName)
63
    {
64
        if (!$this->hasRelation($relationName)) {
65
            $this->initRelation($relationName);
66
        }
67
68
        return $this->relations[$relationName];
69
    }
70
71
    /**
72
     * @param $key
73
     * @return bool
74
     */
75
    public function hasRelation($key)
76
    {
77
        return array_key_exists($key, $this->relations);
78
    }
79
80
    /**
81
     * @param $relationName
82
     */
83
    public function initRelation($relationName)
84
    {
85
        if (!$this->getManager()->hasRelation($relationName)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasRelation does not exist on object<Nip\Records\AbstractModels\RecordManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
            return;
87
        }
88
        $this->relations[$relationName] = $this->newRelation($relationName);
89
    }
90
91
    /**
92
     * @param string $relationName
93
     * @return Relation|null
94
     */
95
    public function newRelation($relationName)
96
    {
97
        $relation = clone $this->getManager()->getRelation($relationName);
0 ignored issues
show
Documentation Bug introduced by
The method getRelation does not exist on object<Nip\Records\AbstractModels\RecordManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
        $relation->setItem($this);
99
100
        return $relation;
101
    }
102
}