| Conditions | 2 |
| Paths | 2 |
| Total Lines | 61 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 | $canonical = [ |
||
| 22 | 'ANONYMOUS', |
||
| 23 | 'INVITEE', |
||
| 24 | 'STUDENT', |
||
| 25 | 'TEACHER', |
||
| 26 | 'ADMIN', |
||
| 27 | 'GLOBAL_ADMIN', |
||
| 28 | 'HR', |
||
| 29 | 'QUESTION_MANAGER', |
||
| 30 | 'SESSION_MANAGER', |
||
| 31 | 'STUDENT_BOSS', |
||
| 32 | ]; |
||
| 33 | $inList = "'" . implode("','", array_map('addslashes', $canonical)) . "'"; |
||
| 34 | |||
| 35 | $this->addSql(" |
||
| 36 | INSERT INTO role (code, constant_value, title, description, system_role, created_at) |
||
| 37 | SELECT 'ANONYMOUS', 0, 'Anonymous', 'Unauthenticated users', 1, NOW() |
||
| 38 | FROM DUAL |
||
| 39 | WHERE NOT EXISTS (SELECT 1 FROM role WHERE code = 'ANONYMOUS') |
||
| 40 | "); |
||
| 41 | $this->write('Ensured ANONYMOUS role exists.'); |
||
| 42 | |||
| 43 | $this->addSql(" |
||
| 44 | DELETE prr FROM permission_rel_role prr |
||
| 45 | INNER JOIN role r ON r.id = prr.role_id |
||
| 46 | WHERE r.code = 'SUPER_ADMIN' |
||
| 47 | "); |
||
| 48 | $this->addSql("DELETE FROM role WHERE code = 'SUPER_ADMIN'"); |
||
| 49 | $this->write('Removed SUPER_ADMIN and related PRR rows.'); |
||
| 50 | |||
| 51 | $this->addSql(" |
||
| 52 | DELETE prr FROM permission_rel_role prr |
||
| 53 | INNER JOIN role r ON r.id = prr.role_id |
||
| 54 | WHERE r.code NOT IN ($inList) |
||
| 55 | "); |
||
| 56 | $this->addSql("DELETE FROM role WHERE code NOT IN ($inList)"); |
||
| 57 | $this->write('Removed non-canonical roles and related PRR rows.'); |
||
| 58 | |||
| 59 | $defaults = [ |
||
| 60 | 'INVITEE' => [1, 'Invitee', 'Invited users', 1], |
||
| 61 | 'STUDENT' => [2, 'Student', 'Students of courses or sessions', 1], |
||
| 62 | 'TEACHER' => [3, 'Teacher', 'Teachers of courses or sessions', 1], |
||
| 63 | 'ADMIN' => [4, 'Administrator', 'Platform administrators', 1], |
||
| 64 | 'GLOBAL_ADMIN' => [6, 'Global Administrator', 'Global admin users', 1], |
||
| 65 | 'HR' => [7, 'HR Manager', 'Human resources managers', 0], |
||
| 66 | 'QUESTION_MANAGER' => [8, 'Question Bank Manager', 'Manages the question bank across courses', 0], |
||
| 67 | 'SESSION_MANAGER' => [9, 'Session Manager', 'Manages sessions and session content', 0], |
||
| 68 | 'STUDENT_BOSS' => [10, 'Student Boss', 'Manages groups of students', 0], |
||
| 69 | ]; |
||
| 70 | |||
| 71 | foreach ($defaults as $code => [$const, $title, $desc, $sys]) { |
||
| 72 | $this->addSql(" |
||
| 73 | INSERT INTO role (code, constant_value, title, description, system_role, created_at) |
||
| 74 | SELECT '$code', $const, '".addslashes($title)."', '".addslashes($desc)."', $sys, NOW() |
||
| 75 | FROM DUAL |
||
| 76 | WHERE NOT EXISTS (SELECT 1 FROM role WHERE code = '$code') |
||
| 77 | "); |
||
| 78 | } |
||
| 79 | $this->write('Ensured all canonical roles exist.'); |
||
| 80 | } |
||
| 93 |