| Conditions | 11 |
| Paths | 20 |
| Total Lines | 59 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 72 | protected static function generateInternal() |
||
| 73 | { |
||
| 74 | $values = []; |
||
| 75 | // Iterate through the globals in the Twig context |
||
| 76 | /* @noinspection PhpInternalEntityUsedInspection */ |
||
| 77 | $globals = Craft::$app->view->getTwig()->getGlobals(); |
||
| 78 | foreach ($globals as $key => $value) { |
||
| 79 | $type = gettype($value); |
||
| 80 | switch ($type) { |
||
| 81 | case 'object': |
||
| 82 | $values[$key] = 'new \\' . get_class($value) . '()'; |
||
| 83 | break; |
||
| 84 | |||
| 85 | case 'boolean': |
||
| 86 | $values[$key] = $value ? 'true' : 'false'; |
||
| 87 | break; |
||
| 88 | |||
| 89 | case 'integer': |
||
| 90 | case 'double': |
||
| 91 | $values[$key] = $value; |
||
| 92 | break; |
||
| 93 | |||
| 94 | case 'string': |
||
| 95 | $values[$key] = "'" . addslashes($value) . "'"; |
||
| 96 | break; |
||
| 97 | |||
| 98 | case 'array': |
||
| 99 | $values[$key] = '[]'; |
||
| 100 | break; |
||
| 101 | |||
| 102 | case 'NULL': |
||
| 103 | $values[$key] = 'null'; |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // Mix in element route variables, and override values that should be used for autocompletion |
||
| 109 | $values = array_merge( |
||
| 110 | $values, |
||
| 111 | static::elementRouteVariables(), |
||
| 112 | static::globalVariables(), |
||
| 113 | static::overrideValues() |
||
| 114 | ); |
||
| 115 | |||
| 116 | // Allow plugins to modify the values |
||
| 117 | $event = new DefineGeneratorValuesEvent([ |
||
| 118 | 'values' => $values, |
||
| 119 | ]); |
||
| 120 | Event::trigger(self::class, self::EVENT_BEFORE_GENERATE, $event); |
||
| 121 | $values = $event->values; |
||
| 122 | |||
| 123 | // Format the line output for each value |
||
| 124 | foreach ($values as $key => $value) { |
||
| 125 | $values[$key] = " '" . $key . "' => " . $value . ","; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Save the template with variable substitution |
||
| 129 | self::saveTemplate([ |
||
| 130 | '{{ globals }}' => implode(PHP_EOL, $values), |
||
| 131 | ]); |
||
| 197 |