| Conditions | 11 |
| Paths | 50 |
| Total Lines | 86 |
| Code Lines | 49 |
| 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 = 'score_grade_model'; |
||
| 26 | |||
| 27 | // Find the template definition from fixtures (single variable only). |
||
| 28 | $templatesGrouped = SettingsValueTemplateFixtures::getTemplatesGrouped(); |
||
| 29 | |||
| 30 | $template = null; |
||
| 31 | foreach ($templatesGrouped as $category => $list) { |
||
| 32 | foreach ($list as $tpl) { |
||
| 33 | if (!is_array($tpl)) { |
||
| 34 | continue; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (($tpl['variable'] ?? null) === $targetVariable) { |
||
| 38 | $template = $tpl; |
||
| 39 | break 2; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | if (null === $template) { |
||
| 45 | $this->write("Template not found in SettingsValueTemplateFixtures for variable '{$targetVariable}'. No changes applied."); |
||
| 46 | |||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $jsonExampleRaw = $template['json_example'] ?? null; |
||
| 51 | if (!is_array($jsonExampleRaw) && !is_string($jsonExampleRaw)) { |
||
| 52 | $this->write("Template '{$targetVariable}' has no usable json_example payload. No changes applied."); |
||
| 53 | |||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Store JSON example as a string in DB. |
||
| 58 | $jsonExample = is_string($jsonExampleRaw) |
||
| 59 | ? $jsonExampleRaw |
||
| 60 | : (string) json_encode($jsonExampleRaw, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
| 61 | |||
| 62 | // Upsert into settings_value_template (no deletes). |
||
| 63 | $templateId = $this->connection->fetchOne( |
||
| 64 | 'SELECT id FROM settings_value_template WHERE variable = ?', |
||
| 65 | [$targetVariable] |
||
| 66 | ); |
||
| 67 | |||
| 68 | if ($templateId) { |
||
| 69 | $updated = $this->connection->executeStatement( |
||
| 70 | 'UPDATE settings_value_template |
||
| 71 | SET json_example = ?, updated_at = CURRENT_TIMESTAMP |
||
| 72 | WHERE id = ?', |
||
| 73 | [$jsonExample, (int) $templateId] |
||
| 74 | ); |
||
| 75 | |||
| 76 | $this->write("Updated settings_value_template for '{$targetVariable}' (rows: {$updated})."); |
||
| 77 | } else { |
||
| 78 | $inserted = $this->connection->executeStatement( |
||
| 79 | 'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at) |
||
| 80 | VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)', |
||
| 81 | [$targetVariable, $jsonExample] |
||
| 82 | ); |
||
| 83 | |||
| 84 | $templateId = $this->connection->fetchOne( |
||
| 85 | 'SELECT id FROM settings_value_template WHERE variable = ?', |
||
| 86 | [$targetVariable] |
||
| 87 | ); |
||
| 88 | |||
| 89 | $this->write("Inserted settings_value_template for '{$targetVariable}' (rows: {$inserted}, id: ".((string) $templateId).').'); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!$templateId) { |
||
| 93 | $this->write("Unable to resolve template id for '{$targetVariable}'. Settings will not be linked."); |
||
| 94 | |||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Link settings rows to this template (no value changes, no deletes). |
||
| 99 | // Apply to all access URLs to avoid inconsistencies in multi-url setups. |
||
| 100 | $linkedRows = $this->connection->executeStatement( |
||
| 101 | 'UPDATE settings |
||
| 102 | SET value_template_id = ? |
||
| 103 | WHERE variable = ? |
||
| 104 | AND (subkey IS NULL OR subkey = \'\')', |
||
| 105 | [(int) $templateId, $targetVariable] |
||
| 106 | ); |
||
| 107 | |||
| 108 | $this->write("Linked settings.value_template_id for '{$targetVariable}' (rows: {$linkedRows})."); |
||
| 109 | } |
||
| 117 |