Conditions | 9 |
Paths | 19 |
Total Lines | 53 |
Code Lines | 31 |
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 |
||
29 | public function actionSaveFlags(): \yii\web\Response |
||
30 | { |
||
31 | $this->requirePostRequest(); |
||
32 | $this->requireAcceptsJson(); |
||
33 | |||
34 | if (!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) { |
||
35 | throw new ForbiddenHttpException('Administrative changes are disallowed in this environment.'); |
||
36 | } |
||
37 | |||
38 | $params = Craft::$app->getRequest()->getBodyParams(); |
||
39 | $cacheFlags = $params['cacheflags'] ?? null; |
||
40 | |||
41 | $error = null; |
||
42 | |||
43 | foreach ($cacheFlags as $source => $flags) { |
||
44 | |||
45 | $sourceArray = explode(':', $source); |
||
46 | $sourceColumn = $sourceArray[0] ?? null; |
||
47 | $sourceId = $sourceArray[1] ?? null; |
||
48 | |||
49 | if (!$sourceColumn || !$sourceId) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | $flags = preg_replace('/\s+/', '', $flags); |
||
54 | |||
55 | try { |
||
56 | if (!$flags) { |
||
57 | CacheFlag::getInstance()->cacheFlag->deleteFlagsBySource($sourceColumn, $sourceId); |
||
58 | continue; |
||
59 | } |
||
60 | CacheFlag::getInstance()->cacheFlag->saveFlags($flags, $sourceColumn, $sourceId); |
||
61 | } catch (\Throwable $e) { |
||
62 | $error = $e->getMessage(); |
||
63 | } |
||
64 | |||
65 | if ($error) { |
||
66 | break; |
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | if ($error) { |
||
72 | return $this->asJson([ |
||
73 | 'success' => false, |
||
74 | 'message' => $error, |
||
75 | ]); |
||
76 | } |
||
77 | |||
78 | return $this->asJson([ |
||
79 | 'success' => true, |
||
80 | 'message' => Craft::t('cache-flag', 'Cache flags saved'), |
||
81 | 'flags' => CacheFlag::getInstance()->cacheFlag->getAllFlags(), |
||
82 | ]); |
||
154 |