| Conditions | 12 |
| Paths | 576 |
| Total Lines | 67 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 15 | public function up(Schema $schema): void |
||
| 16 | { |
||
| 17 | // Update iso |
||
| 18 | $sql = 'UPDATE course SET course_language = (SELECT isocode FROM language WHERE english_name = course_language);'; |
||
| 19 | $this->addSql($sql); |
||
| 20 | |||
| 21 | $table = $schema->getTable('course'); |
||
| 22 | if (false === $table->hasColumn('resource_node_id')) { |
||
| 23 | $this->addSql('ALTER TABLE course ADD COLUMN resource_node_id INT DEFAULT NULL;'); |
||
| 24 | $this->addSql( |
||
| 25 | 'ALTER TABLE course ADD CONSTRAINT FK_169E6FB91BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;' |
||
| 26 | ); |
||
| 27 | $this->addSql('CREATE UNIQUE INDEX UNIQ_169E6FB91BAD783F ON course (resource_node_id);'); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (false === $schema->hasTable('course_rel_category')) { |
||
| 31 | $this->addSql('CREATE TABLE course_rel_category (course_id INT NOT NULL, course_category_id INT NOT NULL, INDEX IDX_8EB34CC5591CC992 (course_id), INDEX IDX_8EB34CC56628AD36 (course_category_id), PRIMARY KEY(course_id, course_category_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC'); |
||
| 32 | $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_8EB34CC5591CC992 FOREIGN KEY (course_id) REFERENCES course (id) ON DELETE CASCADE'); |
||
| 33 | $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_8EB34CC56628AD36 FOREIGN KEY (course_category_id) REFERENCES course_category (id) ON DELETE CASCADE'); |
||
| 34 | |||
| 35 | $courseTable = $schema->getTable('course'); |
||
| 36 | if ($courseTable->hasForeignKey('FK_169E6FB912469DE2')) { |
||
| 37 | $this->addSql('ALTER TABLE course DROP FOREIGN KEY FK_169E6FB912469DE2'); |
||
| 38 | } |
||
| 39 | if ($courseTable->hasForeignKey('IDX_169E6FB912469DE2')) { |
||
| 40 | $this->addSql('DROP INDEX IDX_169E6FB912469DE2 ON course'); |
||
| 41 | } |
||
| 42 | if ($courseTable->hasColumn('category_id')) { |
||
| 43 | $this->addSql('ALTER TABLE course DROP category_id'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | $connection = $this->getEntityManager()->getConnection(); |
||
| 48 | $sql = 'SELECT * FROM course_category'; |
||
| 49 | $result = $connection->executeQuery($sql); |
||
| 50 | $all = $result->fetchAllAssociative(); |
||
| 51 | |||
| 52 | $categories = array_column($all, 'parent_id', 'id'); |
||
| 53 | $categoryCodeList = array_column($all, 'id', 'code'); |
||
| 54 | |||
| 55 | foreach ($categories as $categoryId => $parentId) { |
||
| 56 | if (empty($parentId)) { |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | $newParentId = $categoryCodeList[$parentId]; |
||
| 60 | if (!empty($newParentId)) { |
||
| 61 | $this->addSql("UPDATE course_category SET parent_id = $newParentId WHERE id = $categoryId"); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->addSql('ALTER TABLE course_category CHANGE parent_id parent_id INT DEFAULT NULL;'); |
||
| 66 | |||
| 67 | $table = $schema->getTable('course_category'); |
||
| 68 | if (false === $table->hasForeignKey('FK_AFF87497727ACA70')) { |
||
| 69 | $this->addSql( |
||
| 70 | 'ALTER TABLE course_category ADD CONSTRAINT FK_AFF87497727ACA70 FOREIGN KEY (parent_id) REFERENCES course_category (id);' |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | if (!$table->hasColumn('image')) { |
||
| 74 | $this->addSql('ALTER TABLE course_category ADD image VARCHAR(255) DEFAULT NULL'); |
||
| 75 | } |
||
| 76 | if (!$table->hasColumn('description')) { |
||
| 77 | $this->addSql('ALTER TABLE course_category ADD description LONGTEXT DEFAULT NULL'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->addSql( |
||
| 81 | 'ALTER TABLE course_category CHANGE auth_course_child auth_course_child VARCHAR(40) DEFAULT NULL' |
||
| 82 | ); |
||
| 89 |