| Conditions | 4 |
| Paths | 8 |
| Total Lines | 95 |
| Code Lines | 36 |
| 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 | // 1. Create table role |
||
| 23 | $this->addSql(" |
||
| 24 | CREATE TABLE role ( |
||
| 25 | id INT UNSIGNED AUTO_INCREMENT NOT NULL, |
||
| 26 | code VARCHAR(50) NOT NULL, |
||
| 27 | constant_value INT NOT NULL, |
||
| 28 | title VARCHAR(255) NOT NULL, |
||
| 29 | description LONGTEXT DEFAULT NULL, |
||
| 30 | system_role TINYINT(1) NOT NULL, |
||
| 31 | created_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime)', |
||
| 32 | created_by INT UNSIGNED DEFAULT NULL, |
||
| 33 | updated_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime)', |
||
| 34 | updated_by INT UNSIGNED DEFAULT NULL, |
||
| 35 | UNIQUE INDEX UNIQ_57698A6A77153098 (code), |
||
| 36 | PRIMARY KEY(id) |
||
| 37 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC |
||
| 38 | "); |
||
| 39 | $this->write("Created table role."); |
||
| 40 | |||
| 41 | // 2. Populate role table |
||
| 42 | $roles = PermissionFixtures::getRoles(); |
||
| 43 | $values = []; |
||
| 44 | $id = 1; |
||
| 45 | |||
| 46 | foreach ($roles as $roleName => $shortCode) { |
||
| 47 | $code = substr($roleName, 5); |
||
| 48 | $constantValue = $this->getRoleConstantValue($code); |
||
| 49 | $title = $this->getRoleTitle($code); |
||
| 50 | $description = $this->getRoleDescription($code); |
||
| 51 | |||
| 52 | $values[] = sprintf( |
||
| 53 | "(%d, '%s', %d, '%s', '%s', %d, NOW())", |
||
| 54 | $id, |
||
| 55 | $code, |
||
| 56 | $constantValue, |
||
| 57 | addslashes($title), |
||
| 58 | addslashes($description), |
||
| 59 | 1 |
||
| 60 | ); |
||
| 61 | |||
| 62 | $id++; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (!empty($values)) { |
||
| 66 | $this->addSql(" |
||
| 67 | INSERT INTO role |
||
| 68 | (id, code, constant_value, title, description, system_role, created_at) |
||
| 69 | VALUES " . implode(", ", $values) |
||
| 70 | ); |
||
| 71 | $this->write("Inserted role data from fixtures."); |
||
| 72 | } |
||
| 73 | |||
| 74 | // 3. Add nullable role_id |
||
| 75 | $this->addSql(" |
||
| 76 | ALTER TABLE permission_rel_role |
||
| 77 | ADD role_id INT UNSIGNED DEFAULT NULL |
||
| 78 | "); |
||
| 79 | $this->write("Added role_id column as nullable."); |
||
| 80 | |||
| 81 | // 4. Migrate existing data |
||
| 82 | $this->addSql(" |
||
| 83 | UPDATE permission_rel_role prr |
||
| 84 | JOIN role r ON r.code = SUBSTRING(prr.role_code, 6) |
||
| 85 | SET prr.role_id = r.id |
||
| 86 | "); |
||
| 87 | $this->write("Migrated data from role_code to role_id."); |
||
| 88 | |||
| 89 | // 5. Drop old role_code |
||
| 90 | $schemaManager = $this->connection->createSchemaManager(); |
||
| 91 | $columns = $schemaManager->listTableColumns('permission_rel_role'); |
||
| 92 | if (isset($columns['role_code'])) { |
||
| 93 | $this->addSql(" |
||
| 94 | ALTER TABLE permission_rel_role DROP COLUMN role_code |
||
| 95 | "); |
||
| 96 | $this->write("Dropped column role_code."); |
||
| 97 | } |
||
| 98 | |||
| 99 | // 6. Set role_id NOT NULL |
||
| 100 | $this->addSql(" |
||
| 101 | ALTER TABLE permission_rel_role |
||
| 102 | MODIFY role_id INT UNSIGNED NOT NULL |
||
| 103 | "); |
||
| 104 | $this->write("Set role_id column to NOT NULL."); |
||
| 105 | |||
| 106 | // 7. Add FK and index |
||
| 107 | $this->addSql(" |
||
| 108 | ALTER TABLE permission_rel_role |
||
| 109 | ADD CONSTRAINT FK_14B93D3DD60322AC FOREIGN KEY (role_id) REFERENCES role (id) |
||
| 110 | "); |
||
| 111 | $this->addSql(" |
||
| 112 | CREATE INDEX IDX_14B93D3DD60322AC ON permission_rel_role (role_id) |
||
| 113 | "); |
||
| 114 | $this->write("Added FK constraint and index for role_id."); |
||
| 115 | } |
||
| 196 |