| Conditions | 8 |
| Paths | 50 |
| Total Lines | 53 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | public function alterColumn(string $table, string $column, ColumnInterface|string $type): string |
||
| 31 | { |
||
| 32 | $columnName = $this->quoter->quoteColumnName($column); |
||
|
|
|||
| 33 | $tableName = $this->quoter->quoteTableName($table); |
||
| 34 | |||
| 35 | if ($type instanceof ColumnInterface) { |
||
| 36 | $type = $type->asString(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @link https://github.com/yiisoft/yii2/issues/4492 |
||
| 41 | * @link https://www.postgresql.org/docs/9.1/static/sql-altertable.html |
||
| 42 | */ |
||
| 43 | if (preg_match('/^(DROP|SET|RESET|USING)\s+/i', $type)) { |
||
| 44 | return "ALTER TABLE $tableName ALTER COLUMN $columnName $type"; |
||
| 45 | } |
||
| 46 | |||
| 47 | $type = 'TYPE ' . $this->queryBuilder->getColumnType($type); |
||
| 48 | $multiAlterStatement = []; |
||
| 49 | $constraintPrefix = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column); |
||
| 50 | |||
| 51 | if (preg_match('/\s+DEFAULT\s+(["\']?\w*["\']?)/i', $type, $matches)) { |
||
| 52 | $type = preg_replace('/\s+DEFAULT\s+(["\']?\w*["\']?)/i', '', $type); |
||
| 53 | $multiAlterStatement[] = "ALTER COLUMN $columnName SET DEFAULT $matches[1]"; |
||
| 54 | } |
||
| 55 | |||
| 56 | $type = preg_replace('/\s+NOT\s+NULL/i', '', $type, -1, $count); |
||
| 57 | |||
| 58 | if ($count) { |
||
| 59 | $multiAlterStatement[] = "ALTER COLUMN $columnName SET NOT NULL"; |
||
| 60 | } else { |
||
| 61 | /** remove additional null if any */ |
||
| 62 | $type = preg_replace('/\s+NULL/i', '', $type, -1, $count); |
||
| 63 | if ($count) { |
||
| 64 | $multiAlterStatement[] = "ALTER COLUMN $columnName DROP NOT NULL"; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | if (preg_match('/\s+CHECK\s+\((.+)\)/i', $type, $matches)) { |
||
| 69 | $type = preg_replace('/\s+CHECK\s+\((.+)\)/i', '', $type); |
||
| 70 | $multiAlterStatement[] = "ADD CONSTRAINT {$constraintPrefix}_check CHECK ($matches[1])"; |
||
| 71 | } |
||
| 72 | |||
| 73 | $type = preg_replace('/\s+UNIQUE/i', '', $type, -1, $count); |
||
| 74 | |||
| 75 | if ($count) { |
||
| 76 | $multiAlterStatement[] = "ADD UNIQUE ($columnName)"; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** add what's left at the beginning */ |
||
| 80 | array_unshift($multiAlterStatement, "ALTER COLUMN $columnName $type"); |
||
| 81 | |||
| 82 | return 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $multiAlterStatement); |
||
| 83 | } |
||
| 161 |