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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | ||
| 31 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { | ||
| 32 | |||
| 33 | /** | ||
| 34 |      * Holds the {@see AbstractData} instances. | ||
| 35 | */ | ||
| 36 | protected $datas; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Holds whether we manage the i18n. | ||
| 40 | */ | ||
| 41 | protected $manageI18n; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Reads and returns the contents of the given Yaml file. If | ||
| 45 | * it goes wrong, it throws an exception. | ||
| 46 | * | ||
| 47 | * @param string $fileName | ||
| 48 | * the file to read | ||
| 49 | * | ||
| 50 | * @return array | ||
| 51 | * the file contents | ||
| 52 | * | ||
| 53 | * @throws \RuntimeException | ||
| 54 | * thrown if the file could not be read or parsed | ||
| 55 | */ | ||
| 56 |     protected function readYaml($fileName) { | ||
| 57 |         try { | ||
| 58 | $fileContent = file_get_contents($fileName); | ||
| 59 | $parsedYaml = Yaml::parse($fileContent); | ||
| 60 |             if (!is_array($parsedYaml)) { | ||
| 61 | $parsedYaml = []; | ||
| 62 | } | ||
| 63 | return $parsedYaml; | ||
| 64 |         } catch (\Exception $e) { | ||
| 65 |             throw new \RuntimeException('Could not open CRUD file '.$fileName, $e->getCode(), $e); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Initializes needed but yet missing service providers. | ||
| 71 | * | ||
| 72 | * @param Container $app | ||
| 73 | * the application container | ||
| 74 | */ | ||
| 75 |     protected function initMissingServiceProviders(Container $app) { | ||
| 76 | |||
| 77 |         if (!$app->offsetExists('translator')) { | ||
| 78 | $app->register(new LocaleServiceProvider()); | ||
| 79 | $app->register(new TranslationServiceProvider(), [ | ||
| 80 | 'locale_fallbacks' => ['en'], | ||
| 81 | ]); | ||
| 82 | } | ||
| 83 | |||
| 84 |         if (!$app->offsetExists('session')) { | ||
| 85 | $app->register(new SessionServiceProvider()); | ||
| 86 | } | ||
| 87 | |||
| 88 |         if (!$app->offsetExists('twig')) { | ||
| 89 | $app->register(new TwigServiceProvider()); | ||
| 90 | } | ||
| 91 | $app['twig.loader.filesystem']->addPath(__DIR__.'/../views/', 'crud'); | ||
| 92 | } | ||
| 93 | |||
| 94 | /** | ||
| 95 | * Initializes the available locales. | ||
| 96 | * | ||
| 97 | * @param Container $app | ||
| 98 | * the application container | ||
| 99 | * | ||
| 100 | * @return array | ||
| 101 | * the available locales | ||
| 102 | */ | ||
| 103 |     protected function initLocales(Container $app) { | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Initializes the children of the data entries. | ||
| 115 | */ | ||
| 116 |     protected function initChildren() { | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Gets a map with localized entity labels from the CRUD YML. | ||
| 129 | * | ||
| 130 | * @param array $locales | ||
| 131 | * the available locales | ||
| 132 | * @param array $crud | ||
| 133 | * the CRUD entity map | ||
| 134 | * | ||
| 135 | * @return array | ||
| 136 | * the map with localized entity labels | ||
| 137 | */ | ||
| 138 |     protected function getLocaleLabels($locales, $crud) { | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Configures the EntityDefinition according to the given | ||
| 150 | * CRUD entity map. | ||
| 151 | * | ||
| 152 | * @param EntityDefinition $definition | ||
| 153 | * the definition to configure | ||
| 154 | * @param array $crud | ||
| 155 | * the CRUD entity map | ||
| 156 | */ | ||
| 157 |     protected function configureDefinition(EntityDefinition $definition, array $crud) { | ||
| 174 | |||
| 175 | /** | ||
| 176 | * Creates and setups an EntityDefinition instance. | ||
| 177 | * | ||
| 178 | * @param Container $app | ||
| 179 | * the application container | ||
| 180 | * @param array $locales | ||
| 181 | * the available locales | ||
| 182 | * @param array $crud | ||
| 183 | * the parsed YAML of a CRUD entity | ||
| 184 | * @param string $name | ||
| 185 | * the name of the entity | ||
| 186 | * | ||
| 187 | * @return EntityDefinition | ||
| 188 | * the EntityDefinition good to go | ||
| 189 | */ | ||
| 190 |     protected function createDefinition(Container $app, array $locales, array $crud, $name) { | ||
| 212 | |||
| 213 | /** | ||
| 214 | * Validates the parsed entity definition. | ||
| 215 | * | ||
| 216 | * @param Container $app | ||
| 217 | * the application container | ||
| 218 | * @param array $entityDefinition | ||
| 219 | * the entity definition to validate | ||
| 220 | */ | ||
| 221 |     protected function validateEntityDefinition(Container $app, array $entityDefinition) { | ||
| 230 | |||
| 231 | /** | ||
| 232 | * Initializes the instance. | ||
| 233 | * | ||
| 234 | * @param DataFactoryInterface $dataFactory | ||
| 235 | * the factory to create the concrete AbstractData instances | ||
| 236 | * @param string $crudFile | ||
| 237 | * the CRUD YAML file to parse | ||
| 238 | * @param FileProcessorInterface $fileProcessor | ||
| 239 | * the file processor used for file fields | ||
| 240 | * @param boolean $manageI18n | ||
| 241 | * holds whether we manage the i18n | ||
| 242 | * @param Container $app | ||
| 243 | * the application container | ||
| 244 | */ | ||
| 245 |     public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Container $app) { | ||
| 267 | |||
| 268 | /** | ||
| 269 | * Implements ServiceProviderInterface::register() registering $app['crud']. | ||
| 270 | * $app['crud'] contains an instance of the ServiceProvider afterwards. | ||
| 271 | * | ||
| 272 | * @param Container $app | ||
| 273 | * the Container instance of the Silex application | ||
| 274 | */ | ||
| 275 |     public function register(Container $app) { | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Initializes the crud service right after boot. | ||
| 284 | * | ||
| 285 | * @param Application $app | ||
| 286 | * the Container instance of the Silex application | ||
| 287 | */ | ||
| 288 |     public function boot(Application $app) { | ||
| 293 | |||
| 294 | /** | ||
| 295 |      * Getter for the {@see AbstractData} instances. | ||
| 296 | * | ||
| 297 | * @param string $name | ||
| 298 | * the entity name of the desired Data instance | ||
| 299 | * | ||
| 300 | * @return AbstractData | ||
| 301 | * the AbstractData instance or null on invalid name | ||
| 302 | */ | ||
| 303 |     public function getData($name) { | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Getter for all available entity names. | ||
| 312 | * | ||
| 313 | * @return string[] | ||
| 314 | * a list of all available entity names | ||
| 315 | */ | ||
| 316 |     public function getEntities() { | ||
| 319 | |||
| 320 | /** | ||
| 321 | * Determines the Twig template to use for the given parameters depending on | ||
| 322 | * the existance of certain keys in the Container $app in this order: | ||
| 323 | * | ||
| 324 | * crud.$section.$action.$entity | ||
| 325 | * crud.$section.$action | ||
| 326 | * crud.$section | ||
| 327 | * | ||
| 328 | * If nothing exists, this string is returned: "@crud/<action>.twig" | ||
| 329 | * | ||
| 330 | * @param Container $app | ||
| 331 | * the Silex application | ||
| 332 | * @param string $section | ||
| 333 | * the section of the template, either "layout" or "template" | ||
| 334 | * @param string $action | ||
| 335 | * the current calling action like "create" or "show" | ||
| 336 | * @param string $entity | ||
| 337 | * the current calling entity | ||
| 338 | * | ||
| 339 | * @return string | ||
| 340 | * the best fitting template | ||
| 341 | */ | ||
| 342 |     public function getTemplate(Container $app, $section, $action, $entity) { | ||
| 360 | |||
| 361 | /** | ||
| 362 | * Gets whether CRUDlex manages the i18n system. | ||
| 363 | * | ||
| 364 | * @return boolean | ||
| 365 | * true if CRUDlex manages the i18n system | ||
| 366 | */ | ||
| 367 |     public function isManagingI18n() { | ||
| 370 | |||
| 371 | /** | ||
| 372 | * Sets the locale to be used. | ||
| 373 | * | ||
| 374 | * @param string $locale | ||
| 375 | * the locale to be used. | ||
| 376 | */ | ||
| 377 |     public function setLocale($locale) { | ||
| 382 | |||
| 383 | /** | ||
| 384 | * Gets the available locales. | ||
| 385 | * | ||
| 386 | * @return array | ||
| 387 | * the available locales | ||
| 388 | */ | ||
| 389 |     public function getLocales() { | ||
| 406 | |||
| 407 | } | ||
| 408 | 
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.