| Conditions | 8 |
| Paths | 28 |
| Total Lines | 75 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 23 | public function up(Schema $schema): void |
||
| 24 | { |
||
| 25 | $targetVariable = 'search_prefilter_prefix'; |
||
| 26 | |||
| 27 | $templates = SettingsValueTemplateFixtures::getTemplatesGrouped(); |
||
| 28 | |||
| 29 | $foundTemplate = null; |
||
| 30 | |||
| 31 | foreach ($templates as $category => $settings) { |
||
| 32 | foreach ($settings as $setting) { |
||
| 33 | if (($setting['variable'] ?? null) === $targetVariable) { |
||
| 34 | $foundTemplate = $setting; |
||
| 35 | break 2; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | if (!$foundTemplate) { |
||
| 41 | $this->write("No template found in SettingsValueTemplateFixtures for variable '{$targetVariable}'. Skipping."); |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | $jsonExample = json_encode( |
||
| 46 | $foundTemplate['json_example'], |
||
| 47 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
||
| 48 | ); |
||
| 49 | |||
| 50 | // Check if template already exists |
||
| 51 | $templateId = $this->connection->fetchOne( |
||
| 52 | 'SELECT id FROM settings_value_template WHERE variable = ?', |
||
| 53 | [$targetVariable] |
||
| 54 | ); |
||
| 55 | |||
| 56 | if ($templateId) { |
||
| 57 | $this->connection->executeStatement( |
||
| 58 | 'UPDATE settings_value_template |
||
| 59 | SET json_example = ?, updated_at = NOW() |
||
| 60 | WHERE id = ?', |
||
| 61 | [$jsonExample, $templateId] |
||
| 62 | ); |
||
| 63 | $this->write("Updated existing template (ID {$templateId}) for variable '{$targetVariable}'."); |
||
| 64 | } else { |
||
| 65 | $this->connection->executeStatement( |
||
| 66 | 'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at) |
||
| 67 | VALUES (?, ?, NOW(), NOW())', |
||
| 68 | [$targetVariable, $jsonExample] |
||
| 69 | ); |
||
| 70 | |||
| 71 | $templateId = $this->connection->lastInsertId(); |
||
| 72 | $this->write("Inserted new template (ID {$templateId}) for variable '{$targetVariable}'."); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($templateId) { |
||
| 76 | // Link template to existing settings rows |
||
| 77 | $updatedRows = $this->connection->executeStatement( |
||
| 78 | 'UPDATE settings |
||
| 79 | SET value_template_id = ? |
||
| 80 | WHERE variable = ?', |
||
| 81 | [$templateId, $targetVariable] |
||
| 82 | ); |
||
| 83 | $this->write("Updated {$updatedRows} rows in settings for variable '{$targetVariable}'."); |
||
| 84 | |||
| 85 | // Optional: clean legacy boolean values (Yes/No) to avoid invalid JSON |
||
| 86 | $cleanedRows = $this->connection->executeStatement( |
||
| 87 | "UPDATE settings |
||
| 88 | SET selected_value = '' |
||
| 89 | WHERE variable = ? |
||
| 90 | AND (selected_value = 'true' OR selected_value = 'false')", |
||
| 91 | [$targetVariable] |
||
| 92 | ); |
||
| 93 | if ($cleanedRows > 0) { |
||
| 94 | $this->write("Cleaned legacy boolean values in {$cleanedRows} rows for variable '{$targetVariable}'."); |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | $this->write("ERROR: Template ID is null for variable '{$targetVariable}'."); |
||
| 98 | } |
||
| 140 |