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