carono /
yii2-migrate
| 1 | <?php |
||
| 2 | |||
| 3 | namespace carono\yii2migrate; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class PivotColumn |
||
| 7 | * |
||
| 8 | * @package carono\yii2installer |
||
| 9 | */ |
||
| 10 | class PivotColumn |
||
| 11 | { |
||
| 12 | protected $_refTable = null; |
||
| 13 | protected $_refColumn = null; |
||
| 14 | protected $_sourceTable = null; |
||
| 15 | protected $_sourceColumn = null; |
||
| 16 | protected $_name = null; |
||
| 17 | protected $_tableName = null; |
||
| 18 | protected $_columns = []; |
||
| 19 | /** |
||
| 20 | * @var Migration |
||
| 21 | */ |
||
| 22 | public $migrate; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return string |
||
| 26 | */ |
||
| 27 | public function __toString() |
||
| 28 | { |
||
| 29 | return 'pv'; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param $migrate |
||
| 34 | * @return $this |
||
| 35 | */ |
||
| 36 | public function setMigrate($migrate) |
||
| 37 | { |
||
| 38 | $this->migrate = $migrate; |
||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $name |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | public function setName($name) |
||
| 47 | { |
||
| 48 | $this->_name = $name; |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param $name |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function tableName($name) |
||
| 57 | { |
||
| 58 | $this->_tableName = $name; |
||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $columns |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | public function columns($columns) |
||
| 67 | { |
||
| 68 | $this->_columns = $columns; |
||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function getName() |
||
| 76 | { |
||
| 77 | $name = $this->_tableName ? $this->_tableName : join('_', ["pv", $this->_sourceTable, $this->_name]); |
||
| 78 | return "{{%" . Migration::setTablePrefix($name, '') . "}}"; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function remove() |
||
| 82 | { |
||
| 83 | if ($this->_columns) { |
||
|
0 ignored issues
–
show
|
|||
| 84 | $this->migrate->downNewColumns([$this->getName() => $this->_columns]); |
||
| 85 | } |
||
| 86 | $this->migrate->dropTable($this->getName()); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function apply() |
||
| 90 | { |
||
| 91 | /** |
||
| 92 | * @var ForeignKeyColumn $type |
||
| 93 | */ |
||
| 94 | $columns = [ |
||
| 95 | $this->getSourceColumn() => $this->migrate->foreignKey($this->getSourceTable()), |
||
|
0 ignored issues
–
show
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 96 | $this->getRefColumn() => $this->migrate->foreignKey($this->getRefTable()), |
||
|
0 ignored issues
–
show
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 97 | ]; |
||
| 98 | $columnsInt = array_combine(array_keys($columns), [$this->migrate->integer(), $this->migrate->integer()]); |
||
| 99 | if ($this->migrate->db->driverName == "mysql") { |
||
| 100 | $this->migrate->createTable($this->getName(), $columnsInt); |
||
| 101 | $this->migrate->addPrimaryKey(null, $this->getName(), array_keys($columns)); |
||
| 102 | foreach ($columns as $name => $type) { |
||
| 103 | $type->sourceTable($this->getName())->sourceColumn($name); |
||
| 104 | $type->apply(); |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | $this->migrate->createTable($this->getName(), $columns); |
||
| 108 | $this->migrate->addPrimaryKey(null, $this->getName(), array_keys($columns)); |
||
| 109 | } |
||
| 110 | if ($this->_columns) { |
||
|
0 ignored issues
–
show
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 Loading history...
|
|||
| 111 | $this->migrate->upNewColumns([$this->getName() => $this->_columns]); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return null |
||
| 117 | */ |
||
| 118 | public function getRefTable() |
||
| 119 | { |
||
| 120 | return $this->_refTable; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getRefColumn() |
||
| 127 | { |
||
| 128 | if (!$this->_refColumn) { |
||
| 129 | $name = join("_", [$this->getRefTable(), "id"]); |
||
|
0 ignored issues
–
show
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 130 | } else { |
||
| 131 | $name = $this->_refColumn; |
||
| 132 | } |
||
| 133 | return Migration::setTablePrefix($name, ''); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return null |
||
| 138 | */ |
||
| 139 | public function getSourceTable() |
||
| 140 | { |
||
| 141 | return $this->_sourceTable; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function getSourceColumn() |
||
| 148 | { |
||
| 149 | if (!$this->_sourceColumn) { |
||
| 150 | $name = join("_", [$this->getSourceTable(), "id"]); |
||
|
0 ignored issues
–
show
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 151 | } else { |
||
| 152 | $name = $this->_sourceColumn; |
||
| 153 | } |
||
| 154 | return Migration::setTablePrefix($name, ''); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $name |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function refTable($name) |
||
| 162 | { |
||
| 163 | $this->_refTable = $name; |
||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param $name |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function sourceColumn($name) |
||
| 172 | { |
||
| 173 | $this->_sourceColumn = $name; |
||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $name |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function sourceTable($name) |
||
| 182 | { |
||
| 183 | $this->_sourceTable = $name; |
||
| 184 | return $this; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param $name |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function refColumn($name) |
||
| 192 | { |
||
| 193 | $this->_refColumn = $name; |
||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | } |
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.