| Conditions | 3 |
| Paths | 2 |
| Total Lines | 55 |
| 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 |
||
| 19 | public function up(Schema $schema): void |
||
| 20 | { |
||
| 21 | $setting = [ |
||
| 22 | 'variable' => 'hosting_limit_users_per_course', |
||
| 23 | 'selected_value' => '0', |
||
| 24 | 'title' => 'Global limit of users per course', |
||
| 25 | 'comment' => 'Defines a global maximum number of users (teachers included) allowed to be subscribed to any single course in the platform. Set this value to 0 to disable the limit. This helps avoid courses being overloaded in open portals.', |
||
| 26 | 'category' => 'platform', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | // Check if the setting exists for access_url = 1 |
||
| 30 | $sqlCheck = sprintf( |
||
| 31 | "SELECT COUNT(*) as count |
||
| 32 | FROM settings |
||
| 33 | WHERE variable = '%s' |
||
| 34 | AND subkey IS NULL |
||
| 35 | AND access_url = 1", |
||
| 36 | addslashes($setting['variable']) |
||
| 37 | ); |
||
| 38 | |||
| 39 | $stmt = $this->connection->executeQuery($sqlCheck); |
||
| 40 | $result = $stmt->fetchAssociative(); |
||
| 41 | |||
| 42 | if ($result && (int)$result['count'] > 0) { |
||
| 43 | // UPDATE existing setting |
||
| 44 | $this->addSql(sprintf( |
||
| 45 | "UPDATE settings |
||
| 46 | SET selected_value = '%s', |
||
| 47 | title = '%s', |
||
| 48 | comment = '%s', |
||
| 49 | category = '%s' |
||
| 50 | WHERE variable = '%s' |
||
| 51 | AND subkey IS NULL |
||
| 52 | AND access_url = 1", |
||
| 53 | addslashes($setting['selected_value']), |
||
| 54 | addslashes($setting['title']), |
||
| 55 | addslashes($setting['comment']), |
||
| 56 | addslashes($setting['category']), |
||
| 57 | addslashes($setting['variable']) |
||
| 58 | )); |
||
| 59 | $this->write(sprintf("Updated setting: %s", $setting['variable'])); |
||
| 60 | } else { |
||
| 61 | // INSERT new setting |
||
| 62 | $this->addSql(sprintf( |
||
| 63 | "INSERT INTO settings |
||
| 64 | (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
||
| 65 | VALUES |
||
| 66 | ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
||
| 67 | addslashes($setting['variable']), |
||
| 68 | addslashes($setting['category']), |
||
| 69 | addslashes($setting['selected_value']), |
||
| 70 | addslashes($setting['title']), |
||
| 71 | addslashes($setting['comment']) |
||
| 72 | )); |
||
| 73 | $this->write(sprintf("Inserted setting: %s", $setting['variable'])); |
||
| 74 | } |
||
| 89 |