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

MorphToMany   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 160
Duplicated Lines 6.88 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 11
loc 160
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePivotTable() 0 6 1
A getPivotManager() 0 4 2
A isInverse() 0 4 1
A setInverse() 0 4 1
A populateQuerySpecific() 11 11 2
A formatAttachData() 0 6 1
A getMorphKey() 0 4 1
A getMorphType() 0 7 2
A setMorphType() 0 4 1
A initMorphType() 0 4 1
A generateMorphType() 0 4 2
A queryDetachRecords() 0 5 1
A hydrateQueryWithPivotConstraints() 0 8 1
A getPivotFK() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nip\Records\Relations;
4
5
use Nip\Database\Query\AbstractQuery;
6
use Nip\Database\Query\Delete as DeleteQuery;
7
use Nip\Database\Query\Select as SelectQuery;
8
use Nip\Records\Relations\Traits\HasCollectionResults;
9
10
/**
11
 * Class HasOneOrMany
12
 * @package Nip\Records\Relations
13
 */
14
class MorphToMany extends HasAndBelongsToMany
15
{
16
    use HasCollectionResults;
17
18
    protected $type = 'morphToMany';
19
20
    /**
21
     * The type of the polymorphic relation.
22
     *
23
     * @var string
24
     */
25
    protected $morphType = null;
26
27
    /**
28
     * Indicates if we are connecting the inverse of the relation.
29
     *
30
     * This primarily affects the morphClass constraint.
31
     *
32
     * @var bool
33
     */
34
    protected $inverse = false;
35
36
    /** @noinspection PhpMissingParentCallCommonInspection
37
     * Builds the name of a has-and-belongs-to-many association table
38
     * @return string
39
     */
40
    public function generatePivotTable()
41
    {
42
        $pivotManager = $this->getPivotManager();
43
44
        return $pivotManager->getTable() . '_pivot';
45
    }
46
47
    /**
48
     * @return \Nip\Records\AbstractModels\RecordManager
49
     */
50
    protected function getPivotManager()
51
    {
52
        return $this->isInverse() ? $this->getManager() : $this->getWith();
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isInverse(): bool
59
    {
60
        return $this->inverse;
61
    }
62
63
    /**
64
     * @param bool $inverse
65
     */
66
    public function setInverse(bool $inverse)
67
    {
68
        $this->inverse = $inverse;
69
    }
70
71
    /**
72
     * @param AbstractQuery $query
73
     * @return AbstractQuery
74
     */
75 View Code Duplication
    public function populateQuerySpecific(AbstractQuery $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        if ($this->isInverse()) {
78
            return parent::populateQuerySpecific($query);
79
        }
80
        $pk1 = $this->getManager()->getPrimaryKey();
81
82
        $query->where("`{$this->getTable()}`.`pivotal_id` = ?", $this->getItem()->{$pk1});
83
84
        return $query;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    protected function formatAttachData($record)
91
    {
92
        $data = parent::formatAttachData($record);
93
        $data[$this->getMorphKey()] = $this->getMorphType();
94
        return $data;
95
    }
96
97
    /**
98
     * Get the foreign key "type" name.
99
     *
100
     * @return string
101
     */
102
    public function getMorphKey()
103
    {
104
        return 'pivotal_type';
105
    }
106
107
    /**
108
     * Get the foreign key "type" name.
109
     *
110
     * @return string
111
     */
112
    public function getMorphType()
113
    {
114
        if ($this->morphType == null) {
115
            $this->initMorphType();
116
        }
117
        return $this->morphType;
118
    }
119
120
    /**
121
     * @param string $morphType
122
     */
123
    public function setMorphType(string $morphType)
124
    {
125
        $this->morphType = $morphType;
126
    }
127
128
    protected function initMorphType()
129
    {
130
        $this->setMorphType($this->generateMorphType());
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    protected function generateMorphType()
137
    {
138
        return $this->isInverse() ? $this->getWith()->getTable() : $this->getManager()->getTable();
139
    }
140
141
    /**
142
     * @param DeleteQuery $query
143
     * @param $records
144
     */
145
    protected function queryDetachRecords($query, $records)
146
    {
147
        parent::queryDetachRecords($query, $records);
148
        $query->where("`{$this->getMorphKey()}` = ?", $this->getMorphType());
0 ignored issues
show
Documentation introduced by
$this->getMorphType() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
    }
150
151
    /**
152
     * @param SelectQuery $query
153
     */
154
    protected function hydrateQueryWithPivotConstraints($query)
155
    {
156
        parent::hydrateQueryWithPivotConstraints($query);
157
        $query->where(
158
            "`{$this->getTable()}`.`{$this->getMorphKey()}` = ?",
159
            $this->getMorphType()
0 ignored issues
show
Documentation introduced by
$this->getMorphType() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        );
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166
    protected function getPivotFK()
167
    {
168
        if ($this->isInverse()) {
169
            return 'pivotal_id';
170
        }
171
        return parent::getPivotFK();
172
    }
173
}
174