Total Complexity | 40 |
Total Lines | 265 |
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 |
||
42 | } |
||
43 | |||
44 | public function addCommentOnTable(string $table, string $comment): string |
||
45 | { |
||
46 | return 'COMMENT ON TABLE ' |
||
47 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
48 | . ' IS ' |
||
49 | . $this->queryBuilder->quoter()->quoteValue($comment); |
||
50 | } |
||
51 | |||
52 | public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
||
53 | { |
||
54 | throw new NotSupportedException(static::class . ' does not support adding default value constraints.'); |
||
55 | } |
||
56 | |||
57 | public function addForeignKey( |
||
58 | string $name, |
||
59 | string $table, |
||
60 | array|string $columns, |
||
61 | string $refTable, |
||
62 | array|string $refColumns, |
||
63 | ?string $delete = null, |
||
64 | ?string $update = null |
||
65 | ): string { |
||
66 | $sql = 'ALTER TABLE ' |
||
67 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
68 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
69 | . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')' |
||
70 | . ' REFERENCES ' . $this->queryBuilder->quoter()->quoteTableName($refTable) |
||
71 | . ' (' . $this->queryBuilder->buildColumns($refColumns) . ')'; |
||
72 | |||
73 | if ($delete !== null) { |
||
74 | $sql .= ' ON DELETE ' . $delete; |
||
75 | } |
||
76 | |||
77 | if ($update !== null) { |
||
78 | $sql .= ' ON UPDATE ' . $update; |
||
79 | } |
||
80 | |||
81 | return $sql; |
||
82 | } |
||
83 | |||
84 | public function addPrimaryKey(string $name, string $table, array|string $columns): string |
||
85 | { |
||
86 | if (is_string($columns)) { |
||
87 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
88 | } |
||
89 | |||
90 | foreach ($columns as $i => $col) { |
||
91 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
92 | } |
||
93 | |||
94 | return 'ALTER TABLE ' |
||
95 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
96 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
97 | . ' PRIMARY KEY (' . implode(', ', $columns) . ')'; |
||
98 | } |
||
99 | |||
100 | public function addUnique(string $name, string $table, array|string $columns): string |
||
101 | { |
||
102 | if (is_string($columns)) { |
||
103 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
104 | } |
||
105 | |||
106 | foreach ($columns as $i => $col) { |
||
107 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
108 | } |
||
109 | |||
110 | return 'ALTER TABLE ' |
||
111 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
112 | . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
113 | . ' UNIQUE (' . implode(', ', $columns) . ')'; |
||
114 | } |
||
115 | |||
116 | public function alterColumn(string $table, string $column, string $type): string |
||
117 | { |
||
118 | return 'ALTER TABLE ' |
||
119 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
120 | . ' CHANGE ' |
||
121 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
122 | . ' ' |
||
123 | . $this->queryBuilder->quoter()->quoteColumnName($column) . ' ' |
||
124 | . $this->queryBuilder->getColumnType($type); |
||
125 | } |
||
126 | |||
127 | public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
||
128 | { |
||
129 | throw new NotSupportedException(static::class . ' does not support enabling/disabling integrity check.'); |
||
130 | } |
||
131 | |||
132 | public function createIndex(string $name, string $table, array|string $columns, bool $unique = false): string |
||
133 | { |
||
134 | return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') |
||
135 | . $this->queryBuilder->quoter()->quoteTableName($name) |
||
136 | . ' ON ' . $this->queryBuilder->quoter()->quoteTableName($table) |
||
137 | . ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
||
138 | } |
||
139 | |||
140 | public function createTable(string $table, array $columns, ?string $options = null): string |
||
141 | { |
||
142 | $cols = []; |
||
143 | |||
144 | foreach ($columns as $name => $type) { |
||
145 | if (is_string($name)) { |
||
146 | $cols[] = "\t" |
||
147 | . $this->queryBuilder->quoter()->quoteColumnName($name) |
||
148 | . ' ' |
||
149 | . $this->queryBuilder->getColumnType($type); |
||
150 | } else { |
||
151 | $cols[] = "\t" . $type; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $sql = 'CREATE TABLE ' |
||
156 | . $this->queryBuilder->quoter()->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
||
157 | |||
158 | return $options === null ? $sql : $sql . ' ' . $options; |
||
159 | } |
||
160 | |||
161 | public function createView(string $viewName, Query|string $subQuery): string |
||
162 | { |
||
163 | if ($subQuery instanceof QueryInterface) { |
||
164 | /** @psalm-var array<array-key, int|string> $params */ |
||
165 | [$rawQuery, $params] = $this->queryBuilder->build($subQuery); |
||
166 | |||
167 | foreach ($params as $key => $value) { |
||
168 | $params[$key] = $this->queryBuilder->quoter()->quoteValue($value); |
||
169 | } |
||
170 | |||
171 | $subQuery = strtr($rawQuery, $params); |
||
172 | } |
||
173 | |||
174 | return 'CREATE VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName) . ' AS ' . $subQuery; |
||
175 | } |
||
176 | |||
177 | public function dropCheck(string $name, string $table): string |
||
178 | { |
||
179 | return 'ALTER TABLE ' |
||
180 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
181 | . ' DROP CONSTRAINT ' |
||
182 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
183 | } |
||
184 | |||
185 | public function dropColumn(string $table, string $column): string |
||
186 | { |
||
187 | return 'ALTER TABLE ' |
||
188 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
189 | . ' DROP COLUMN ' |
||
190 | . $this->queryBuilder->quoter()->quoteColumnName($column); |
||
191 | } |
||
192 | |||
193 | public function dropCommentFromColumn(string $table, string $column): string |
||
194 | { |
||
195 | return 'COMMENT ON COLUMN ' |
||
196 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
197 | . '.' |
||
198 | . $this->queryBuilder->quoter()->quoteColumnName($column) |
||
199 | . ' IS NULL'; |
||
200 | } |
||
201 | |||
202 | public function dropCommentFromTable(string $table): string |
||
203 | { |
||
204 | return 'COMMENT ON TABLE ' |
||
205 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
206 | . ' IS NULL'; |
||
207 | } |
||
208 | |||
209 | public function dropDefaultValue(string $name, string $table): string |
||
210 | { |
||
211 | throw new NotSupportedException(static::class . ' does not support dropping default value constraints.'); |
||
212 | } |
||
213 | |||
214 | public function dropForeignKey(string $name, string $table): string |
||
215 | { |
||
216 | return 'ALTER TABLE ' |
||
217 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
218 | . ' DROP CONSTRAINT ' |
||
219 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
220 | } |
||
221 | |||
222 | public function dropIndex(string $name, string $table): string |
||
223 | { |
||
224 | return 'DROP INDEX ' |
||
225 | . $this->queryBuilder->quoter()->quoteTableName($name) |
||
226 | . ' ON ' |
||
227 | . $this->queryBuilder->quoter()->quoteTableName($table); |
||
228 | } |
||
229 | |||
230 | public function dropPrimaryKey(string $name, string $table): string |
||
231 | { |
||
232 | return 'ALTER TABLE ' |
||
233 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
234 | . ' DROP CONSTRAINT ' |
||
235 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
236 | } |
||
237 | |||
238 | public function dropTable(string $table): string |
||
239 | { |
||
240 | return 'DROP TABLE ' . $this->queryBuilder->quoter()->quoteTableName($table); |
||
241 | } |
||
242 | |||
243 | public function dropUnique(string $name, string $table): string |
||
244 | { |
||
245 | return 'ALTER TABLE ' |
||
246 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
247 | . ' DROP CONSTRAINT ' |
||
248 | . $this->queryBuilder->quoter()->quoteColumnName($name); |
||
249 | } |
||
250 | |||
251 | public function dropView(string $viewName): string |
||
252 | { |
||
253 | return 'DROP VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName); |
||
254 | } |
||
255 | |||
256 | public function renameColumn(string $table, string $oldName, string $newName): string |
||
257 | { |
||
258 | return 'ALTER TABLE ' |
||
259 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
260 | . ' RENAME COLUMN ' . $this->queryBuilder->quoter()->quoteColumnName($oldName) |
||
261 | . ' TO ' . $this->queryBuilder->quoter()->quoteColumnName($newName); |
||
262 | } |
||
263 | |||
264 | public function renameTable(string $oldName, string $newName): string |
||
265 | { |
||
266 | return 'RENAME TABLE ' |
||
267 | . $this->queryBuilder->quoter()->quoteTableName($oldName) |
||
268 | . ' TO ' . $this->queryBuilder->quoter()->quoteTableName($newName); |
||
269 | } |
||
270 | |||
271 | public function truncateTable(string $table): string |
||
274 | } |
||
275 | } |
||
276 |
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