| Conditions | 5 |
| Paths | 4 |
| Total Lines | 80 |
| Code Lines | 45 |
| 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 |
||
| 19 | public function up(Schema $schema): void |
||
| 20 | { |
||
| 21 | // 1. Add new column to user table |
||
| 22 | $this->addSql('ALTER TABLE `user` ADD COLUMN `password_updated_at` DATETIME DEFAULT NULL;'); |
||
| 23 | |||
| 24 | // 2. Insert or update the new setting in settings table |
||
| 25 | $setting = [ |
||
| 26 | 'variable' => 'password_rotation_days', |
||
| 27 | 'selected_value' => '0', |
||
| 28 | 'title' => 'Password rotation interval (days)', |
||
| 29 | 'comment' => 'Number of days before users must rotate their password (0 = disabled).', |
||
| 30 | 'category' => 'security', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | $sqlCheck = \sprintf( |
||
| 34 | "SELECT COUNT(*) AS count |
||
| 35 | FROM settings |
||
| 36 | WHERE variable = '%s' |
||
| 37 | AND subkey IS NULL |
||
| 38 | AND access_url = 1", |
||
| 39 | addslashes($setting['variable']) |
||
| 40 | ); |
||
| 41 | $result = $this->connection->fetchAssociative($sqlCheck); |
||
| 42 | |||
| 43 | if ($result && (int) $result['count'] > 0) { |
||
| 44 | // UPDATE existing setting |
||
| 45 | $this->addSql(\sprintf( |
||
| 46 | "UPDATE settings |
||
| 47 | SET selected_value = '%s', |
||
| 48 | title = '%s', |
||
| 49 | comment = '%s', |
||
| 50 | category = '%s' |
||
| 51 | WHERE variable = '%s' |
||
| 52 | AND subkey IS NULL |
||
| 53 | AND access_url = 1", |
||
| 54 | addslashes($setting['selected_value']), |
||
| 55 | addslashes($setting['title']), |
||
| 56 | addslashes($setting['comment']), |
||
| 57 | addslashes($setting['category']), |
||
| 58 | addslashes($setting['variable']) |
||
| 59 | )); |
||
| 60 | } else { |
||
| 61 | // INSERT new setting |
||
| 62 | $this->addSql(\sprintf( |
||
| 63 | "INSERT INTO settings |
||
| 64 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 65 | VALUES |
||
| 66 | ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
||
| 67 | addslashes($setting['variable']), |
||
| 68 | addslashes($setting['category']), |
||
| 69 | addslashes($setting['selected_value']), |
||
| 70 | addslashes($setting['title']), |
||
| 71 | addslashes($setting['comment']) |
||
| 72 | )); |
||
| 73 | } |
||
| 74 | |||
| 75 | // 3. Migrate existing extra-field data into the new column (if tables exist) |
||
| 76 | if ($schema->hasTable('extra_field_values') && $schema->hasTable('extra_field')) { |
||
| 77 | // Copy field_value into user.password_updated_at for variable 'password_updated_at' |
||
| 78 | $this->addSql(" |
||
| 79 | UPDATE `user` AS u |
||
| 80 | INNER JOIN `extra_field_values` AS efv |
||
| 81 | ON efv.item_id = u.id |
||
| 82 | INNER JOIN `extra_field` AS ef |
||
| 83 | ON ef.id = efv.field_id |
||
| 84 | AND ef.variable = 'password_updated_at' |
||
| 85 | SET u.password_updated_at = efv.field_value |
||
| 86 | "); |
||
| 87 | |||
| 88 | // Delete the old extra_field_values entries |
||
| 89 | $this->addSql(" |
||
| 90 | DELETE efv |
||
| 91 | FROM `extra_field_values` AS efv |
||
| 92 | INNER JOIN `extra_field` AS ef |
||
| 93 | ON ef.id = efv.field_id |
||
| 94 | AND ef.variable = 'password_updated_at' |
||
| 95 | "); |
||
| 96 | |||
| 97 | // Delete the old extra_field definition |
||
| 98 | $this->addSql(" |
||
| 99 | DELETE ef |
||
| 121 |