| Conditions | 6 |
| Paths | 20 |
| Total Lines | 78 |
| Code Lines | 47 |
| 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 |
||
| 183 | private function dumpServiceProviderHelper(string $className): string |
||
| 184 | { |
||
| 185 | $slashPos = strrpos($className, '\\'); |
||
| 186 | if ($slashPos !== false) { |
||
| 187 | $namespace = 'namespace '.substr($className, 0, $slashPos).";\n"; |
||
| 188 | $shortClassName = substr($className, $slashPos+1); |
||
| 189 | } else { |
||
| 190 | $namespace = null; |
||
| 191 | $shortClassName = $className; |
||
| 192 | } |
||
| 193 | |||
| 194 | $factoriesArrayCode = []; |
||
| 195 | $factories = []; |
||
| 196 | $factoryCount = 0; |
||
| 197 | foreach ($this->getFactoryDefinitions() as $definition) { |
||
| 198 | if ($definition->isPsrFactory()) { |
||
| 199 | $factoriesArrayCode[] = ' '.var_export($definition->getName(), true). |
||
| 200 | ' => ['.var_export($definition->getReflectionMethod()->getDeclaringClass()->getName(), true). |
||
| 201 | ', '.var_export($definition->getReflectionMethod()->getName(), true)."],\n"; |
||
| 202 | } else { |
||
| 203 | $factoryCount++; |
||
| 204 | $localFactoryName = 'factory'.$factoryCount; |
||
| 205 | $factoriesArrayCode[] = ' '.var_export($definition->getName(), true). |
||
| 206 | ' => [self::class, '.var_export($localFactoryName, true)."],\n"; |
||
| 207 | $factories[] = $definition->buildFactoryCode($localFactoryName); |
||
| 208 | } |
||
| 209 | foreach ($definition->getAliases() as $alias) { |
||
| 210 | $factoriesArrayCode[] = ' '.var_export($alias, true). |
||
| 211 | ' => new Alias('.var_export($definition->getName(), true)."),\n"; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $factoriesArrayStr = implode("\n", $factoriesArrayCode); |
||
| 216 | $factoriesStr = implode("\n", $factories); |
||
| 217 | |||
| 218 | $extensionsArrayCode = []; |
||
| 219 | $extensions = []; |
||
| 220 | $extensionCount = 0; |
||
| 221 | foreach ($this->getExtensionDefinitions() as $definition) { |
||
| 222 | $extensionCount++; |
||
| 223 | $localExtensionName = 'extension'.$extensionCount; |
||
| 224 | $extensionsArrayCode[] = ' '.var_export($definition->getName(), true). |
||
| 225 | ' => [self::class, '.var_export($localExtensionName, true)."],\n"; |
||
| 226 | $extensions[] = $definition->buildExtensionCode($localExtensionName); |
||
| 227 | } |
||
| 228 | |||
| 229 | $extensionsArrayStr = implode("\n", $extensionsArrayCode); |
||
| 230 | $extensionsStr = implode("\n", $extensions); |
||
| 231 | |||
| 232 | $code = <<<EOF |
||
| 233 | <?php |
||
| 234 | $namespace |
||
| 235 | |||
| 236 | use Interop\Container\Factories\Alias; |
||
| 237 | use Psr\Container\ContainerInterface; |
||
| 238 | |||
| 239 | final class $shortClassName |
||
| 240 | { |
||
| 241 | public static function getFactories(): array |
||
| 242 | { |
||
| 243 | return [ |
||
| 244 | $factoriesArrayStr |
||
| 245 | ]; |
||
| 246 | } |
||
| 247 | |||
| 248 | public static function getExtensions(): array |
||
| 249 | { |
||
| 250 | return [ |
||
| 251 | $extensionsArrayStr |
||
| 252 | ]; |
||
| 253 | } |
||
| 254 | |||
| 255 | $factoriesStr |
||
| 256 | $extensionsStr |
||
| 257 | } |
||
| 258 | EOF; |
||
| 259 | |||
| 260 | return $code; |
||
| 261 | } |
||
| 263 |