Conditions | 11 |
Paths | 20 |
Total Lines | 48 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
69 | private static function generateInternal() |
||
70 | { |
||
71 | $values = []; |
||
72 | // Iterate through the globals in the Twig context |
||
73 | /* @noinspection PhpInternalEntityUsedInspection */ |
||
74 | $globals = Craft::$app->view->getTwig()->getGlobals(); |
||
75 | foreach ($globals as $key => $value) { |
||
76 | $type = gettype($value); |
||
77 | switch ($type) { |
||
78 | case 'object': |
||
79 | $values[$key] = 'new \\' . get_class($value) . '()'; |
||
80 | break; |
||
81 | |||
82 | case 'boolean': |
||
83 | $values[$key] = $value ? 'true' : 'false'; |
||
84 | break; |
||
85 | |||
86 | case 'integer': |
||
87 | case 'double': |
||
88 | $values[$key] = $value; |
||
89 | break; |
||
90 | |||
91 | case 'string': |
||
92 | $values[$key] = "'" . addslashes($value) . "'"; |
||
93 | break; |
||
94 | |||
95 | case 'array': |
||
96 | $values[$key] = '[]'; |
||
97 | break; |
||
98 | |||
99 | case 'NULL': |
||
100 | $values[$key] = 'null'; |
||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | // Mix in element route variables, and override values that should be used for autocompletion |
||
105 | $values = array_merge( |
||
106 | $values, |
||
107 | static::elementRouteVariables(), |
||
108 | static::overrideValues() |
||
109 | ); |
||
110 | // Format the line output for each value |
||
111 | foreach ($values as $key => $value) { |
||
112 | $values[$key] = " '" . $key . "' => " . $value . ","; |
||
113 | } |
||
114 | // Save the template with variable substitution |
||
115 | self::saveTemplate([ |
||
116 | '{{ globals }}' => implode(PHP_EOL, $values), |
||
117 | ]); |
||
157 |