| Conditions | 5 |
| Paths | 10 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 127 | private function dumpServiceProviderHelper(string $className): string |
||
| 128 | { |
||
| 129 | $slashPos = strrpos($className, '\\'); |
||
| 130 | if ($slashPos !== false) { |
||
| 131 | $namespace = 'namespace '.substr($className, 0, $slashPos).";\n"; |
||
| 132 | $shortClassName = substr($className, $slashPos+1); |
||
| 133 | } else { |
||
| 134 | $namespace = null; |
||
| 135 | $shortClassName = $className; |
||
| 136 | } |
||
| 137 | |||
| 138 | $factoriesArrayCode = []; |
||
| 139 | $factories = []; |
||
| 140 | $factoryCount = 0; |
||
| 141 | foreach ($this->getFactoryDefinitions() as $definition) { |
||
| 142 | if ($definition->isPsrFactory()) { |
||
| 143 | $factoriesArrayCode[] = ' '.var_export($definition->getName(), true).' => ['.var_export($definition->getReflectionMethod()->getDeclaringClass()->getName(), true).', '.var_export($definition->getReflectionMethod()->getName(), true)."],\n"; |
||
| 144 | } else { |
||
| 145 | $factoryCount++; |
||
| 146 | $localFactoryName = 'factory'.$factoryCount; |
||
| 147 | $factoriesArrayCode[] = ' '.var_export($definition->getName(), true).' => [self::class, '.var_export($localFactoryName, true)."],\n"; |
||
| 148 | $factories[] = $definition->buildFactoryCode($localFactoryName); |
||
| 149 | } |
||
| 150 | foreach ($definition->getAliases() as $alias) { |
||
| 151 | $factoriesArrayCode[] = ' '.var_export($alias, true).' => new Alias('.var_export($definition->getName(), true)."),\n"; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $factoriesArrayStr = implode("\n", $factoriesArrayCode); |
||
| 156 | $factoriesStr = implode("\n", $factories); |
||
| 157 | |||
| 158 | $code = <<<EOF |
||
| 159 | <?php |
||
| 160 | $namespace |
||
| 161 | |||
| 162 | use Interop\Container\Factories\Alias; |
||
| 163 | use Psr\Container\ContainerInterface; |
||
| 164 | |||
| 165 | final class $shortClassName |
||
| 166 | { |
||
| 167 | public static function getFactories(): array |
||
| 168 | { |
||
| 169 | return [ |
||
| 170 | $factoriesArrayStr |
||
| 171 | ]; |
||
| 172 | } |
||
| 173 | |||
| 174 | $factoriesStr |
||
| 175 | } |
||
| 176 | EOF; |
||
| 177 | |||
| 178 | return $code; |
||
| 179 | } |
||
| 204 |