Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php declare(strict_types=1); |
||
| 40 | class Version0017Date20191210153032 extends SimpleMigrationStep { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param IOutput $output |
||
| 44 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
||
| 45 | * @param array $options |
||
| 46 | */ |
||
| 47 | public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param IOutput $output |
||
| 52 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
||
| 53 | * @param array $options |
||
| 54 | * |
||
| 55 | * @return null|ISchemaWrapper |
||
| 56 | * @throws SchemaException |
||
| 57 | */ |
||
| 58 | public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
||
| 59 | /** @var ISchemaWrapper $schema */ |
||
| 60 | $schema = $schemaClosure(); |
||
| 61 | |||
| 62 | $table = $schema->getTable('circles_members'); |
||
| 63 | if (!$table->hasColumn('instance')) { |
||
| 64 | $table->addColumn( |
||
| 65 | 'instance', 'string', [ |
||
| 66 | 'notnull' => false, |
||
| 67 | 'length' => 255, |
||
| 68 | ] |
||
| 69 | ); |
||
| 70 | $table->dropPrimaryKey(); |
||
| 71 | $table->setPrimaryKey(['circle_id', 'user_id', 'user_type', 'instance']); |
||
| 72 | } |
||
| 73 | |||
| 74 | View Code Duplication | if (!$schema->hasTable('circles_gsevents')) { |
|
|
|
|||
| 75 | $table = $schema->createTable('circles_gsevents'); |
||
| 76 | $table->addColumn( |
||
| 77 | 'token', 'string', [ |
||
| 78 | 'notnull' => false, |
||
| 79 | 'length' => 63, |
||
| 80 | ] |
||
| 81 | ); |
||
| 82 | $table->addColumn( |
||
| 83 | 'event', 'text', [ |
||
| 84 | 'notnull' => false |
||
| 85 | ] |
||
| 86 | ); |
||
| 87 | $table->addColumn( |
||
| 88 | 'instance', 'string', [ |
||
| 89 | 'length' => 255, |
||
| 90 | 'notnull' => false |
||
| 91 | ] |
||
| 92 | ); |
||
| 93 | $table->addColumn( |
||
| 94 | 'severity', 'integer', [ |
||
| 95 | 'length' => 3, |
||
| 96 | 'notnull' => false |
||
| 97 | ] |
||
| 98 | ); |
||
| 99 | $table->addColumn( |
||
| 100 | 'status', 'integer', [ |
||
| 101 | 'length' => 3, |
||
| 102 | 'notnull' => false |
||
| 103 | ] |
||
| 104 | ); |
||
| 105 | $table->addColumn( |
||
| 106 | 'creation', 'bigint', [ |
||
| 107 | 'length' => 14, |
||
| 108 | 'notnull' => false |
||
| 109 | ] |
||
| 110 | ); |
||
| 111 | $table->addUniqueIndex(['token', 'instance']); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (!$schema->hasTable('circles_gsshares')) { |
||
| 115 | $table = $schema->createTable('circles_gsshares'); |
||
| 116 | $table->addColumn( |
||
| 117 | 'id', 'integer', [ |
||
| 118 | 'notnull' => false, |
||
| 119 | 'length' => 11, |
||
| 120 | 'autoincrement' => true, |
||
| 121 | 'unsigned' => true |
||
| 122 | ] |
||
| 123 | ); |
||
| 124 | $table->addColumn( |
||
| 125 | 'circle_unique_id', 'string', [ |
||
| 126 | 'length' => 15, |
||
| 127 | 'notnull' => false |
||
| 128 | ] |
||
| 129 | ); |
||
| 130 | $table->addColumn( |
||
| 131 | 'owner', 'string', [ |
||
| 132 | 'length' => 15, |
||
| 133 | 'notnull' => false |
||
| 134 | ] |
||
| 135 | ); |
||
| 136 | $table->addColumn( |
||
| 137 | 'instance', 'string', [ |
||
| 138 | 'length' => 255, |
||
| 139 | 'notnull' => false |
||
| 140 | ] |
||
| 141 | ); |
||
| 142 | $table->addColumn( |
||
| 143 | 'token', 'string', [ |
||
| 144 | 'notnull' => false, |
||
| 145 | 'length' => 63 |
||
| 146 | ] |
||
| 147 | ); |
||
| 148 | $table->addColumn( |
||
| 149 | 'parent', 'integer', [ |
||
| 150 | 'notnull' => false, |
||
| 151 | 'length' => 11, |
||
| 152 | ] |
||
| 153 | ); |
||
| 154 | $table->addColumn( |
||
| 155 | 'mountpoint', 'text', [ |
||
| 156 | 'notnull' => false |
||
| 157 | ] |
||
| 158 | ); |
||
| 159 | $table->addColumn( |
||
| 160 | 'mountpoint_hash', 'string', [ |
||
| 161 | 'length' => 64, |
||
| 162 | 'notnull' => false |
||
| 163 | ] |
||
| 164 | ); |
||
| 165 | $table->setPrimaryKey(['id']); |
||
| 166 | $table->addUniqueIndex(['circle_unique_id', 'mountpoint_hash']); |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | return $schema; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * @param IOutput $output |
||
| 176 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
||
| 177 | * @param array $options |
||
| 178 | */ |
||
| 179 | public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
||
| 181 | |||
| 182 | } |
||
| 183 | |||
| 184 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.