| Total Complexity | 43 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ServiceProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class ServiceProvider extends AbstractServiceProvider |
||
| 31 | { |
||
| 32 | protected static function getPackagePath(): string |
||
| 33 | { |
||
| 34 | return __DIR__ . '/../'; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function getFactories(): array |
||
| 38 | { |
||
| 39 | return [ |
||
| 40 | Adapter\SymfonyEventDispatcher::class => [ static::class, 'getSymfonyEventDispatcher' ], |
||
| 41 | Cache\CacheManager::class => [ static::class, 'getCacheManager' ], |
||
| 42 | Charset\CharsetConverter::class => [ static::class, 'getCharsetConverter' ], |
||
| 43 | Configuration\SiteConfiguration::class => [ static::class, 'getSiteConfiguration' ], |
||
| 44 | Console\CommandApplication::class => [ static::class, 'getConsoleCommandApplication' ], |
||
| 45 | Console\CommandRegistry::class => [ static::class, 'getConsoleCommandRegistry' ], |
||
| 46 | Context\Context::class => [ static::class, 'getContext' ], |
||
| 47 | Crypto\PasswordHashing\PasswordHashFactory::class => [ static::class, 'getPasswordHashFactory' ], |
||
| 48 | EventDispatcher\EventDispatcher::class => [ static::class, 'getEventDispatcher' ], |
||
| 49 | EventDispatcher\ListenerProvider::class => [ static::class, 'getEventListenerProvider' ], |
||
| 50 | Http\MiddlewareStackResolver::class => [ static::class, 'getMiddlewareStackResolver' ], |
||
| 51 | Http\RequestFactory::class => [ static::class, 'getRequestFactory' ], |
||
| 52 | Imaging\IconFactory::class => [ static::class, 'getIconFactory' ], |
||
| 53 | Imaging\IconRegistry::class => [ static::class, 'getIconRegistry' ], |
||
| 54 | Localization\LanguageServiceFactory::class => [ static::class, 'getLanguageServiceFactory' ], |
||
| 55 | Localization\LanguageStore::class => [ static::class, 'getLanguageStore' ], |
||
| 56 | Localization\Locales::class => [ static::class, 'getLocales' ], |
||
| 57 | Localization\LocalizationFactory::class => [ static::class, 'getLocalizationFactory' ], |
||
| 58 | Mail\TransportFactory::class => [ static::class, 'getMailTransportFactory' ], |
||
| 59 | Messaging\FlashMessageService::class => [ static::class, 'getFlashMessageService' ], |
||
| 60 | Package\FailsafePackageManager::class => [ static::class, 'getFailsafePackageManager' ], |
||
| 61 | Registry::class => [ static::class, 'getRegistry' ], |
||
| 62 | Resource\Index\FileIndexRepository::class => [ static::class, 'getFileIndexRepository' ], |
||
| 63 | Resource\Index\MetaDataRepository::class => [ static::class, 'getMetaDataRepository' ], |
||
| 64 | Resource\Driver\DriverRegistry::class => [ static::class, 'getDriverRegistry' ], |
||
| 65 | Resource\ProcessedFileRepository::class => [ static::class, 'getProcessedFileRepository' ], |
||
| 66 | Resource\ResourceFactory::class => [ static::class, 'getResourceFactory' ], |
||
| 67 | Resource\StorageRepository::class => [ static::class, 'getStorageRepository' ], |
||
| 68 | Service\DependencyOrderingService::class => [ static::class, 'getDependencyOrderingService' ], |
||
| 69 | Service\FlexFormService::class => [ static::class, 'getFlexFormService' ], |
||
| 70 | Service\OpcodeCacheService::class => [ static::class, 'getOpcodeCacheService' ], |
||
| 71 | TimeTracker\TimeTracker::class => [ static::class, 'getTimeTracker' ], |
||
| 72 | TypoScript\Parser\ConstantConfigurationParser::class => [ static::class, 'getTypoScriptConstantConfigurationParser' ], |
||
| 73 | TypoScript\TypoScriptService::class => [ static::class, 'getTypoScriptService' ], |
||
| 74 | 'middlewares' => [ static::class, 'getMiddlewares' ], |
||
| 75 | ]; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getExtensions(): array |
||
| 79 | { |
||
| 80 | return [ |
||
| 81 | EventDispatcherInterface::class => [ static::class, 'provideFallbackEventDispatcher' ], |
||
| 82 | EventDispatcher\ListenerProvider::class => [ static::class, 'extendEventListenerProvider' ], |
||
| 83 | ] + parent::getExtensions(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public static function getSymfonyEventDispatcher(ContainerInterface $container): SymfonyEventDispatcherInterface |
||
| 87 | { |
||
| 88 | return self::new($container, Adapter\SymfonyEventDispatcher::class, [ |
||
| 89 | $container->get(EventDispatcherInterface::class) |
||
| 90 | ]); |
||
| 91 | } |
||
| 92 | |||
| 93 | public static function getCacheManager(ContainerInterface $container): Cache\CacheManager |
||
| 94 | { |
||
| 95 | if (!$container->get('boot.state')->done) { |
||
| 96 | throw new \LogicException(Cache\CacheManager::class . ' can not be injected/instantiated during ext_localconf.php loading. Use lazy loading instead.', 1549446998); |
||
| 97 | } |
||
| 98 | |||
| 99 | $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] ?? []; |
||
| 100 | $disableCaching = $container->get('boot.state')->cacheDisabled; |
||
| 101 | $defaultCaches = [ |
||
| 102 | $container->get('cache.core'), |
||
| 103 | $container->get('cache.assets'), |
||
| 104 | $container->get('cache.di'), |
||
| 105 | ]; |
||
| 106 | |||
| 107 | $cacheManager = self::new($container, Cache\CacheManager::class, [$disableCaching]); |
||
| 108 | $cacheManager->setCacheConfigurations($cacheConfigurations); |
||
| 109 | $cacheConfigurations['di']['groups'] = ['system']; |
||
| 110 | foreach ($defaultCaches as $cache) { |
||
| 111 | $cacheManager->registerCache($cache, $cacheConfigurations[$cache->getIdentifier()]['groups'] ?? ['all']); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $cacheManager; |
||
| 115 | } |
||
| 116 | |||
| 117 | public static function getCharsetConverter(ContainerInterface $container): Charset\CharsetConverter |
||
| 118 | { |
||
| 119 | return self::new($container, Charset\CharsetConverter::class); |
||
| 120 | } |
||
| 121 | |||
| 122 | public static function getSiteConfiguration(ContainerInterface $container): Configuration\SiteConfiguration |
||
| 123 | { |
||
| 124 | return self::new($container, Configuration\SiteConfiguration::class, [Environment::getConfigPath() . '/sites']); |
||
| 125 | } |
||
| 126 | |||
| 127 | public static function getConsoleCommandApplication(ContainerInterface $container): Console\CommandApplication |
||
| 128 | { |
||
| 129 | return new Console\CommandApplication( |
||
| 130 | $container->get(Context\Context::class), |
||
| 131 | $container->get(Console\CommandRegistry::class) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | public static function getConsoleCommandRegistry(ContainerInterface $container): Console\CommandRegistry |
||
| 136 | { |
||
| 137 | return new Console\CommandRegistry($container); |
||
| 138 | } |
||
| 139 | |||
| 140 | public static function getEventDispatcher(ContainerInterface $container): EventDispatcher\EventDispatcher |
||
| 141 | { |
||
| 142 | return new EventDispatcher\EventDispatcher( |
||
| 143 | $container->get(EventDispatcher\ListenerProvider::class) |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | public static function getEventListenerProvider(ContainerInterface $container): EventDispatcher\ListenerProvider |
||
| 148 | { |
||
| 149 | return new EventDispatcher\ListenerProvider($container); |
||
| 150 | } |
||
| 151 | |||
| 152 | public static function extendEventListenerProvider( |
||
| 153 | ContainerInterface $container, |
||
| 154 | EventDispatcher\ListenerProvider $listenerProvider |
||
| 155 | ): EventDispatcher\ListenerProvider { |
||
| 156 | $listenerProvider->addListener( |
||
| 157 | Package\Event\PackagesMayHaveChangedEvent::class, |
||
| 158 | Package\PackageManager::class, |
||
| 159 | 'packagesMayHaveChanged' |
||
| 160 | ); |
||
| 161 | return $listenerProvider; |
||
| 162 | } |
||
| 163 | |||
| 164 | public static function getContext(ContainerInterface $container): Context\Context |
||
|
|
|||
| 165 | { |
||
| 166 | return new Context\Context(); |
||
| 167 | } |
||
| 168 | |||
| 169 | public static function getPasswordHashFactory(ContainerInterface $container): Crypto\PasswordHashing\PasswordHashFactory |
||
| 170 | { |
||
| 171 | return new Crypto\PasswordHashing\PasswordHashFactory(); |
||
| 172 | } |
||
| 173 | |||
| 174 | public static function getIconFactory(ContainerInterface $container): Imaging\IconFactory |
||
| 175 | { |
||
| 176 | return self::new($container, Imaging\IconFactory::class, [ |
||
| 177 | $container->get(EventDispatcherInterface::class), |
||
| 178 | $container->get(Imaging\IconRegistry::class) |
||
| 179 | ]); |
||
| 180 | } |
||
| 181 | |||
| 182 | public static function getIconRegistry(ContainerInterface $container): Imaging\IconRegistry |
||
| 183 | { |
||
| 184 | return self::new($container, Imaging\IconRegistry::class); |
||
| 185 | } |
||
| 186 | |||
| 187 | public static function getLanguageServiceFactory(ContainerInterface $container): Localization\LanguageServiceFactory |
||
| 188 | { |
||
| 189 | return self::new($container, Localization\LanguageServiceFactory::class, [ |
||
| 190 | $container->get(Localization\Locales::class), |
||
| 191 | $container->get(Localization\LocalizationFactory::class) |
||
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | public static function getLanguageStore(ContainerInterface $container): Localization\LanguageStore |
||
| 196 | { |
||
| 197 | return self::new($container, Localization\LanguageStore::class); |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function getLocales(ContainerInterface $container): Localization\Locales |
||
| 201 | { |
||
| 202 | return self::new($container, Localization\Locales::class); |
||
| 203 | } |
||
| 204 | |||
| 205 | public static function getLocalizationFactory(ContainerInterface $container): Localization\LocalizationFactory |
||
| 206 | { |
||
| 207 | return self::new($container, Localization\LocalizationFactory::class, [ |
||
| 208 | $container->get(Localization\LanguageStore::class), |
||
| 209 | $container->get(Cache\CacheManager::class) |
||
| 210 | ]); |
||
| 211 | } |
||
| 212 | |||
| 213 | public static function getMailTransportFactory(ContainerInterface $container): Mail\TransportFactory |
||
| 214 | { |
||
| 215 | return self::new($container, Mail\TransportFactory::class, [ |
||
| 216 | $container->get(Adapter\SymfonyEventDispatcher::class), |
||
| 217 | $container->get(Log\LogManager::class) |
||
| 218 | ]); |
||
| 219 | } |
||
| 220 | |||
| 221 | public static function getFlashMessageService(ContainerInterface $container): Messaging\FlashMessageService |
||
| 222 | { |
||
| 223 | return self::new($container, Messaging\FlashMessageService::class); |
||
| 224 | } |
||
| 225 | |||
| 226 | public static function getFailsafePackageManager(ContainerInterface $container): Package\FailsafePackageManager |
||
| 227 | { |
||
| 228 | $packageManager = $container->get(Package\PackageManager::class); |
||
| 229 | if ($packageManager instanceof Package\FailsafePackageManager) { |
||
| 230 | return $packageManager; |
||
| 231 | } |
||
| 232 | throw new \RuntimeException('FailsafePackageManager can only be instantiated in failsafe (maintenance tool) mode.', 1586861816); |
||
| 233 | } |
||
| 234 | |||
| 235 | public static function getRegistry(ContainerInterface $container): Registry |
||
| 236 | { |
||
| 237 | return self::new($container, Registry::class); |
||
| 238 | } |
||
| 239 | |||
| 240 | public static function getFileIndexRepository(ContainerInterface $container): Resource\Index\FileIndexRepository |
||
| 241 | { |
||
| 242 | return self::new($container, Resource\Index\FileIndexRepository::class, [ |
||
| 243 | $container->get(EventDispatcherInterface::class) |
||
| 244 | ]); |
||
| 245 | } |
||
| 246 | |||
| 247 | public static function getMetaDataRepository(ContainerInterface $container): Resource\Index\MetaDataRepository |
||
| 248 | { |
||
| 249 | return self::new($container, Resource\Index\MetaDataRepository::class, [ |
||
| 250 | $container->get(EventDispatcherInterface::class) |
||
| 251 | ]); |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function getDriverRegistry(ContainerInterface $container): Resource\Driver\DriverRegistry |
||
| 255 | { |
||
| 256 | return self::new($container, Resource\Driver\DriverRegistry::class); |
||
| 257 | } |
||
| 258 | |||
| 259 | public static function getProcessedFileRepository(ContainerInterface $container): Resource\ProcessedFileRepository |
||
| 260 | { |
||
| 261 | return self::new($container, Resource\ProcessedFileRepository::class); |
||
| 262 | } |
||
| 263 | |||
| 264 | public static function getResourceFactory(ContainerInterface $container): Resource\ResourceFactory |
||
| 265 | { |
||
| 266 | return self::new($container, Resource\ResourceFactory::class, [ |
||
| 267 | $container->get(EventDispatcherInterface::class) |
||
| 268 | ]); |
||
| 269 | } |
||
| 270 | |||
| 271 | public static function getStorageRepository(ContainerInterface $container): Resource\StorageRepository |
||
| 272 | { |
||
| 273 | return self::new($container, Resource\StorageRepository::class); |
||
| 274 | } |
||
| 275 | |||
| 276 | public static function getDependencyOrderingService(ContainerInterface $container): Service\DependencyOrderingService |
||
| 277 | { |
||
| 278 | return new Service\DependencyOrderingService(); |
||
| 279 | } |
||
| 280 | |||
| 281 | public static function getFlexFormService(ContainerInterface $container): Service\FlexFormService |
||
| 282 | { |
||
| 283 | return self::new($container, Service\FlexFormService::class); |
||
| 284 | } |
||
| 285 | |||
| 286 | public static function getOpcodeCacheService(ContainerInterface $container): Service\OpcodeCacheService |
||
| 287 | { |
||
| 288 | return self::new($container, Service\OpcodeCacheService::class); |
||
| 289 | } |
||
| 290 | |||
| 291 | public static function getTimeTracker(ContainerInterface $container): TimeTracker\TimeTracker |
||
| 294 | } |
||
| 295 | |||
| 296 | public static function getTypoScriptConstantConfigurationParser(ContainerInterface $container): TypoScript\Parser\ConstantConfigurationParser |
||
| 297 | { |
||
| 298 | return self::new($container, TypoScript\Parser\ConstantConfigurationParser::class); |
||
| 299 | } |
||
| 300 | |||
| 301 | public static function getTypoScriptService(ContainerInterface $container): TypoScript\TypoScriptService |
||
| 302 | { |
||
| 303 | return self::new($container, TypoScript\TypoScriptService::class); |
||
| 304 | } |
||
| 305 | |||
| 306 | public static function getRequestFactory(ContainerInterface $container): Http\RequestFactory |
||
| 307 | { |
||
| 308 | return new Http\RequestFactory(); |
||
| 309 | } |
||
| 310 | |||
| 311 | public static function getMiddlewareStackResolver(ContainerInterface $container): Http\MiddlewareStackResolver |
||
| 312 | { |
||
| 313 | return new Http\MiddlewareStackResolver( |
||
| 314 | $container, |
||
| 315 | $container->get(Service\DependencyOrderingService::class), |
||
| 316 | $container->get('cache.core') |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | |||
| 320 | public static function getMiddlewares(ContainerInterface $container): ArrayObject |
||
| 321 | { |
||
| 322 | return new ArrayObject(); |
||
| 323 | } |
||
| 324 | |||
| 325 | public static function provideFallbackEventDispatcher( |
||
| 332 | ); |
||
| 333 | } |
||
| 334 | } |
||
| 335 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.