Test Failed
Branch master (1fbe3c)
by Aleksandr
02:14
created

PivotColumn::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 $_name;
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
    public function __toString()
32
    {
33
        return $this->getPrefix();
34
    }
35
36
    public function setPrefix($value)
37
    {
38
        $this->_prefix = $value;
39
    }
40
41
    public function getPrefix()
42
    {
43
        return $this->_prefix;
44
    }
45
46
    /**
47
     * @param $migrate
48
     * @return $this
49
     */
50
    public function setMigrate($migrate)
51
    {
52
        $this->migrate = $migrate;
53
        return $this;
54
    }
55
56
    /**
57
     * @param $name
58
     * @return $this
59
     */
60
    public function setName($name)
61
    {
62
        $this->_name = $name;
63
        return $this;
64
    }
65
66
    /**
67
     * @param $name
68
     * @return $this
69
     */
70
    public function tableName($name)
71
    {
72
        $this->_tableName = $name;
73
        return $this;
74
    }
75
76
    /**
77
     * @param array $columns
78
     * @return $this
79
     */
80
    public function columns($columns)
81
    {
82
        $this->_columns = $columns;
83
        return $this;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getColumns()
90
    {
91
        return $this->_columns;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function getTableName()
98
    {
99
        return $this->_tableName;
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    public function getName()
106
    {
107
        $name = $this->_tableName ?: implode('_', [$this->getPrefix(), $this->_sourceTable, $this->_name]);
108
        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

108
        return '{{%' . /** @scrutinizer ignore-type */ SchemaHelper::expandTablePrefix($name, '') . '}}';
Loading history...
109
    }
110
111
    public function remove()
112
    {
113
        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
            $this->migrate->downNewColumns([$this->getName() => $this->_columns]);
115
        }
116
        $this->migrate->dropTable($this->getName());
117
    }
118
119
    public function apply()
120
    {
121
        /**
122
         * @var ForeignKeyColumn $type
123
         */
124
        $columns = [
125
            $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
            $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
        $columnsInt = array_combine(array_keys($columns), [$this->migrate->integer(), $this->migrate->integer()]);
129
        if ($this->migrate->db->driverName === 'mysql') {
130
            $this->migrate->createTable($this->getName(), $columnsInt);
131
            $this->migrate->addPrimaryKey(null, $this->getName(), array_keys($columns));
132
            foreach ($columns as $name => $type) {
133
                $type->sourceTable($this->getName())->sourceColumn($name);
134
                $type->apply();
135
            }
136
        } else {
137
            $this->migrate->createTable($this->getName(), $columns);
138
            $this->migrate->addPrimaryKey(null, $this->getName(), array_keys($columns));
139
        }
140
        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...
141
            $this->migrate->upNewColumns([$this->getName() => $this->_columns]);
142
        }
143
    }
144
145
    /**
146
     * @return null
147
     */
148
    public function getRefTable()
149
    {
150
        return $this->_refTable;
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function getRefColumn()
157
    {
158
        if (!$this->_refColumn) {
159
            $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...
160
        } else {
161
            $name = $this->_refColumn;
162
        }
163
        $refColumn = SchemaHelper::expandTablePrefix($name, '');
164
        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

164
        if (strtolower(/** @scrutinizer ignore-type */ $refColumn) === strtolower($this->getSourceColumn())) {
Loading history...
165
            $refColumn = Inflector::singularize($this->_name) . '_id';
166
        }
167
        return $refColumn;
168
    }
169
170
    /**
171
     * @return null
172
     */
173
    public function getSourceTable()
174
    {
175
        return $this->_sourceTable;
176
    }
177
178
    /**
179
     * @return mixed
180
     */
181
    public function getSourceColumn()
182
    {
183
        if (!$this->_sourceColumn) {
184
            $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...
185
        } else {
186
            $name = $this->_sourceColumn;
187
        }
188
        return SchemaHelper::expandTablePrefix($name, '');
189
    }
190
191
    /**
192
     * @param $name
193
     * @return $this
194
     */
195
    public function refTable($name)
196
    {
197
        $this->_refTable = $name;
198
        return $this;
199
    }
200
201
    /**
202
     * @param $name
203
     * @return $this
204
     */
205
    public function sourceColumn($name)
206
    {
207
        $this->_sourceColumn = $name;
208
        return $this;
209
    }
210
211
    /**
212
     * @param $name
213
     * @return $this
214
     */
215
    public function sourceTable($name)
216
    {
217
        $this->_sourceTable = $name;
218
        return $this;
219
    }
220
221
    /**
222
     * @param $name
223
     * @return $this
224
     */
225
    public function refColumn($name)
226
    {
227
        $this->_refColumn = $name;
228
        return $this;
229
    }
230
}