| Conditions | 10 |
| Paths | 21 |
| Total Lines | 117 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 23 | public function up(Schema $schema): void |
||
| 24 | { |
||
| 25 | // Upsert settings (title/comment/category and default selected_value if missing) |
||
| 26 | $settings = [ |
||
| 27 | [ |
||
| 28 | 'name' => 'disable_gdpr', |
||
| 29 | 'title' => 'Disable GDPR features', |
||
| 30 | 'comment' => 'If you already manage your personal data protection declaration to users elsewhere, you can safely disable this feature.', |
||
| 31 | 'category' => 'profile', |
||
| 32 | 'default' => 'false', |
||
| 33 | ], |
||
| 34 | [ |
||
| 35 | 'name' => 'show_conditions_to_user', |
||
| 36 | 'title' => 'Show specific registration conditions', |
||
| 37 | 'comment' => "Show multiple conditions to user during sign up process. Provide an array with each element containing 'variable' (internal extra field name), 'display_text' (simple text for a checkbox), 'text_area' (long text of conditions).", |
||
| 38 | 'category' => 'registration', |
||
| 39 | 'default' => '[]', |
||
| 40 | ], |
||
| 41 | ]; |
||
| 42 | |||
| 43 | foreach ($settings as $setting) { |
||
| 44 | $variable = addslashes($setting['name']); |
||
| 45 | $title = addslashes($setting['title']); |
||
| 46 | $comment = addslashes($setting['comment']); |
||
| 47 | $category = addslashes($setting['category']); |
||
| 48 | $default = addslashes($setting['default']); |
||
| 49 | |||
| 50 | $sqlCheck = \sprintf( |
||
| 51 | "SELECT COUNT(*) AS count |
||
| 52 | FROM settings |
||
| 53 | WHERE variable = '%s' |
||
| 54 | AND subkey IS NULL |
||
| 55 | AND access_url = 1", |
||
| 56 | $variable |
||
| 57 | ); |
||
| 58 | |||
| 59 | $stmt = $this->connection->executeQuery($sqlCheck); |
||
| 60 | $result = $stmt->fetchAssociative(); |
||
| 61 | |||
| 62 | if ($result && (int) $result['count'] > 0) { |
||
| 63 | $this->addSql(\sprintf( |
||
| 64 | "UPDATE settings |
||
| 65 | SET title = '%s', |
||
| 66 | comment = '%s', |
||
| 67 | category = '%s' |
||
| 68 | WHERE variable = '%s' |
||
| 69 | AND subkey IS NULL |
||
| 70 | AND access_url = 1", |
||
| 71 | $title, |
||
| 72 | $comment, |
||
| 73 | $category, |
||
| 74 | $variable |
||
| 75 | )); |
||
| 76 | $this->write(\sprintf('Updated setting: %s', $setting['name'])); |
||
| 77 | } else { |
||
| 78 | $this->addSql(\sprintf( |
||
| 79 | "INSERT INTO settings |
||
| 80 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 81 | VALUES |
||
| 82 | ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
||
| 83 | $variable, |
||
| 84 | $category, |
||
| 85 | $default, |
||
| 86 | $title, |
||
| 87 | $comment |
||
| 88 | )); |
||
| 89 | $this->write(\sprintf('Inserted setting: %s', $setting['name'])); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // Link show_conditions_to_user to its value template (from fixtures), only this variable |
||
| 94 | $targetVariable = 'show_conditions_to_user'; |
||
| 95 | $templatesGrouped = SettingsValueTemplateFixtures::getTemplatesGrouped(); |
||
| 96 | |||
| 97 | foreach ($templatesGrouped as $category => $settingsList) { |
||
| 98 | foreach ($settingsList as $tpl) { |
||
| 99 | if (!isset($tpl['variable']) || $tpl['variable'] !== $targetVariable) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | |||
| 103 | $jsonExample = json_encode( |
||
| 104 | $tpl['json_example'], |
||
| 105 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
||
| 106 | ); |
||
| 107 | |||
| 108 | // Check if template already exists |
||
| 109 | $templateId = $this->connection->fetchOne( |
||
| 110 | 'SELECT id FROM settings_value_template WHERE variable = ?', |
||
| 111 | [$targetVariable] |
||
| 112 | ); |
||
| 113 | |||
| 114 | if ($templateId) { |
||
| 115 | $this->connection->executeStatement( |
||
| 116 | 'UPDATE settings_value_template SET json_example = ?, updated_at = NOW() WHERE id = ?', |
||
| 117 | [$jsonExample, $templateId] |
||
| 118 | ); |
||
| 119 | } else { |
||
| 120 | $this->connection->executeStatement( |
||
| 121 | 'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at) |
||
| 122 | VALUES (?, ?, NOW(), NOW())', |
||
| 123 | [$targetVariable, $jsonExample] |
||
| 124 | ); |
||
| 125 | $templateId = $this->connection->lastInsertId(); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($templateId) { |
||
| 129 | $updatedRows = $this->connection->executeStatement( |
||
| 130 | 'UPDATE settings |
||
| 131 | SET value_template_id = ? |
||
| 132 | WHERE variable = ? AND subkey IS NULL AND access_url = 1', |
||
| 133 | [$templateId, $targetVariable] |
||
| 134 | ); |
||
| 135 | $this->write("Linked settings.value_template_id for '{$targetVariable}' (updated rows: {$updatedRows})"); |
||
| 136 | } |
||
| 137 | |||
| 138 | // We found and processed the target; break both loops |
||
| 139 | break 2; |
||
| 140 | } |
||
| 185 |