| Conditions | 4 |
| Paths | 3 |
| Total Lines | 65 |
| 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 |
||
| 19 | public function up(Schema $schema): void |
||
| 20 | { |
||
| 21 | $settings = [ |
||
| 22 | [ |
||
| 23 | 'variable' => 'chamilo_latest_news', |
||
| 24 | 'selected_value' => 'true', |
||
| 25 | 'title' => 'Latest news', |
||
| 26 | 'comment' => 'Get the latest news from Chamilo, including security vulnerabilities and events, directly inside your administration panel. These pieces of news will be checked on the Chamilo news server every time you load the administration page and are only visible to administrators.', |
||
| 27 | 'category' => 'admin', |
||
| 28 | ], |
||
| 29 | [ |
||
| 30 | 'variable' => 'chamilo_support', |
||
| 31 | 'selected_value' => 'true', |
||
| 32 | 'title' => 'Chamilo support block', |
||
| 33 | 'comment' => 'Get pro tips and an easy way to contact official service providers for professional support, directly from the makers of Chamilo. This block appears on your administration page, is only visible by administrators, and refreshes every time you load the administration page.', |
||
| 34 | 'category' => 'admin', |
||
| 35 | ], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | foreach ($settings as $setting) { |
||
| 39 | // Check if the setting exists for access_url = 1 |
||
| 40 | $sqlCheck = sprintf( |
||
| 41 | "SELECT COUNT(*) as count |
||
| 42 | FROM settings |
||
| 43 | WHERE variable = '%s' |
||
| 44 | AND subkey IS NULL |
||
| 45 | AND access_url = 1", |
||
| 46 | addslashes($setting['variable']) |
||
| 47 | ); |
||
| 48 | |||
| 49 | $stmt = $this->connection->executeQuery($sqlCheck); |
||
| 50 | $result = $stmt->fetchAssociative(); |
||
| 51 | |||
| 52 | if ($result && (int)$result['count'] > 0) { |
||
| 53 | // UPDATE existing setting |
||
| 54 | $this->addSql(sprintf( |
||
| 55 | "UPDATE settings |
||
| 56 | SET selected_value = '%s', |
||
| 57 | title = '%s', |
||
| 58 | comment = '%s', |
||
| 59 | category = '%s' |
||
| 60 | WHERE variable = '%s' |
||
| 61 | AND subkey IS NULL |
||
| 62 | AND access_url = 1", |
||
| 63 | addslashes($setting['selected_value']), |
||
| 64 | addslashes($setting['title']), |
||
| 65 | addslashes($setting['comment']), |
||
| 66 | addslashes($setting['category']), |
||
| 67 | addslashes($setting['variable']) |
||
| 68 | )); |
||
| 69 | $this->write(sprintf("Updated setting: %s", $setting['variable'])); |
||
| 70 | } else { |
||
| 71 | // INSERT new setting |
||
| 72 | $this->addSql(sprintf( |
||
| 73 | "INSERT INTO settings |
||
| 74 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 75 | VALUES |
||
| 76 | ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
||
| 77 | addslashes($setting['variable']), |
||
| 78 | addslashes($setting['category']), |
||
| 79 | addslashes($setting['selected_value']), |
||
| 80 | addslashes($setting['title']), |
||
| 81 | addslashes($setting['comment']) |
||
| 82 | )); |
||
| 83 | $this->write(sprintf("Inserted setting: %s", $setting['variable'])); |
||
| 84 | } |
||
| 100 |