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 |
||
| 33 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Holds the data instances. |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $datas; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initializes needed but yet missing service providers. |
||
| 44 | * |
||
| 45 | * @param Container $app |
||
| 46 | * the application container |
||
| 47 | */ |
||
| 48 | 77 | protected function initMissingServiceProviders(Container $app) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Initializes the available locales. |
||
| 70 | * |
||
| 71 | * @param Translator $translator |
||
| 72 | * the translator |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | * the available locales |
||
| 76 | */ |
||
| 77 | 76 | protected function initLocales(Translator $translator) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Initializes the children of the data entries. |
||
| 90 | */ |
||
| 91 | 76 | protected function initChildren() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 105 | * |
||
| 106 | * @param array $locales |
||
| 107 | * the available locales |
||
| 108 | * @param array $crud |
||
| 109 | * the CRUD entity map |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | * the map with localized entity labels |
||
| 113 | */ |
||
| 114 | 76 | protected function getLocaleLabels(array $locales, array $crud) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Configures the EntityDefinition according to the given |
||
| 127 | * CRUD entity map. |
||
| 128 | * |
||
| 129 | * @param EntityDefinition $definition |
||
| 130 | * the definition to configure |
||
| 131 | * @param array $crud |
||
| 132 | * the CRUD entity map |
||
| 133 | */ |
||
| 134 | 76 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Creates and setups an EntityDefinition instance. |
||
| 158 | * |
||
| 159 | * @param Translator $translator |
||
| 160 | * the Translator to use for some standard field labels |
||
| 161 | * @param EntityDefinitionFactoryInterface $entityDefinitionFactory |
||
| 162 | * the EntityDefinitionFactory to use |
||
| 163 | * @param array $locales |
||
| 164 | * the available locales |
||
| 165 | * @param array $crud |
||
| 166 | * the parsed YAML of a CRUD entity |
||
| 167 | * @param string $name |
||
| 168 | * the name of the entity |
||
| 169 | * |
||
| 170 | * @return EntityDefinition |
||
| 171 | * the EntityDefinition good to go |
||
| 172 | */ |
||
| 173 | 76 | protected function createDefinition(Translator $translator, EntityDefinitionFactoryInterface $entityDefinitionFactory, array $locales, array $crud, $name) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Validates the parsed entity definition. |
||
| 197 | * |
||
| 198 | * @param Container $app |
||
| 199 | * the application container |
||
| 200 | * @param array $entityDefinition |
||
| 201 | * the entity definition to validate |
||
| 202 | */ |
||
| 203 | 76 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Initializes the instance. |
||
| 216 | * |
||
| 217 | * @param string|null $crudFileCachingDirectory |
||
| 218 | * the writable directory to store the CRUD YAML file cache |
||
| 219 | * @param Container $app |
||
| 220 | * the application container |
||
| 221 | */ |
||
| 222 | 77 | public function init($crudFileCachingDirectory, Container $app) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 244 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 245 | * |
||
| 246 | * @param Container $app |
||
| 247 | * the Container instance of the Silex application |
||
| 248 | */ |
||
| 249 | 11 | public function register(Container $app) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Initializes the crud service right after boot. |
||
| 264 | * |
||
| 265 | * @param Application $app |
||
| 266 | * the Container instance of the Silex application |
||
| 267 | */ |
||
| 268 | 77 | public function boot(Application $app) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Getter for the AbstractData instances. |
||
| 277 | * |
||
| 278 | * @param string $name |
||
| 279 | * the entity name of the desired Data instance |
||
| 280 | * |
||
| 281 | * @return AbstractData |
||
| 282 | * the AbstractData instance or null on invalid name |
||
| 283 | */ |
||
| 284 | 70 | public function getData($name) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Getter for all available entity names. |
||
| 294 | * |
||
| 295 | * @return string[] |
||
| 296 | * a list of all available entity names |
||
| 297 | */ |
||
| 298 | 7 | public function getEntities() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Getter for the entities for the navigation bar. |
||
| 305 | * |
||
| 306 | * @return string[] |
||
| 307 | * a list of all available entity names with their group |
||
| 308 | */ |
||
| 309 | 10 | public function getEntitiesNavBar() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Determines the Twig template to use for the given parameters depending on |
||
| 325 | * the existance of certain keys in the Container $app in this order: |
||
| 326 | * |
||
| 327 | * crud.$section.$action.$entity |
||
| 328 | * crud.$section.$action |
||
| 329 | * crud.$section |
||
| 330 | * |
||
| 331 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 332 | * |
||
| 333 | * @param Container $app |
||
| 334 | * the Silex application |
||
| 335 | * @param string $section |
||
| 336 | * the section of the template, either "layout" or "template" |
||
| 337 | * @param string $action |
||
| 338 | * the current calling action like "create" or "show" |
||
| 339 | * @param string $entity |
||
| 340 | * the current calling entity |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | * the best fitting template |
||
| 344 | */ |
||
| 345 | 11 | public function getTemplate(Container $app, $section, $action, $entity) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Sets the locale to be used. |
||
| 367 | * |
||
| 368 | * @param string $locale |
||
| 369 | * the locale to be used. |
||
| 370 | */ |
||
| 371 | 10 | public function setLocale($locale) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the available locales. |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | * the available locales |
||
| 383 | */ |
||
| 384 | 77 | public function getLocales() |
|
| 402 | |||
| 403 | } |
||
| 404 |