| Conditions | 4 |
| Paths | 3 |
| Total Lines | 55 |
| Code Lines | 40 |
| 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 |
||
| 19 | public function up(Schema $schema): void |
||
| 20 | { |
||
| 21 | $settings = [ |
||
| 22 | [ |
||
| 23 | 'name' => 'disable_gdpr', |
||
| 24 | 'title' => 'Disable GDPR features', |
||
| 25 | 'comment' => 'If you already manage your personal data protection declaration to users elsewhere, you can safely disable this feature.', |
||
| 26 | ], |
||
| 27 | ]; |
||
| 28 | |||
| 29 | foreach ($settings as $setting) { |
||
| 30 | $variable = addslashes($setting['name']); |
||
| 31 | $title = addslashes($setting['title']); |
||
| 32 | $comment = addslashes($setting['comment']); |
||
| 33 | |||
| 34 | // Check if the setting exists for the main access_url (1) and no subkey |
||
| 35 | $sqlCheck = \sprintf( |
||
| 36 | "SELECT COUNT(*) AS count |
||
| 37 | FROM settings |
||
| 38 | WHERE variable = '%s' |
||
| 39 | AND subkey IS NULL |
||
| 40 | AND access_url = 1", |
||
| 41 | $variable |
||
| 42 | ); |
||
| 43 | |||
| 44 | $stmt = $this->connection->executeQuery($sqlCheck); |
||
| 45 | $result = $stmt->fetchAssociative(); |
||
| 46 | |||
| 47 | if ($result && (int) $result['count'] > 0) { |
||
| 48 | // Update only title/comment/category; keep selected_value as is |
||
| 49 | $this->addSql(\sprintf( |
||
| 50 | "UPDATE settings |
||
| 51 | SET title = '%s', |
||
| 52 | comment = '%s', |
||
| 53 | category = 'privacy' |
||
| 54 | WHERE variable = '%s' |
||
| 55 | AND subkey IS NULL |
||
| 56 | AND access_url = 1", |
||
| 57 | $title, |
||
| 58 | $comment, |
||
| 59 | $variable |
||
| 60 | )); |
||
| 61 | $this->write(\sprintf('Updated setting: %s', $setting['name'])); |
||
| 62 | } else { |
||
| 63 | // Insert with sensible defaults (selected_value = 'false') |
||
| 64 | $this->addSql(\sprintf( |
||
| 65 | "INSERT INTO settings |
||
| 66 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 67 | VALUES |
||
| 68 | ('%s', NULL, NULL, 'privacy', 'false', '%s', '%s', 1, 0, 1)", |
||
| 69 | $variable, |
||
| 70 | $title, |
||
| 71 | $comment |
||
| 72 | )); |
||
| 73 | $this->write(\sprintf('Inserted setting: %s', $setting['name'])); |
||
| 74 | } |
||
| 94 |