| Conditions | 12 |
| Paths | 126 |
| Total Lines | 64 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 63 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 64 | { |
||
| 65 | $this->migration_service |
||
| 66 | ->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||
| 67 | |||
| 68 | $platform = DB::connection()->getDatabasePlatform(); |
||
| 69 | $platform->registerDoctrineTypeMapping(dbType: 'enum', doctrineType: 'string'); |
||
| 70 | |||
| 71 | $schema_manager = DB::connection()->createSchemaManager(); |
||
| 72 | $comparator = $schema_manager->createComparator(); |
||
| 73 | $source = $schema_manager->introspectSchema(); |
||
| 74 | $target = WebtreesSchema::schema(); |
||
| 75 | |||
| 76 | // doctrine/dbal 4.0 does not have the concept of "saveSQL" |
||
| 77 | foreach ($source->getTables() as $table) { |
||
| 78 | if (!$target->hasTable($table->getName())) { |
||
| 79 | $source->dropTable($table->getName()); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $schema_diff = $comparator->compareSchemas(oldSchema: $source, newSchema: $target); |
||
| 84 | $queries = $platform->getAlterSchemaSQL($schema_diff); |
||
| 85 | |||
| 86 | foreach ($target->getTables() as $table) { |
||
| 87 | foreach ($table->getForeignKeys() as $foreign_key) { |
||
| 88 | $queries[] = |
||
| 89 | 'DELETE FROM ' . $table->getName() . |
||
| 90 | ' WHERE (' . implode(', ', $foreign_key->getLocalColumns()) . ')' . |
||
| 91 | ' NOT IN (SELECT ' . implode(', ', $foreign_key->getForeignColumns()) . ' FROM ' . $foreign_key->getForeignTableName() . ')'; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // SQLite, PostgreSQL and SQL-Server all support DDL in transactions |
||
| 96 | if (DB::connection()->getDriver() instanceof AbstractMySQLDriver) { |
||
| 97 | $phase = fn (string $query): int => str_contains($query, 'DROP FOREIGN KEY') ? 1 : (str_contains($query, 'FOREIGN KEY') ? 3 : 2); |
||
| 98 | usort($queries, fn (string $x, string $y): int => $phase($x) <=> $phase($y)); |
||
| 99 | } else { |
||
| 100 | $queries = [ |
||
| 101 | 'START TRANSACTION', |
||
| 102 | ...$queries, |
||
| 103 | 'COMMIT', |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | |||
| 107 | foreach ($queries as $query) { |
||
| 108 | if (str_contains($query, 'RENAME INDEX')) { |
||
| 109 | echo '<p><i>', $query, ';</i></p>'; |
||
| 110 | } elseif (str_contains($query, 'CHANGE')) { |
||
| 111 | echo '<p><b>', $query, ';</b></p>'; |
||
| 112 | } else { |
||
| 113 | echo '<p>', $query, ';</p>'; |
||
| 114 | } |
||
| 115 | try { |
||
| 116 | //$t = microtime(true); |
||
| 117 | DB::connection()->executeStatement($query); |
||
| 118 | //echo '<p>', (int) (1000.8 * (microtime(true) - $t)), 'ms</p>'; |
||
| 119 | } catch (Throwable $ex) { |
||
| 120 | echo '<p style="color:red">', $ex->getMessage(), ';</p>'; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | exit; |
||
|
|
|||
| 125 | |||
| 126 | return $handler->handle($request); |
||
| 127 | } |
||
| 129 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.