| Total Complexity | 66 |
| Total Lines | 529 |
| Duplicated Lines | 0 % |
| Coverage | 79.47% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MysqlConstantWorker 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 MysqlConstantWorker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MysqlConstantWorker extends MySqlWorker implements ConstantWorker |
||
| 18 | { |
||
| 19 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 20 | /** |
||
| 21 | * Name of the class that contains all constants. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $className; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * All columns in the MySQL schema. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $columns = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array All constants. |
||
| 36 | */ |
||
| 37 | private $constants = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Filename with column names, their widths, and constant names. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $constantsFilename; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * All primary key labels, their widths and constant names. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $labels = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The previous column names, widths, and constant names (i.e. the content of $constantsFilename upon starting |
||
| 55 | * this program). |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $oldColumns = []; |
||
| 60 | |||
| 61 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | * |
||
| 66 | * @throws MySqlConnectFailedException |
||
| 67 | * @throws MySqlDataLayerException |
||
| 68 | * @throws RuntimeException |
||
| 69 | */ |
||
| 70 | 1 | public function execute(): int |
|
| 71 | { |
||
| 72 | 1 | $this->constantsFilename = $this->settings->optString('constants.columns'); |
|
| 73 | 1 | $this->className = $this->settings->optString('constants.class'); |
|
| 74 | |||
| 75 | 1 | if ($this->constantsFilename!==null || $this->className!==null) |
|
| 76 | { |
||
| 77 | 1 | $this->io->title('PhpStratum: MySql Constants'); |
|
| 78 | |||
| 79 | 1 | $this->connect(); |
|
| 80 | |||
| 81 | 1 | $this->executeEnabled(); |
|
| 82 | |||
| 83 | 1 | $this->disconnect(); |
|
| 84 | } |
||
| 85 | else |
||
| 86 | { |
||
| 87 | $this->io->logVerbose('Constants not enabled'); |
||
| 88 | } |
||
| 89 | |||
| 90 | 1 | return 0; |
|
| 91 | } |
||
| 92 | |||
| 93 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 94 | /** |
||
| 95 | * Enhances $oldColumns as follows: |
||
| 96 | * If the constant name is *, is is replaced with the column name prefixed by $this->myPrefix in uppercase. |
||
| 97 | * Otherwise the constant name is set to uppercase. |
||
| 98 | */ |
||
| 99 | 1 | private function enhanceColumns(): void |
|
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | 1 | } |
|
| 121 | |||
| 122 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 123 | /** |
||
| 124 | * Gathers constants based on column widths. |
||
| 125 | * |
||
| 126 | * @throws RuntimeException |
||
| 127 | * @throws MySqlQueryErrorException |
||
| 128 | */ |
||
| 129 | 1 | private function executeColumnWidths(): void |
|
| 130 | { |
||
| 131 | 1 | $this->loadOldColumns(); |
|
| 132 | |||
| 133 | 1 | $this->loadColumns(); |
|
| 134 | |||
| 135 | 1 | $this->enhanceColumns(); |
|
| 136 | |||
| 137 | 1 | $this->mergeColumns(); |
|
| 138 | |||
| 139 | 1 | $this->writeColumns(); |
|
| 140 | 1 | } |
|
| 141 | |||
| 142 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 143 | /** |
||
| 144 | * Creates constants declarations in a class. |
||
| 145 | * |
||
| 146 | * @throws MySqlQueryErrorException |
||
| 147 | * @throws RuntimeException |
||
| 148 | */ |
||
| 149 | 1 | private function executeCreateConstants(): void |
|
| 150 | { |
||
| 151 | 1 | $this->loadLabels(); |
|
| 152 | |||
| 153 | 1 | $this->fillConstants(); |
|
| 154 | |||
| 155 | 1 | $this->writeConstantClass(); |
|
| 156 | 1 | } |
|
| 157 | |||
| 158 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 159 | /** |
||
| 160 | * Executes the enabled functionalities. |
||
| 161 | * |
||
| 162 | * @throws RuntimeException |
||
| 163 | * @throws MySqlQueryErrorException |
||
| 164 | */ |
||
| 165 | 1 | private function executeEnabled(): void |
|
| 166 | { |
||
| 167 | 1 | if ($this->constantsFilename!==null) |
|
| 168 | { |
||
| 169 | 1 | $this->executeColumnWidths(); |
|
| 170 | } |
||
| 171 | |||
| 172 | 1 | if ($this->className!==null) |
|
| 173 | { |
||
| 174 | 1 | $this->executeCreateConstants(); |
|
| 175 | } |
||
| 176 | |||
| 177 | 1 | $this->logNumberOfConstants(); |
|
| 178 | 1 | } |
|
| 179 | |||
| 180 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 181 | /** |
||
| 182 | * Searches for 3 lines in the source code of the class for constants. The lines are: |
||
| 183 | * * The first line of the doc block with the annotation '@setbased.stratum.constants'. |
||
| 184 | * * The last line of this doc block. |
||
| 185 | * * The last line of continuous constant declarations directly after the doc block. |
||
| 186 | * If one of these line can not be found the line number will be set to null. |
||
| 187 | * |
||
| 188 | * @param string $source The source code of the constant class. |
||
| 189 | * |
||
| 190 | * @return array With the 3 line number as described |
||
| 191 | */ |
||
| 192 | 1 | private function extractLines(string $source): array |
|
| 193 | { |
||
| 194 | 1 | $tokens = token_get_all($source); |
|
| 195 | |||
| 196 | 1 | $line1 = null; |
|
| 197 | 1 | $line2 = null; |
|
| 198 | 1 | $line3 = null; |
|
| 199 | |||
| 200 | // Find annotation @constants |
||
| 201 | 1 | $step = 1; |
|
| 202 | 1 | foreach ($tokens as $token) |
|
| 203 | { |
||
| 204 | switch ($step) |
||
| 205 | { |
||
| 206 | 1 | case 1: |
|
| 207 | // Step 1: Find doc comment with annotation. |
||
| 208 | 1 | if (is_array($token) && $token[0]==T_DOC_COMMENT) |
|
| 209 | { |
||
| 210 | 1 | if (strpos($token[1], '@setbased.stratum.constants')!==false) |
|
| 211 | { |
||
| 212 | 1 | $line1 = $token[2]; |
|
| 213 | 1 | $step = 2; |
|
| 214 | } |
||
| 215 | } |
||
| 216 | 1 | break; |
|
| 217 | |||
| 218 | 1 | case 2: |
|
| 219 | // Step 2: Find end of doc block. |
||
| 220 | 1 | if (is_array($token)) |
|
| 221 | { |
||
| 222 | 1 | if ($token[0]==T_WHITESPACE) |
|
| 223 | { |
||
| 224 | 1 | $line2 = $token[2]; |
|
| 225 | 1 | if (substr_count($token[1], "\n")>1) |
|
| 226 | { |
||
| 227 | // Whitespace contains new line: end doc block without constants. |
||
| 228 | 1 | $step = 4; |
|
| 229 | } |
||
| 230 | } |
||
| 231 | else |
||
| 232 | { |
||
| 233 | 1 | if ($token[0]==T_CONST) |
|
| 234 | { |
||
| 235 | 1 | $line3 = $token[2]; |
|
| 236 | 1 | $step = 3; |
|
| 237 | } |
||
| 238 | else |
||
| 239 | { |
||
| 240 | $step = 4; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | 1 | break; |
|
| 245 | |||
| 246 | 1 | case 3: |
|
| 247 | // Step 4: Find en of constants declarations. |
||
| 248 | 1 | if (is_array($token)) |
|
| 249 | { |
||
| 250 | 1 | if ($token[0]==T_WHITESPACE) |
|
| 251 | { |
||
| 252 | 1 | if (substr_count($token[1], "\n")<=1) |
|
| 253 | { |
||
| 254 | // Ignore whitespace. |
||
| 255 | 1 | $line3 = $token[2]; |
|
| 256 | } |
||
| 257 | else |
||
| 258 | { |
||
| 259 | // Whitespace contains new line: end of const declarations. |
||
| 260 | 1 | $step = 4; |
|
| 261 | } |
||
| 262 | } |
||
| 263 | 1 | elseif ($token[0]==T_CONST || $token[2]==$line3) |
|
| 264 | { |
||
| 265 | 1 | $line3 = $token[2]; |
|
| 266 | } |
||
| 267 | else |
||
| 268 | { |
||
| 269 | $step = 4; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | 1 | break; |
|
| 273 | |||
| 274 | case 4: |
||
| 275 | // Leave loop. |
||
| 276 | break; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | // @todo get indent based on indent of the doc block. |
||
| 281 | |||
| 282 | 1 | return [$line1, $line2, $line3]; |
|
| 283 | } |
||
| 284 | |||
| 285 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 286 | /** |
||
| 287 | * Merges $columns and $labels (i.e. all known constants) into $constants. |
||
| 288 | */ |
||
| 289 | 1 | private function fillConstants(): void |
|
| 290 | { |
||
| 291 | 1 | foreach ($this->columns as $table_name => $table) |
|
| 292 | { |
||
| 293 | 1 | foreach ($table as $column_name => $column) |
|
| 294 | { |
||
| 295 | 1 | if (isset($this->columns[$table_name][$column_name]['constant_name'])) |
|
| 296 | { |
||
| 297 | $this->constants[$column['constant_name']] = $column['length']; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | 1 | foreach ($this->labels as $label => $id) |
|
| 303 | { |
||
| 304 | 1 | $this->constants[$label] = $id; |
|
| 305 | } |
||
| 306 | |||
| 307 | 1 | ksort($this->constants); |
|
| 308 | 1 | } |
|
| 309 | |||
| 310 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 311 | /** |
||
| 312 | * Loads the width of all columns in the MySQL schema into $columns. |
||
| 313 | * |
||
| 314 | * @throws MySqlQueryErrorException |
||
| 315 | */ |
||
| 316 | 1 | private function loadColumns(): void |
|
| 317 | { |
||
| 318 | 1 | $rows = $this->dl->allTableColumns(); |
|
| 319 | 1 | foreach ($rows as $row) |
|
| 320 | { |
||
| 321 | 1 | $row['length'] = DataTypeHelper::deriveFieldLength($row); |
|
| 322 | 1 | $this->columns[$row['table_name']][$row['column_name']] = $row; |
|
| 323 | } |
||
| 324 | 1 | } |
|
| 325 | |||
| 326 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 327 | /** |
||
| 328 | * Loads all primary key labels from the MySQL database. |
||
| 329 | * |
||
| 330 | * @throws MySqlQueryErrorException |
||
| 331 | */ |
||
| 332 | 1 | private function loadLabels(): void |
|
| 333 | { |
||
| 334 | 1 | $tables = $this->dl->allLabelTables(); |
|
| 335 | 1 | foreach ($tables as $table) |
|
| 336 | { |
||
| 337 | 1 | $rows = $this->dl->labelsFromTable($table['table_name'], $table['id'], $table['label']); |
|
| 338 | 1 | foreach ($rows as $row) |
|
| 339 | { |
||
| 340 | 1 | $this->labels[$row['label']] = $row['id']; |
|
| 341 | } |
||
| 342 | } |
||
| 343 | 1 | } |
|
| 344 | |||
| 345 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 346 | /** |
||
| 347 | * Loads from file $constantsFilename the previous table and column names, the width of the column, |
||
| 348 | * and the constant name (if assigned) and stores this data in $oldColumns. |
||
| 349 | * |
||
| 350 | * @throws RuntimeException |
||
| 351 | */ |
||
| 352 | 1 | private function loadOldColumns(): void |
|
| 353 | { |
||
| 354 | 1 | if (file_exists($this->constantsFilename)) |
|
| 355 | { |
||
| 356 | 1 | $handle = fopen($this->constantsFilename, 'r'); |
|
| 357 | |||
| 358 | 1 | $line_number = 0; |
|
| 359 | 1 | while (($line = fgets($handle))) |
|
|
1 ignored issue
–
show
|
|||
| 360 | { |
||
| 361 | 1 | $line_number++; |
|
| 362 | 1 | if ($line!="\n") |
|
| 363 | { |
||
| 364 | 1 | $n = preg_match('/^\s*(([a-zA-Z0-9_]+)\.)?([a-zA-Z0-9_]+)\.([a-zA-Z0-9_]+)\s+(\d+)\s*(\*|[a-zA-Z0-9_]+)?\s*$/', $line, $matches); |
|
| 365 | 1 | if ($n==0) |
|
| 366 | { |
||
| 367 | throw new RuntimeException("Illegal format at line %d in file '%s'.", |
||
| 368 | $line_number, |
||
| 369 | $this->constantsFilename); |
||
| 370 | } |
||
| 371 | |||
| 372 | 1 | if (isset($matches[6])) |
|
| 373 | { |
||
| 374 | $schema_name = $matches[2]; |
||
| 375 | $table_name = $matches[3]; |
||
| 376 | $column_name = $matches[4]; |
||
| 377 | $length = $matches[5]; |
||
| 378 | $constant_name = $matches[6]; |
||
| 379 | |||
| 380 | if ($schema_name) |
||
| 381 | { |
||
| 382 | $table_name = $schema_name.'.'.$table_name; |
||
| 383 | } |
||
| 384 | |||
| 385 | $this->oldColumns[$table_name][$column_name] = ['table_name' => $table_name, |
||
| 386 | 'column_name' => $column_name, |
||
| 387 | 'length' => $length, |
||
| 388 | 'constant_name' => $constant_name]; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | 1 | if (!feof($handle)) |
|
|
1 ignored issue
–
show
|
|||
| 393 | { |
||
| 394 | throw new RuntimeException("Error reading from file '%s'.", $this->constantsFilename); |
||
| 395 | } |
||
| 396 | |||
| 397 | 1 | $ok = fclose($handle); |
|
|
1 ignored issue
–
show
|
|||
| 398 | 1 | if ($ok===false) |
|
| 399 | { |
||
| 400 | throw new RuntimeException("Error closing file '%s'.", $this->constantsFilename); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | 1 | } |
|
| 404 | |||
| 405 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 406 | /** |
||
| 407 | * Logs the number of constants generated. |
||
| 408 | */ |
||
| 409 | 1 | private function logNumberOfConstants(): void |
|
| 417 | 1 | } |
|
| 418 | |||
| 419 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 420 | /** |
||
| 421 | * Generates PHP code with constant declarations. |
||
| 422 | * |
||
| 423 | * @return array The generated PHP code, lines are stored as rows in the array. |
||
| 424 | */ |
||
| 425 | 1 | private function makeConstantStatements(): array |
|
| 426 | { |
||
| 427 | 1 | $width1 = 0; |
|
| 428 | 1 | $width2 = 0; |
|
| 429 | 1 | $constants = []; |
|
| 430 | |||
| 431 | 1 | foreach ($this->constants as $constant => $value) |
|
| 432 | { |
||
| 433 | 1 | $width1 = max(mb_strlen($constant), $width1); |
|
| 434 | 1 | $width2 = max(mb_strlen((string)$value), $width2); |
|
| 435 | } |
||
| 436 | |||
| 437 | 1 | $line_format = sprintf(' const %%-%ds = %%%dd;', $width1, $width2); |
|
| 438 | 1 | foreach ($this->constants as $constant => $value) |
|
| 439 | { |
||
| 440 | 1 | $constants[] = sprintf($line_format, $constant, $value); |
|
| 441 | } |
||
| 442 | |||
| 443 | 1 | return $constants; |
|
| 444 | } |
||
| 445 | |||
| 446 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 447 | /** |
||
| 448 | * Preserves relevant data in $oldColumns into $columns. |
||
| 449 | */ |
||
| 450 | 1 | private function mergeColumns(): void |
|
| 459 | } |
||
| 460 | } |
||
| 461 | } |
||
| 462 | 1 | } |
|
| 463 | |||
| 464 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 465 | /** |
||
| 466 | * Writes table and column names, the width of the column, and the constant name (if assigned) to |
||
| 467 | * $constantsFilename. |
||
| 468 | */ |
||
| 469 | 1 | private function writeColumns(): void |
|
| 513 | 1 | } |
|
| 514 | |||
| 515 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 516 | /** |
||
| 517 | * Inserts new and replace old (if any) constant declaration statements in a PHP source file. |
||
| 518 | * |
||
| 519 | * @throws RuntimeException |
||
| 520 | */ |
||
| 521 | 1 | private function writeConstantClass(): void |
|
| 546 | 1 | } |
|
| 547 | |||
| 548 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 549 | } |
||
| 550 | |||
| 551 | //---------------------------------------------------------------------------------------------------------------------- |
||
| 552 |