Conditions | 18 |
Paths | 105 |
Total Lines | 64 |
Code Lines | 43 |
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 |
||
22 | public function up(Schema $schema): void |
||
23 | { |
||
24 | $conn = $this->connection; |
||
25 | |||
26 | $rows = $conn->fetchAllAssociative('SELECT id, active, configuration FROM access_url_rel_plugin'); |
||
27 | |||
28 | foreach ($rows as $row) { |
||
29 | $id = (int) $row['id']; |
||
30 | $active = isset($row['active']) ? (int) $row['active'] : 0; |
||
31 | $cfgRaw = $row['configuration']; |
||
32 | $cfg = []; |
||
33 | |||
34 | // configuration may be a JSON string (common) or an array (driver dependent) |
||
35 | if (is_string($cfgRaw) && $cfgRaw !== '') { |
||
36 | $decoded = json_decode($cfgRaw, true); |
||
37 | if (is_array($decoded)) { |
||
38 | $cfg = $decoded; |
||
39 | } |
||
40 | } elseif (is_array($cfgRaw)) { |
||
41 | $cfg = $cfgRaw; |
||
42 | } |
||
43 | |||
44 | if (!array_key_exists('tool_enable', $cfg)) { |
||
45 | continue; // nothing to migrate for this row |
||
46 | } |
||
47 | |||
48 | $val = $cfg['tool_enable']; |
||
49 | $newActive = null; |
||
50 | |||
51 | // Normalize accepted legacy values |
||
52 | if ($val === true || $val === 1 || $val === '1') { |
||
53 | $newActive = 1; |
||
54 | } elseif ($val === false || $val === 0 || $val === '0') { |
||
55 | $newActive = 0; |
||
56 | } elseif (is_string($val)) { |
||
57 | $v = strtolower(trim($val)); |
||
58 | if (in_array($v, ['true', 'on', 'yes', 'y'], true)) { |
||
59 | $newActive = 1; |
||
60 | } elseif (in_array($v, ['false', 'off', 'no', 'n'], true)) { |
||
61 | $newActive = 0; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | // Remove the legacy key from configuration |
||
66 | unset($cfg['tool_enable']); |
||
67 | $payload = json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
||
68 | |||
69 | // Update row: set active if we could infer it; otherwise just clean configuration |
||
70 | if ($newActive !== null) { |
||
71 | $conn->update( |
||
72 | 'access_url_rel_plugin', |
||
73 | ['active' => $newActive, 'configuration' => $payload], |
||
74 | ['id' => $id], |
||
75 | ['configuration' => \PDO::PARAM_STR] |
||
76 | ); |
||
77 | $this->write("Row {$id}: moved tool_enable => active={$newActive}; configuration cleaned."); |
||
78 | } else { |
||
79 | $conn->update( |
||
80 | 'access_url_rel_plugin', |
||
81 | ['configuration' => $payload], |
||
82 | ['id' => $id], |
||
83 | ['configuration' => \PDO::PARAM_STR] |
||
84 | ); |
||
85 | $this->write("Row {$id}: tool_enable removed from configuration; active left unchanged={$active}."); |
||
86 | } |
||
130 |