| Conditions | 11 |
| Paths | 20 |
| Total Lines | 52 |
| Code Lines | 32 |
| 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 |
||
| 65 | private static function generateInternal() |
||
| 66 | { |
||
| 67 | // Start from the cache values, so the auto-complete variables are additive for generation |
||
| 68 | $values = static::getCachedValues(); |
||
| 69 | // Route variables are not merged in until the template is rendered, so do it here manually |
||
| 70 | /* @noinspection PhpInternalEntityUsedInspection */ |
||
| 71 | $globals = array_merge( |
||
| 72 | Craft::$app->view->getTwig()->getGlobals(), |
||
| 73 | Craft::$app->controller->actionParams['variables'] ?? [] |
||
| 74 | ); |
||
| 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 | |||
| 105 | // Override values that should be used for autocompletion |
||
| 106 | static::overrideValues($values); |
||
| 107 | // Cache the values |
||
| 108 | static::setCachedValues($values); |
||
| 109 | // Format the line output for each value |
||
| 110 | foreach ($values as $key => $value) { |
||
| 111 | $values[$key] = " '" . $key . "' => " . $value . ","; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Save the template with variable substitution |
||
| 115 | self::saveTemplate([ |
||
| 116 | '{{ globals }}' => implode(PHP_EOL, $values), |
||
| 117 | ]); |
||
| 180 |