Total Complexity | 57 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Database extends AbstractDatabase |
||
16 | { |
||
17 | /** |
||
18 | * @inheritDoc |
||
19 | */ |
||
20 | public function alterTable(string $table, string $name, array $fields, array $foreign, |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @inheritDoc |
||
57 | */ |
||
58 | public function alterIndexes(string $table, array $alter) |
||
59 | { |
||
60 | foreach ($alter as $key => $val) { |
||
61 | $alter[$key] = ( |
||
62 | $val[2] == "DROP" |
||
63 | ? "\nDROP INDEX " . $this->driver->escapeId($val[1]) |
||
64 | : "\nADD $val[0] " . ($val[0] == "PRIMARY" ? "KEY " : "") . ($val[1] != "" ? $this->driver->escapeId($val[1]) . " " : "") . "(" . implode(", ", $val[2]) . ")" |
||
65 | ); |
||
66 | } |
||
67 | $result = $this->driver->execute("ALTER TABLE " . $this->driver->table($table) . implode(",", $alter)); |
||
68 | return $result !== false; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @inheritDoc |
||
73 | */ |
||
74 | public function tables() |
||
75 | { |
||
76 | return $this->driver->keyValues($this->driver->minVersion(5) ? |
||
77 | "SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME" : |
||
78 | "SHOW TABLES"); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @inheritDoc |
||
83 | */ |
||
84 | public function countTables(array $databases) |
||
85 | { |
||
86 | $counts = []; |
||
87 | foreach ($databases as $database) { |
||
88 | $counts[$database] = count($this->driver->values("SHOW TABLES IN " . $this->driver->escapeId($database))); |
||
89 | } |
||
90 | return $counts; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | public function dropViews(array $views) |
||
97 | { |
||
98 | $this->driver->execute("DROP VIEW " . implode(", ", array_map(function ($view) { |
||
99 | return $this->driver->table($view); |
||
100 | }, $views))); |
||
101 | return true; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @inheritDoc |
||
106 | */ |
||
107 | public function dropTables(array $tables) |
||
108 | { |
||
109 | $this->driver->execute("DROP TABLE " . implode(", ", array_map(function ($table) { |
||
110 | return $this->driver->table($table); |
||
111 | }, $tables))); |
||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | public function truncateTables(array $tables) |
||
119 | { |
||
120 | return $this->driver->applyQueries("TRUNCATE TABLE", $tables); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @inheritDoc |
||
125 | */ |
||
126 | public function moveTables(array $tables, array $views, string $target) |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @inheritDoc |
||
152 | */ |
||
153 | public function copyTables(array $tables, array $views, string $target) |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function events() |
||
187 | { |
||
188 | return $this->driver->rows("SHOW EVENTS"); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @inheritDoc |
||
193 | */ |
||
194 | public function routine(string $name, string $type) |
||
195 | { |
||
196 | $enumLength = $this->driver->enumLength(); |
||
197 | $aliases = ["bool", "boolean", "integer", "double precision", "real", "dec", |
||
198 | "numeric", "fixed", "national char", "national varchar"]; |
||
199 | $space = "(?:\\s|/\\*[\s\S]*?\\*/|(?:#|-- )[^\n]*\n?|--\r?\n)"; |
||
200 | $type_pattern = "((" . implode("|", array_merge(array_keys($this->driver->types()), $aliases)) . |
||
201 | ")\\b(?:\\s*\\(((?:[^'\")]|$enumLength)++)\\))?\\s*(zerofill\\s*)?(unsigned" . |
||
202 | "(?:\\s+zerofill)?)?)(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s,]+)['\"]?)?"; |
||
203 | $pattern = "$space*(" . ($type == "FUNCTION" ? "" : $this->driver->inout()) . |
||
204 | ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; |
||
205 | $create = $this->connection->result("SHOW CREATE $type " . $this->driver->escapeId($name), 2); |
||
206 | preg_match("~\\(((?:$pattern\\s*,?)*)\\)\\s*" . |
||
207 | ($type == "FUNCTION" ? "RETURNS\\s+$type_pattern\\s+" : "") . "(.*)~is", $create, $match); |
||
208 | $fields = []; |
||
209 | preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER); |
||
210 | foreach ($matches as $param) { |
||
211 | $fields[] = [ |
||
212 | "field" => str_replace("``", "`", $param[2]) . $param[3], |
||
213 | "type" => strtolower($param[5]), |
||
214 | "length" => preg_replace_callback("~$enumLength~s", 'normalize_enum', $param[6]), |
||
215 | "unsigned" => strtolower(preg_replace('~\s+~', ' ', trim("$param[8] $param[7]"))), |
||
216 | "null" => 1, |
||
217 | "full_type" => $param[4], |
||
218 | "inout" => strtoupper($param[1]), |
||
219 | "collation" => strtolower($param[9]), |
||
220 | ]; |
||
221 | } |
||
222 | if ($type != "FUNCTION") { |
||
223 | return ["fields" => $fields, "definition" => $match[11]]; |
||
224 | } |
||
225 | return [ |
||
226 | "fields" => $fields, |
||
227 | "returns" => ["type" => $match[12], "length" => $match[13], "unsigned" => $match[15], "collation" => $match[16]], |
||
228 | "definition" => $match[17], |
||
229 | "language" => "SQL", // available in information_schema.ROUTINES.PARAMETER_STYLE |
||
230 | ]; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @inheritDoc |
||
235 | */ |
||
236 | public function routines() |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @inheritDoc |
||
249 | */ |
||
250 | public function routineId(string $name, array $row) |
||
253 | } |
||
254 | } |
||
255 |
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