| Conditions | 11 |
| Paths | 10 |
| Total Lines | 73 |
| Code Lines | 57 |
| 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 |
||
| 27 | public function onProjectConfigChange(ConfigEvent $event) |
||
| 28 | { |
||
| 29 | |||
| 30 | $uid = $event->tokenMatches[0]; |
||
| 31 | |||
| 32 | $id = (new Query()) |
||
| 33 | ->select(['id']) |
||
| 34 | ->from(Flags::tableName()) |
||
| 35 | ->where(['uid' => $uid]) |
||
| 36 | ->scalar(); |
||
| 37 | |||
| 38 | $isNew = empty($id); |
||
| 39 | |||
| 40 | if ($isNew) { |
||
| 41 | |||
| 42 | $source = \explode(':', $event->newValue['source']); |
||
| 43 | $sourceKey = $source[0] ?? null; |
||
| 44 | $sourceValue = $source[1] ?? null; |
||
| 45 | |||
| 46 | if (!$sourceKey || !$sourceValue) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | switch ($sourceKey) { |
||
| 51 | case 'section': |
||
| 52 | $column = 'sectionId'; |
||
| 53 | $value = (int)Db::idByUid(Table::SECTIONS, $sourceValue); |
||
| 54 | break; |
||
| 55 | case 'categoryGroup': |
||
| 56 | $column = 'categoryGroupId'; |
||
| 57 | $value = (int)Db::idByUid(Table::CATEGORYGROUPS, $sourceValue); |
||
| 58 | break; |
||
| 59 | case 'tagGroup': |
||
| 60 | $column = 'tagGroupId'; |
||
| 61 | $value = (int)Db::idByUid(Table::TAGGROUPS, $sourceValue); |
||
| 62 | break; |
||
| 63 | case 'userGroup': |
||
| 64 | $column = 'userGroupId'; |
||
| 65 | $value = (int)Db::idByUid(Table::USERGROUPS, $sourceValue); |
||
| 66 | break; |
||
| 67 | case 'volume': |
||
| 68 | $column = 'volumeId'; |
||
| 69 | $value = (int)Db::idByUid(Table::VOLUMES, $sourceValue); |
||
| 70 | break; |
||
| 71 | case 'globalSet': |
||
| 72 | $column = 'globalSetId'; |
||
| 73 | $value = (int)Db::idByUid(Table::GLOBALSETS, $sourceValue); |
||
| 74 | break; |
||
| 75 | case 'elementType': |
||
| 76 | $column = 'elementType'; |
||
| 77 | $value = $sourceValue; |
||
| 78 | break; |
||
| 79 | default: |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $flags = $event->newValue['flags']; |
||
| 84 | |||
| 85 | Craft::$app->db->createCommand() |
||
| 86 | ->insert(Flags::tableName(), [ |
||
| 87 | 'flags' => $flags, |
||
| 88 | $column => $value, |
||
| 89 | 'uid' => $uid, |
||
| 90 | ]) |
||
| 91 | ->execute(); |
||
| 92 | |||
| 93 | } else { |
||
| 94 | |||
| 95 | Craft::$app->db->createCommand() |
||
| 96 | ->update(Flags::tableName(), [ |
||
| 97 | 'flags' => $event->newValue['flags'], |
||
| 98 | ], ['id' => $id]) |
||
| 99 | ->execute(); |
||
| 100 | } |
||
| 184 |