Conditions | 11 |
Paths | 20 |
Total Lines | 50 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
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 |
||
49 | public static function regenerate() |
||
50 | { |
||
51 | $values = []; |
||
52 | // Route variables are not merged in until the template is rendered, so do it here manually |
||
53 | /* @noinspection PhpInternalEntityUsedInspection */ |
||
54 | $globals = array_merge( |
||
55 | Craft::$app->view->getTwig()->getGlobals(), |
||
56 | Craft::$app->controller->actionParams['variables'] ?? [] |
||
57 | ); |
||
58 | foreach ($globals as $key => $value) { |
||
59 | $type = gettype($value); |
||
60 | switch ($type) { |
||
61 | case 'object': |
||
62 | $values[$key] = 'new \\' . get_class($value) . '()'; |
||
63 | break; |
||
64 | |||
65 | case 'boolean': |
||
66 | $values[$key] = $value ? 'true' : 'false'; |
||
67 | break; |
||
68 | |||
69 | case 'integer': |
||
70 | case 'double': |
||
71 | $values[$key] = $value; |
||
72 | break; |
||
73 | |||
74 | case 'string': |
||
75 | $values[$key] = "'" . addslashes($value) . "'"; |
||
76 | break; |
||
77 | |||
78 | case 'array': |
||
79 | $values[$key] = '[]'; |
||
80 | break; |
||
81 | |||
82 | case 'NULL': |
||
83 | $values[$key] = 'null'; |
||
84 | break; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // Override values that should be used for autocompletion |
||
89 | static::overrideValues($values); |
||
90 | |||
91 | // Format the line output for each value |
||
92 | foreach ($values as $key => $value) { |
||
93 | $values[$key] = " '" . $key . "' => " . $value . ","; |
||
94 | } |
||
95 | |||
96 | // Save the template with variable substitution |
||
97 | self::saveTemplate([ |
||
98 | '{{ globals }}' => implode(PHP_EOL, $values), |
||
99 | ]); |
||
122 |