| Conditions | 13 |
| Paths | 77 |
| Total Lines | 56 |
| Code Lines | 34 |
| 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 |
||
| 20 | public function up(Schema $schema): void |
||
| 21 | { |
||
| 22 | $conn = $this->connection; |
||
| 23 | $vars = ['show_tabs', 'show_tabs_per_role']; |
||
| 24 | |||
| 25 | foreach ($vars as $var) { |
||
| 26 | $rows = $conn->fetchAllAssociative( |
||
| 27 | 'SELECT id, access_url, category, selected_value |
||
| 28 | FROM settings |
||
| 29 | WHERE variable = ?', |
||
| 30 | [$var] |
||
| 31 | ); |
||
| 32 | |||
| 33 | if (empty($rows)) { |
||
| 34 | $this->dbg("No '{$var}' entries found, skipping."); |
||
| 35 | continue; |
||
| 36 | } |
||
| 37 | |||
| 38 | $byUrl = []; |
||
| 39 | foreach ($rows as $r) { |
||
| 40 | $urlKey = $r['access_url'] === null ? 'NULL' : (string)$r['access_url']; |
||
| 41 | $byUrl[$urlKey][] = $r; |
||
| 42 | } |
||
| 43 | |||
| 44 | foreach ($byUrl as $urlKey => $group) { |
||
| 45 | $main = null; |
||
| 46 | foreach ($group as $r) { |
||
| 47 | if ($r['category'] === 'display') { |
||
| 48 | $main = $r; |
||
| 49 | break; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | if (!$main) { |
||
| 54 | $main = $group[0]; |
||
| 55 | $conn->update('settings', ['category' => 'display'], ['id' => $main['id']]); |
||
| 56 | $this->dbg("Moved '{$var}' id={$main['id']} to category 'display'."); |
||
| 57 | } |
||
| 58 | |||
| 59 | foreach ($group as $r) { |
||
| 60 | if ($r['id'] === $main['id']) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | if (empty($main['selected_value']) && !empty($r['selected_value'])) { |
||
| 65 | $conn->update('settings', ['selected_value' => $r['selected_value']], ['id' => $main['id']]); |
||
| 66 | $this->dbg("Updated '{$var}' id={$main['id']} with non-empty value from id={$r['id']}."); |
||
| 67 | } |
||
| 68 | |||
| 69 | $conn->delete('settings', ['id' => $r['id']]); |
||
| 70 | $this->dbg("Deleted duplicate '{$var}' id={$r['id']} (category={$r['category']})."); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->dbg('--- [END] show_tabs consolidation done ---'); |
||
| 76 | } |
||
| 90 |