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