| Total Complexity | 40 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DDLQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DDLQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class DDLQueryBuilder |
||
| 10 | { |
||
| 11 | public function __construct(private QueryBuilderInterface $queryBuilder) |
||
|
|
|||
| 12 | { |
||
| 13 | } |
||
| 14 | |||
| 15 | public function addCheck(string $name, string $table, string $expression): string |
||
| 22 | } |
||
| 23 | |||
| 24 | public function addColumn(string $table, string $column, string $type): string |
||
| 25 | { |
||
| 26 | return 'ALTER TABLE ' |
||
| 27 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 28 | . ' ADD ' |
||
| 29 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
| 30 | . ' ' |
||
| 31 | . $this->queryBuilder->getColumnType($type); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function addCommentOnColumn(string $table, string $column, string $comment): string |
||
| 35 | { |
||
| 36 | return 'COMMENT ON COLUMN ' |
||
| 37 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 38 | . '.' |
||
| 39 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
| 40 | . ' IS ' |
||
| 41 | . $this->queryBuilder->quoter()->quoteValue($comment); |
||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | public function addCommentOnTable(string $table, string $comment): string |
||
| 46 | { |
||
| 47 | return 'COMMENT ON TABLE ' |
||
| 48 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 49 | . ' IS ' |
||
| 50 | . $this->queryBuilder->quoter()->quoteValue($comment); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
||
| 54 | { |
||
| 55 | throw new NotSupportedException(static::class . ' does not support adding default value constraints.'); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function addForeignKey( |
||
| 59 | string $name, |
||
| 60 | string $table, |
||
| 61 | array|string $columns, |
||
| 62 | string $refTable, |
||
| 63 | array|string $refColumns, |
||
| 64 | ?string $delete = null, |
||
| 65 | ?string $update = null |
||
| 66 | ): string { |
||
| 67 | $sql = 'ALTER TABLE ' |
||
| 68 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 69 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
| 70 | . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')' |
||
| 71 | . ' REFERENCES ' . $this->queryBuilder->quoter()->quoteTableName($refTable) |
||
| 72 | . ' (' . $this->queryBuilder->buildColumns($refColumns) . ')'; |
||
| 73 | |||
| 74 | if ($delete !== null) { |
||
| 75 | $sql .= ' ON DELETE ' . $delete; |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($update !== null) { |
||
| 79 | $sql .= ' ON UPDATE ' . $update; |
||
| 80 | } |
||
| 81 | |||
| 82 | return $sql; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function addPrimaryKey(string $name, string $table, array|string $columns): string |
||
| 86 | { |
||
| 87 | if (is_string($columns)) { |
||
| 88 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
| 89 | } |
||
| 90 | |||
| 91 | foreach ($columns as $i => $col) { |
||
| 92 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 93 | } |
||
| 94 | |||
| 95 | return 'ALTER TABLE ' |
||
| 96 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 97 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
| 98 | . ' PRIMARY KEY (' . implode(', ', $columns) . ')'; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function addUnique(string $name, string $table, array|string $columns): string |
||
| 102 | { |
||
| 103 | if (is_string($columns)) { |
||
| 104 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
| 105 | } |
||
| 106 | |||
| 107 | foreach ($columns as $i => $col) { |
||
| 108 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 109 | } |
||
| 110 | |||
| 111 | return 'ALTER TABLE ' |
||
| 112 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 113 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
| 114 | . ' UNIQUE (' . implode(', ', $columns) . ')'; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function alterColumn(string $table, string $column, string $type): string |
||
| 118 | { |
||
| 119 | return 'ALTER TABLE ' |
||
| 120 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 121 | . ' CHANGE ' |
||
| 122 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
| 123 | . ' ' |
||
| 124 | . $this->queryBuilder->quoter()->quoteColumnName($column) . ' ' |
||
| 125 | . $this->queryBuilder->getColumnType($type); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
||
| 129 | { |
||
| 130 | throw new NotSupportedException(static::class . ' does not support enabling/disabling integrity check.'); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function createIndex(string $name, string $table, array|string $columns, bool $unique = false): string |
||
| 134 | { |
||
| 135 | return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') |
||
| 136 | . $this->queryBuilder->quoter()->quoteTableName($name) |
||
| 137 | . ' ON ' . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 138 | . ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function createTable(string $table, array $columns, ?string $options = null): string |
||
| 142 | { |
||
| 143 | $cols = []; |
||
| 144 | |||
| 145 | foreach ($columns as $name => $type) { |
||
| 146 | if (is_string($name)) { |
||
| 147 | $cols[] = "\t" |
||
| 148 | . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
| 149 | . ' ' |
||
| 150 | . $this->queryBuilder->getColumnType($type); |
||
| 151 | } else { |
||
| 152 | $cols[] = "\t" . $type; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $sql = 'CREATE TABLE ' |
||
| 157 | . $this->queryBuilder->quoter()->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
||
| 158 | |||
| 159 | return $options === null ? $sql : $sql . ' ' . $options; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function createView(string $viewName, Query|string $subQuery): string |
||
| 163 | { |
||
| 164 | if ($subQuery instanceof Query) { |
||
| 165 | /** @psalm-var array<array-key, int|string> $params */ |
||
| 166 | [$rawQuery, $params] = $this->queryBuilder->build($subQuery); |
||
| 167 | |||
| 168 | foreach ($params as $key => $value) { |
||
| 169 | $params[$key] = $this->queryBuilder->quoter()->quoteValue($value); |
||
| 170 | } |
||
| 171 | |||
| 172 | $subQuery = strtr($rawQuery, $params); |
||
| 173 | } |
||
| 174 | |||
| 175 | return 'CREATE VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName) . ' AS ' . $subQuery; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function dropCheck(string $name, string $table): string |
||
| 179 | { |
||
| 180 | return 'ALTER TABLE ' |
||
| 181 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 182 | . ' DROP CONSTRAINT ' |
||
| 183 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function dropColumn(string $table, string $column): string |
||
| 187 | { |
||
| 188 | return 'ALTER TABLE ' |
||
| 189 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 190 | . ' DROP COLUMN ' |
||
| 191 | . $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function dropCommentFromColumn(string $table, string $column): string |
||
| 195 | { |
||
| 196 | return 'COMMENT ON COLUMN ' |
||
| 197 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 198 | . '.' |
||
| 199 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
| 200 | . ' IS NULL'; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function dropCommentFromTable(string $table): string |
||
| 204 | { |
||
| 205 | return 'COMMENT ON TABLE ' |
||
| 206 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 207 | . ' IS NULL'; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function dropDefaultValue(string $name, string $table): string |
||
| 211 | { |
||
| 212 | throw new NotSupportedException(static::class . ' does not support dropping default value constraints.'); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function dropForeignKey(string $name, string $table): string |
||
| 216 | { |
||
| 217 | return 'ALTER TABLE ' |
||
| 218 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 219 | . ' DROP CONSTRAINT ' |
||
| 220 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function dropIndex(string $name, string $table): string |
||
| 224 | { |
||
| 225 | return 'DROP INDEX ' |
||
| 226 | . $this->queryBuilder->quoter()->quoteTableName($name) |
||
| 227 | . ' ON ' |
||
| 228 | . $this->queryBuilder->quoter()->quoteTableName($table); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function dropPrimaryKey(string $name, string $table): string |
||
| 232 | { |
||
| 233 | return 'ALTER TABLE ' |
||
| 234 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 235 | . ' DROP CONSTRAINT ' |
||
| 236 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function dropTable(string $table): string |
||
| 240 | { |
||
| 241 | return 'DROP TABLE ' . $this->queryBuilder->quoter()->quoteTableName($table); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function dropUnique(string $name, string $table): string |
||
| 245 | { |
||
| 246 | return 'ALTER TABLE ' |
||
| 247 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 248 | . ' DROP CONSTRAINT ' |
||
| 249 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function dropView(string $viewName): string |
||
| 253 | { |
||
| 254 | return 'DROP VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function renameColumn(string $table, string $oldName, string $newName): string |
||
| 258 | { |
||
| 259 | return 'ALTER TABLE ' |
||
| 260 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
| 261 | . ' RENAME COLUMN ' . $this->queryBuilder->quoter()->quoteColumnName($oldName) |
||
| 262 | . ' TO ' . $this->queryBuilder->quoter()->quoteColumnName($newName); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function renameTable(string $oldName, string $newName): string |
||
| 266 | { |
||
| 267 | return 'RENAME TABLE ' |
||
| 268 | . $this->queryBuilder->quoter()->quoteTableName($oldName) |
||
| 269 | . ' TO ' . $this->queryBuilder->quoter()->quoteTableName($newName); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function truncateTable(string $table): string |
||
| 275 | } |
||
| 276 | } |
||
| 277 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths