| Conditions | 16 |
| Paths | 8 |
| Total Lines | 90 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 75 | protected static function generateInternal() |
||
| 76 | { |
||
| 77 | $properties = []; |
||
| 78 | $methods = []; |
||
| 79 | /* @noinspection PhpInternalEntityUsedInspection */ |
||
| 80 | $globals = Craft::$app->view->getTwig()->getGlobals(); |
||
| 81 | /* @var CraftVariable $craftVariable */ |
||
| 82 | if (isset($globals['craft'])) { |
||
| 83 | $craftVariable = $globals['craft']; |
||
| 84 | // Handle the components |
||
| 85 | foreach ($craftVariable->getComponents() as $key => $value) { |
||
| 86 | try { |
||
| 87 | $properties[$key] = get_class($craftVariable->get($key)); |
||
| 88 | } catch (Throwable $e) { |
||
| 89 | // That's okay |
||
| 90 | } |
||
| 91 | } |
||
| 92 | // Handle the behaviors |
||
| 93 | foreach ($craftVariable->getBehaviors() as $behavior) { |
||
| 94 | try { |
||
| 95 | $reflect = new ReflectionClass($behavior); |
||
| 96 | // Properties |
||
| 97 | foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectProp) { |
||
| 98 | // Property name |
||
| 99 | $reflectPropName = $reflectProp->getName(); |
||
| 100 | // Ensure the property exists only for this class and not any parent class |
||
| 101 | if (property_exists(get_parent_class($behavior), $reflectPropName)) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | // Do it this way because getType() reflection method is >= PHP 7.4 |
||
| 105 | $reflectPropType = gettype($behavior->$reflectPropName); |
||
| 106 | switch ($reflectPropType) { |
||
| 107 | case 'object': |
||
| 108 | $properties[$reflectPropName] = get_class($behavior->$reflectPropName); |
||
| 109 | break; |
||
| 110 | default: |
||
| 111 | $properties[$reflectPropName] = $reflectPropType; |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | // Methods |
||
| 116 | foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectMethod) { |
||
| 117 | // Method name |
||
| 118 | $reflectMethodName = $reflectMethod->getName(); |
||
| 119 | // Ensure the method exists only for this class and not any parent class |
||
| 120 | if (method_exists(get_parent_class($behavior), $reflectMethodName)) { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | // Method return type |
||
| 124 | $methodReturn = ''; |
||
| 125 | $reflectMethodReturnType = $reflectMethod->getReturnType(); |
||
| 126 | if ($reflectMethodReturnType instanceof ReflectionNamedType) { |
||
| 127 | $methodReturn = ': ' . $reflectMethodReturnType->getName(); |
||
| 128 | } |
||
| 129 | // Method parameters |
||
| 130 | $methodParams = []; |
||
| 131 | foreach ($reflectMethod->getParameters() as $methodParam) { |
||
| 132 | $paramType = ''; |
||
| 133 | $methodParamType = $methodParam->getType(); |
||
| 134 | if ($methodParamType) { |
||
| 135 | $paramType = $methodParamType . ' '; |
||
| 136 | } |
||
| 137 | $methodParams[] = $paramType . '$' . $methodParam->getName(); |
||
| 138 | } |
||
| 139 | $methods[$reflectMethodName] = '(' . implode(', ', $methodParams) . ')' . $methodReturn; |
||
| 140 | } |
||
| 141 | } catch (\ReflectionException $e) { |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | // Allow plugins to modify the values |
||
| 147 | $event = new DefineGeneratorValuesEvent([ |
||
| 148 | 'values' => $properties, |
||
| 149 | ]); |
||
| 150 | Event::trigger(self::class, self::EVENT_BEFORE_GENERATE, $event); |
||
| 151 | $properties = $event->values; |
||
| 152 | |||
| 153 | // Format the line output for each property |
||
| 154 | foreach ($properties as $key => $value) { |
||
| 155 | $properties[$key] = ' * @property \\' . $value . ' $' . $key; |
||
| 156 | } |
||
| 157 | // Format the line output for each method |
||
| 158 | foreach ($methods as $key => $value) { |
||
| 159 | $methods[$key] = ' * @method ' . $key . $value; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Save the template with variable substitution |
||
| 163 | self::saveTemplate([ |
||
| 164 | '{{ properties }}' => implode(PHP_EOL, array_merge($properties, $methods)), |
||
| 165 | ]); |
||
| 168 |