Conditions | 13 |
Paths | 81 |
Total Lines | 75 |
Code Lines | 54 |
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 |
||
34 | public function down(Schema $schema): void |
||
35 | { |
||
36 | $conn = $this->connection; |
||
37 | |||
38 | // Fetch all plugin + url configs |
||
39 | $rows = $conn->fetchAllAssociative( |
||
40 | 'SELECT p.title AS plugin_title, |
||
41 | p.installed AS plugin_installed, |
||
42 | r.url_id AS access_url, |
||
43 | r.active AS rel_active, |
||
44 | r.configuration AS cfg |
||
45 | FROM plugin p |
||
46 | LEFT JOIN access_url_rel_plugin r ON r.plugin_id = p.id |
||
47 | ORDER BY p.title, r.url_id' |
||
48 | ); |
||
49 | |||
50 | $recreated = 0; |
||
51 | |||
52 | foreach ($rows as $row) { |
||
53 | $title = (string) $row['plugin_title']; |
||
54 | $installed = (int) ($row['plugin_installed'] ?? 0); |
||
55 | $accessUrl = $row['access_url'] !== null ? (int) $row['access_url'] : 1; // default URL=1 if missing |
||
56 | $cfgRaw = $row['cfg']; |
||
57 | |||
58 | // Restore 'status' row (legacy semantics) |
||
59 | $conn->insert('settings', [ |
||
60 | 'variable' => 'status', |
||
61 | 'subkey' => $title, |
||
62 | 'type' => 'setting', |
||
63 | 'category' => 'plugins', |
||
64 | 'selected_value' => $installed ? 'installed' : 'uninstalled', |
||
65 | 'title' => $title, |
||
66 | 'comment' => '', |
||
67 | 'access_url_changeable' => 1, |
||
68 | 'access_url_locked' => 0, |
||
69 | 'access_url' => $accessUrl, |
||
70 | ]); |
||
71 | $recreated++; |
||
72 | |||
73 | // Restore configuration rows from JSON |
||
74 | $cfg = []; |
||
75 | if (is_string($cfgRaw) && $cfgRaw !== '') { |
||
76 | $decoded = json_decode($cfgRaw, true); |
||
77 | if (is_array($decoded)) { |
||
78 | $cfg = $decoded; |
||
79 | } |
||
80 | } elseif (is_array($cfgRaw)) { |
||
81 | $cfg = $cfgRaw; |
||
82 | } |
||
83 | |||
84 | if (!empty($cfg)) { |
||
85 | foreach ($cfg as $key => $val) { |
||
86 | $variable = $title . '_' . (string) $key; |
||
87 | $selectedValue = |
||
88 | is_bool($val) ? ($val ? 'true' : 'false') : |
||
89 | (is_scalar($val) ? (string) $val : json_encode($val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); |
||
90 | |||
91 | $conn->insert('settings', [ |
||
92 | 'variable' => $variable, |
||
93 | 'subkey' => $title, |
||
94 | 'type' => 'setting', |
||
95 | 'category' => 'plugins', |
||
96 | 'selected_value' => $selectedValue, |
||
97 | 'title' => $title, |
||
98 | 'comment' => '', |
||
99 | 'access_url_changeable' => 1, |
||
100 | 'access_url_locked' => 0, |
||
101 | 'access_url' => $accessUrl, |
||
102 | ]); |
||
103 | $recreated++; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | $this->write("Down(): recreated {$recreated} legacy 'plugins' setting rows in `settings`. Non-setting rows were not restored."); |
||
109 | } |
||
111 |