| Conditions | 4 |
| Paths | 3 |
| Total Lines | 59 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 18 | public function up(Schema $schema): void |
||
| 19 | { |
||
| 20 | $settings = [ |
||
| 21 | [ |
||
| 22 | 'name' => '2fa_enable', |
||
| 23 | 'title' => 'Enable 2FA', |
||
| 24 | 'comment' => "Add fields in the password update page to enable 2FA using a TOTP authenticator app. When disabled globally, users won't see 2FA fields and won't be prompted for 2FA at login, even if they had enabled it previously.", |
||
| 25 | 'category' => 'security', |
||
| 26 | 'default' => 'false', |
||
| 27 | ], |
||
| 28 | ]; |
||
| 29 | |||
| 30 | foreach ($settings as $setting) { |
||
| 31 | $variable = addslashes($setting['name']); |
||
| 32 | $title = addslashes($setting['title']); |
||
| 33 | $comment = addslashes($setting['comment']); |
||
| 34 | $category = addslashes($setting['category']); |
||
| 35 | $default = addslashes($setting['default']); |
||
| 36 | |||
| 37 | $sqlCheck = \sprintf( |
||
| 38 | "SELECT COUNT(*) AS count |
||
| 39 | FROM settings |
||
| 40 | WHERE variable = '%s' |
||
| 41 | AND subkey IS NULL |
||
| 42 | AND access_url = 1", |
||
| 43 | $variable |
||
| 44 | ); |
||
| 45 | |||
| 46 | $stmt = $this->connection->executeQuery($sqlCheck); |
||
| 47 | $result = $stmt->fetchAssociative(); |
||
| 48 | |||
| 49 | if ($result && (int) $result['count'] > 0) { |
||
| 50 | $this->addSql(\sprintf( |
||
| 51 | "UPDATE settings |
||
| 52 | SET title = '%s', |
||
| 53 | comment = '%s', |
||
| 54 | category = '%s' |
||
| 55 | WHERE variable = '%s' |
||
| 56 | AND subkey IS NULL |
||
| 57 | AND access_url = 1", |
||
| 58 | $title, |
||
| 59 | $comment, |
||
| 60 | $category, |
||
| 61 | $variable |
||
| 62 | )); |
||
| 63 | $this->write(\sprintf('Updated setting: %s', $setting['name'])); |
||
| 64 | } else { |
||
| 65 | $this->addSql(\sprintf( |
||
| 66 | "INSERT INTO settings |
||
| 67 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 68 | VALUES |
||
| 69 | ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
||
| 70 | $variable, |
||
| 71 | $category, |
||
| 72 | $default, |
||
| 73 | $title, |
||
| 74 | $comment |
||
| 75 | )); |
||
| 76 | $this->write(\sprintf('Inserted setting: %s', $setting['name'])); |
||
| 77 | } |
||
| 97 |