| Conditions | 9 |
| Paths | 108 |
| Total Lines | 51 |
| Code Lines | 31 |
| 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 |
||
| 91 | public function updateAuditTable(AbstractSchemaManager $schemaManager, Table $table, EntityManager $em): void |
||
| 92 | { |
||
| 93 | $fromSchema = $schemaManager->createSchema(); |
||
| 94 | $toSchema = clone $fromSchema; |
||
| 95 | |||
| 96 | $table = $toSchema->getTable($table->getName()); |
||
| 97 | $columns = $schemaManager->listTableColumns($table->getName()); |
||
| 98 | $expectedColumns = $this->manager->getHelper()->getAuditTableColumns(); |
||
| 99 | $expectedIndices = $this->manager->getHelper()->getAuditTableIndices($table->getName()); |
||
| 100 | $processed = []; |
||
| 101 | |||
| 102 | // process columns |
||
| 103 | foreach ($columns as $column) { |
||
| 104 | if (array_key_exists($column->getName(), $expectedColumns)) { |
||
| 105 | // column is part of expected columns |
||
| 106 | $table->dropColumn($column->getName()); |
||
| 107 | $table->addColumn($column->getName(), $expectedColumns[$column->getName()]['type'], $expectedColumns[$column->getName()]['options']); |
||
| 108 | } else { |
||
| 109 | // column is not part of expected columns so it has to be removed |
||
| 110 | $table->dropColumn($column->getName()); |
||
| 111 | } |
||
| 112 | |||
| 113 | $processed[] = $column->getName(); |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($expectedColumns as $column => $options) { |
||
|
|
|||
| 117 | if (!\in_array($column, $processed, true)) { |
||
| 118 | // expected column in not part of concrete ones so it's a new column, we need to add it |
||
| 119 | $table->addColumn($column, $options['type'], $options['options']); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // process indices |
||
| 124 | foreach ($expectedIndices as $column => $options) { |
||
| 125 | if ('primary' === $options['type']) { |
||
| 126 | $table->dropPrimaryKey(); |
||
| 127 | $table->setPrimaryKey([$column]); |
||
| 128 | } else { |
||
| 129 | $table->dropIndex($options['name']); |
||
| 130 | $table->addIndex([$column], $options['name']); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // apply changes |
||
| 135 | $sql = $fromSchema->getMigrateToSql($toSchema, $schemaManager->getDatabasePlatform()); |
||
| 136 | foreach ($sql as $query) { |
||
| 137 | try { |
||
| 138 | $statement = $em->getConnection()->prepare($query); |
||
| 139 | $statement->execute(); |
||
| 140 | } catch (\Exception $e) { |
||
| 141 | throw new UpdateException(sprintf('Failed to update/fix "%s" audit table.', $table->getName())); |
||
| 142 | } |
||
| 146 |