| Conditions | 3 |
| Paths | 4 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 12 |
| 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 |
||
| 23 | public function safeUp() |
||
| 24 | { |
||
| 25 | // By default, remove siteId value if it matches the primary site (to mirror what native relations is doing) |
||
| 26 | $primarySiteId = Craft::$app->getSites()->getPrimarySite()->id; |
||
| 27 | $siteSelect = '(case when e.siteId = '.$primarySiteId.' then null else e.siteId end) sourceSiteId'; |
||
| 28 | $joinOn = 'e.fieldId=e.fieldId AND e.sourceId=r.sourceId AND e.targetId=r.targetId'; |
||
| 29 | |||
| 30 | // Handle multi-site differently. Match explicitly. |
||
| 31 | if (Craft::$app->getIsMultiSite()) { |
||
| 32 | $siteSelect = 'e.siteId'; |
||
| 33 | $joinOn .= ' AND e.siteId=r.sourceSiteId'; |
||
| 34 | } |
||
| 35 | |||
| 36 | $query = (new Query()) |
||
| 37 | ->from(['{{%elementlist}} e']) |
||
| 38 | ->leftJoin( |
||
| 39 | Association::tableName() . ' r', |
||
| 40 | $joinOn |
||
| 41 | ) |
||
| 42 | ->select([ |
||
| 43 | 'e.fieldId', |
||
| 44 | 'e.sourceId', |
||
| 45 | 'e.targetId', |
||
| 46 | 'e.sortOrder', |
||
| 47 | 'e.dateCreated', |
||
| 48 | 'e.dateUpdated', |
||
| 49 | 'e.uid', |
||
| 50 | $siteSelect |
||
| 51 | ]) |
||
| 52 | ->andWhere([ |
||
| 53 | 'r.id' => null |
||
| 54 | ]); |
||
| 55 | |||
| 56 | foreach ($query->batch(1000) as $batch) { |
||
| 57 | Craft::$app->getDb()->createCommand()->batchInsert( |
||
| 58 | Association::tableName(), |
||
| 59 | [ |
||
| 60 | 'fieldId', |
||
| 61 | 'sourceId', |
||
| 62 | 'targetId', |
||
| 63 | 'sortOrder', |
||
| 64 | 'dateCreated', |
||
| 65 | 'dateUpdated', |
||
| 66 | 'uid', |
||
| 67 | 'sourceSiteId' |
||
| 68 | ], |
||
| 69 | $batch, |
||
| 70 | false |
||
| 71 | )->execute(); |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->dropTableIfExists('elementlist'); |
||
| 75 | |||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 87 |