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