HasRelationsRecordTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 11.43%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 27
c 3
b 2
f 0
dl 0
loc 111
ccs 4
cts 35
cp 0.1143
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasRelation() 0 3 1
A newRelation() 0 6 1
A getCloneWithRelations() 0 7 1
A cloneRelations() 0 3 1
A initRelation() 0 6 2
A getRelations() 0 3 1
A saveRelations() 0 6 2
A isCallRelationOperation() 0 12 4
A getRelation() 0 7 2
1
<?php
2
3
namespace Nip\Records\Traits\Relations;
4
5
use Nip\Records\AbstractModels\Record;
6
use Nip\Records\AbstractModels\RecordManager;
7
use Nip\Records\Relations\HasMany;
8
use Nip\Records\Relations\Relation;
9
use Nip\Records\Traits\AbstractTrait\RecordTrait;
10
11
/**
12
 * Trait HasRelationsRecordTrait
13
 * @package Nip\Records\Traits\Relations
14
 *
15
 * @method HasRelationsRecordsTrait|RecordManager getManager
16
 */
17
trait HasRelationsRecordTrait
18
{
19
    use RecordTrait;
20
21
    /**
22
     * The loaded relationships for the model.
23
     * @var array
24
     */
25
    protected $relations = [];
26
27
    public function saveRelations()
28
    {
29
        $relations = $this->getRelations();
30
        foreach ($relations as $relation) {
31
            /** @var Relation $relation */
32
            $relation->save();
33
        }
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getRelations()
40
    {
41
        return $this->relations;
42
    }
43
44
    /**
45
     * @param string $name
46
     * @param $arguments
47
     * @return bool|\Nip\Records\AbstractModels\Record|\Nip\Records\Collections\Collection
48
     */
49
    protected function isCallRelationOperation($name, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    protected function isCallRelationOperation($name, /** @scrutinizer ignore-unused */ $arguments = [])

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        if (!$this->getManager()->/** @scrutinizer ignore-call */ hasRelation($relationName)) {
Loading history...
91
            return;
92
        }
93
        $this->relations[$relationName] = $this->newRelation($relationName);
94
    }
95
96
    /**
97
     * @param string $relationName
98
     * @return Relation|null
99
     */
100 1
    public function newRelation($relationName)
101
    {
102 1
        $relation = clone $this->getManager()->getRelation($relationName);
0 ignored issues
show
Bug introduced by
The method getRelation() does not exist on Nip\Records\AbstractModels\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        $relation = clone $this->getManager()->/** @scrutinizer ignore-call */ getRelation($relationName);
Loading history...
103 1
        $relation->setItem($this);
0 ignored issues
show
Bug introduced by
The method setItem() does not exist on Nip\Records\AbstractModels\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $relation->/** @scrutinizer ignore-call */ 
104
                   setItem($this);
Loading history...
Bug introduced by
The method setItem() does not exist on Nip\Records\Collections\Collection. Did you maybe mean setItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $relation->/** @scrutinizer ignore-call */ 
104
                   setItem($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like setItem() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $relation->/** @scrutinizer ignore-call */ 
104
                   setItem($this);
Loading history...
104
105 1
        return $relation;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $relation also could return the type Nip\Records\AbstractMode...asRelationsRecordsTrait which is incompatible with the documented return type Nip\Records\Relations\Relation|null.
Loading history...
106
    }
107
108
    /**
109
     * @return Record|self
110
     */
111
    public function getCloneWithRelations()
112
    {
113
        /** @var self $item */
114
        $item = $this->getClone();
0 ignored issues
show
Bug introduced by
The method getClone() does not exist on Nip\Records\Traits\Relat...HasRelationsRecordTrait. Did you maybe mean getCloneWithRelations()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        /** @scrutinizer ignore-call */ 
115
        $item = $this->getClone();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        $item->cloneRelations($this);
116
117
        return $item;
118
    }
119
120
    /**
121
     * Clone the relations records from a sibling
122
     * @param self $from
123
     * @return \Nip\Records\Traits\Relations\HasRelationsRecordTrait
124
     */
125
    public function cloneRelations($from)
126
    {
127
        return $this->getManager()->cloneRelations($from, $this);
0 ignored issues
show
Bug introduced by
The method cloneRelations() does not exist on Nip\Records\AbstractModels\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        return $this->getManager()->/** @scrutinizer ignore-call */ cloneRelations($from, $this);
Loading history...
128
    }
129
}
130