Conditions | 10 |
Paths | 3 |
Total Lines | 57 |
Code Lines | 28 |
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 |
||
17 | public function actionAdmin() |
||
18 | { |
||
19 | $settings = Setting::model()->findAll(); |
||
20 | $definitions = Setting::model()->getDefinitions(); |
||
21 | |||
22 | // Sort the settings according to their defined order |
||
23 | usort($settings, function($a, $b) use($definitions) |
||
24 | { |
||
25 | return $definitions[$a->name]['order'] > |
||
26 | $definitions[$b->name]['order']; |
||
27 | }); |
||
28 | |||
29 | if (isset($_POST['Setting'])) |
||
30 | { |
||
31 | $allValid = true; |
||
32 | |||
33 | // Validate all settings |
||
34 | foreach ($settings as $setting) |
||
35 | { |
||
36 | $value = $_POST['Setting'][$setting->name]; |
||
37 | |||
38 | // Update the application language and reset any user |
||
39 | // language preferences if the default is changed |
||
40 | if ($setting->name === 'language' && $setting->value !== $value) |
||
41 | { |
||
42 | User::model()->updateAll(array( |
||
43 | 'language'=>null)); |
||
44 | |||
45 | Yii::app()->languageManager->setCurrent($value); |
||
46 | } |
||
47 | |||
48 | $setting->{$setting->name} = $value; |
||
49 | $setting->value = $value; |
||
50 | |||
51 | if (!$setting->validate(array($setting->name))) |
||
52 | $allValid = false; |
||
53 | |||
54 | // Flush the API call cache whenever its option is disabled |
||
55 | if ($setting->name == 'cacheApiCalls' && !$value) |
||
56 | Yii::app()->apiCallCache->flush(); |
||
57 | } |
||
58 | |||
59 | // Only if all are valid we save them and refresh the page |
||
60 | if ($allValid) |
||
61 | { |
||
62 | foreach ($settings as $setting) |
||
63 | $setting->save(false); |
||
64 | |||
65 | $this->log('"%s" updated the application settings', Yii::app()->user->name); |
||
66 | Yii::app()->user->setFlash('success', Yii::t('Settings', 'Settings updated successfully')); |
||
67 | $this->refresh(); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | $this->render('admin', array( |
||
72 | 'settings'=>$settings, |
||
73 | 'definitions'=>$definitions, |
||
74 | )); |
||
103 | } |