| Conditions | 3 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 41 |
| 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 | // Decide whether we can join directly via resource_node.cid (preferred) |
||
| 22 | // or need a fallback via resource_link.c_id (when cid is not present). |
||
| 23 | $hasCid = false; |
||
| 24 | |||
| 25 | if ($schema->hasTable('resource_node')) { |
||
| 26 | $table = $schema->getTable('resource_node'); |
||
| 27 | $hasCid = $table->hasColumn('cid'); |
||
| 28 | } |
||
| 29 | |||
| 30 | if ($hasCid) { |
||
| 31 | // Preferred: "created in course" is stored on resource_node.cid |
||
| 32 | $sqlNode = <<<SQL |
||
| 33 | UPDATE resource_node rn |
||
| 34 | INNER JOIN course c ON c.id = rn.cid |
||
| 35 | INNER JOIN language l ON l.isocode = c.course_language |
||
| 36 | SET rn.language_id = l.id |
||
| 37 | WHERE rn.language_id IS NULL |
||
| 38 | AND rn.cid IS NOT NULL |
||
| 39 | AND c.course_language IS NOT NULL |
||
| 40 | AND c.course_language <> '' |
||
| 41 | SQL; |
||
| 42 | $this->addSql($sqlNode); |
||
| 43 | } else { |
||
| 44 | // Fallback: infer a course from existing links (best-effort). |
||
| 45 | // We pick MIN(c_id) per resource_node_id to have a deterministic choice. |
||
| 46 | $sqlNode = <<<SQL |
||
| 47 | UPDATE resource_node rn |
||
| 48 | INNER JOIN ( |
||
| 49 | SELECT rl.resource_node_id, MIN(rl.c_id) AS c_id |
||
| 50 | FROM resource_link rl |
||
| 51 | WHERE rl.c_id IS NOT NULL |
||
| 52 | GROUP BY rl.resource_node_id |
||
| 53 | ) x ON x.resource_node_id = rn.id |
||
| 54 | INNER JOIN course c ON c.id = x.c_id |
||
| 55 | INNER JOIN language l ON l.isocode = c.course_language |
||
| 56 | SET rn.language_id = l.id |
||
| 57 | WHERE rn.language_id IS NULL |
||
| 58 | AND c.course_language IS NOT NULL |
||
| 59 | AND c.course_language <> '' |
||
| 60 | SQL; |
||
| 61 | $this->addSql($sqlNode); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Copy node language to files when file language is not set yet. |
||
| 65 | $sqlFile = <<<SQL |
||
| 66 | UPDATE resource_file rf |
||
| 67 | INNER JOIN resource_node rn ON rn.id = rf.resource_node_id |
||
| 68 | SET rf.language_id = rn.language_id |
||
| 69 | WHERE rf.language_id IS NULL |
||
| 70 | AND rn.language_id IS NOT NULL |
||
| 71 | SQL; |
||
| 72 | |||
| 73 | $this->addSql($sqlFile); |
||
| 74 | } |
||
| 84 |