Passed
Push — master ( 3db497...381c1f )
by Aleksandr
02:18
created

PivotColumn::getSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace carono\yii2migrate;
4
5
use carono\yii2migrate\helpers\SchemaHelper;
6
use yii\helpers\Inflector;
7
8
/**
9
 * Class PivotColumn
10
 *
11
 * @package carono\yii2installer
12
 */
13
class PivotColumn
14
{
15
    protected $_refTable;
16
    protected $_refColumn;
17
    protected $_sourceTable;
18
    protected $_sourceColumn;
19
    protected $_suffix;
20
    protected $_tableName;
21
    protected $_columns = [];
22
    protected $_prefix = 'pv';
23
    /**
24
     * @var Migration
25
     */
26
    public $migrate;
27
28
    /**
29
     * @return string
30
     */
31 2
    public function __toString()
32
    {
33 2
        return $this->getPrefix();
34
    }
35
36 1
    public function setPrefix($value)
37
    {
38 1
        $this->_prefix = $value;
39 1
    }
40
41 10
    public function getPrefix()
42
    {
43 10
        return $this->_prefix;
44
    }
45
46
    /**
47
     * @param $migrate
48
     * @return $this
49
     */
50 11
    public function setMigrate($migrate)
51
    {
52 11
        $this->migrate = $migrate;
53 11
        return $this;
54
    }
55
56
    /**
57
     * @param $value
58
     * @return $this
59
     */
60 11
    public function setSuffix($value)
61
    {
62 11
        $this->_suffix = $value;
63 11
        return $this;
64
    }
65
66
    /**
67
     * @param $name
68
     * @return $this
69
     */
70 5
    public function tableName($name)
71
    {
72 5
        $this->_tableName = $name;
73 5
        return $this;
74
    }
75
76
    /**
77
     * @param array $columns
78
     * @return $this
79
     */
80 6
    public function columns($columns)
81
    {
82 6
        $this->_columns = $columns;
83 6
        return $this;
84
    }
85
86
    /**
87
     * @return array
88
     */
89 10
    public function getColumns()
90
    {
91 10
        return $this->_columns;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97 12
    public function getTableName()
98
    {
99 12
        $name = $this->_tableName ?: implode('_', [$this->getPrefix(), $this->_sourceTable, $this->_suffix]);
100 12
        return '{{%' . SchemaHelper::expandTablePrefix($name, '') . '}}';
0 ignored issues
show
Bug introduced by
Are you sure carono\yii2migrate\helpe...dTablePrefix($name, '') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

100
        return '{{%' . /** @scrutinizer ignore-type */ SchemaHelper::expandTablePrefix($name, '') . '}}';
Loading history...
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106 1
    public function getSuffix()
107
    {
108 1
        return $this->_suffix;
109
    }
110
111 8
    public function remove()
112
    {
113 8
        if ($this->_columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
114 5
            $this->migrate->downNewColumns([$this->getTableName() => $this->_columns]);
115
        }
116 8
        $this->migrate->dropTable($this->getTableName());
117 8
    }
118
119 9
    public function apply()
120
    {
121
        /**
122
         * @var ForeignKeyColumn $type
123
         */
124
        $columns = [
125 9
            $this->getSourceColumn() => $this->migrate->foreignKey($this->getSourceTable()),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSourceTable() targeting carono\yii2migrate\PivotColumn::getSourceTable() 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...
126 9
            $this->getRefColumn() => $this->migrate->foreignKey($this->getRefTable()),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getRefTable() targeting carono\yii2migrate\PivotColumn::getRefTable() 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...
127
        ];
128 9
        $columnsInt = array_combine(array_keys($columns), [$this->migrate->integer(), $this->migrate->integer()]);
129
130 9
        $this->migrate->createTable($this->getTableName(), $columnsInt);
131 9
        $this->migrate->addPrimaryKey(null, $this->getTableName(), array_keys($columns));
132
133 9
        foreach ($columns as $name => $type) {
134 9
            $type->sourceTable($this->getTableName())->sourceColumn($name);
135 9
            $type->apply();
136
        }
137 9
        if ($advancedColumns = $this->getColumns()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $advancedColumns is dead and can be removed.
Loading history...
138 5
            $this->migrate->upNewColumns([$this->getTableName() => $this->getColumns()]);
139
        }
140 9
    }
141
142
    /**
143
     * @return null
144
     */
145 11
    public function getRefTable()
146
    {
147 11
        return $this->_refTable;
148
    }
149
150
    /**
151
     * @return mixed
152
     */
153 11
    public function getRefColumn()
154
    {
155 11
        if (!$this->_refColumn) {
156 10
            $name = implode('_', [$this->getRefTable(), 'id']);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getRefTable() targeting carono\yii2migrate\PivotColumn::getRefTable() 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...
157
        } else {
158 2
            $name = $this->_refColumn;
159
        }
160 11
        $refColumn = SchemaHelper::expandTablePrefix($name, '');
161 11
        if (strtolower($refColumn) === strtolower($this->getSourceColumn())) {
0 ignored issues
show
Bug introduced by
It seems like $refColumn can also be of type string[]; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

161
        if (strtolower(/** @scrutinizer ignore-type */ $refColumn) === strtolower($this->getSourceColumn())) {
Loading history...
162 1
            $refColumn = Inflector::singularize($this->_suffix) . '_id';
163
        }
164 11
        return $refColumn;
165
    }
166
167
    /**
168
     * @return null
169
     */
170 13
    public function getSourceTable()
171
    {
172 13
        return $this->_sourceTable;
173
    }
174
175
    /**
176
     * @return mixed
177
     */
178 13
    public function getSourceColumn()
179
    {
180 13
        if (!$this->_sourceColumn) {
181 12
            $name = implode('_', [$this->getSourceTable(), 'id']);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSourceTable() targeting carono\yii2migrate\PivotColumn::getSourceTable() 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...
182
        } else {
183 2
            $name = $this->_sourceColumn;
184
        }
185 13
        return SchemaHelper::expandTablePrefix($name, '');
186
    }
187
188
    /**
189
     * @param $name
190
     * @return $this
191
     */
192 13
    public function refTable($name)
193
    {
194 13
        $this->_refTable = $name;
195 13
        return $this;
196
    }
197
198
    /**
199
     * @param $name
200
     * @return $this
201
     */
202 2
    public function sourceColumn($name)
203
    {
204 2
        $this->_sourceColumn = $name;
205 2
        return $this;
206
    }
207
208
    /**
209
     * @param $name
210
     * @return $this
211
     */
212 12
    public function sourceTable($name)
213
    {
214 12
        $this->_sourceTable = $name;
215 12
        return $this;
216
    }
217
218
    /**
219
     * @param $name
220
     * @return $this
221
     */
222 11
    public function refColumn($name)
223
    {
224 11
        $this->_refColumn = $name;
225 11
        return $this;
226
    }
227
}