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