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 | ||
| 28 | class ServiceProvider implements ServiceProviderInterface { | ||
| 29 | |||
| 30 | /** | ||
| 31 |      * Holds the {@see AbstractData} instances. | ||
| 32 | */ | ||
| 33 | protected $datas; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Holds whether we manage the i18n. | ||
| 37 | */ | ||
| 38 | protected $manageI18n; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Reads and returns the contents of the given Yaml file. If | ||
| 42 | * it goes wrong, it throws an exception. | ||
| 43 | * | ||
| 44 | * @param string $fileName | ||
| 45 | * the file to read | ||
| 46 | * | ||
| 47 | * @return array | ||
| 48 | * the file contents | ||
| 49 | * | ||
| 50 | * @throws \RuntimeException | ||
| 51 | * thrown if the file could not be read or parsed | ||
| 52 | */ | ||
| 53 |     protected function readYaml($fileName) { | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Initializes needed but yet missing service providers. | ||
| 68 | * | ||
| 69 | * @param Container $app | ||
| 70 | * the application container | ||
| 71 | */ | ||
| 72 |     protected function initMissingServiceProviders(Container $app) { | ||
| 90 | |||
| 91 | /** | ||
| 92 | * Initializes the available locales. | ||
| 93 | * | ||
| 94 | * @param Container $app | ||
| 95 | * the application container | ||
| 96 | * | ||
| 97 | * @return array | ||
| 98 | * the available locales | ||
| 99 | */ | ||
| 100 |     protected function initLocales(Container $app) { | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Initializes the children of the data entries. | ||
| 112 | */ | ||
| 113 |     protected function initChildren() { | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Gets a map with localized entity labels from the CRUD YML. | ||
| 126 | * | ||
| 127 | * @param array $locales | ||
| 128 | * the available locales | ||
| 129 | * @param array $crud | ||
| 130 | * the CRUD entity map | ||
| 131 | * | ||
| 132 | * @return array | ||
| 133 | * the map with localized entity labels | ||
| 134 | */ | ||
| 135 |     protected function getLocaleLabels($locales, $crud) { | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Configures the EntityDefinition according to the given | ||
| 147 | * CRUD entity map. | ||
| 148 | * | ||
| 149 | * @param EntityDefinition $definition | ||
| 150 | * the definition to configure | ||
| 151 | * @param array $crud | ||
| 152 | * the CRUD entity map | ||
| 153 | */ | ||
| 154 |     protected function configureDefinition(EntityDefinition $definition, array $crud) { | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Creates and setups an EntityDefinition instance. | ||
| 174 | * | ||
| 175 | * @param Container $app | ||
| 176 | * the application container | ||
| 177 | * @param array $locales | ||
| 178 | * the available locales | ||
| 179 | * @param array $crud | ||
| 180 | * the parsed YAML of a CRUD entity | ||
| 181 | * @param string $name | ||
| 182 | * the name of the entity | ||
| 183 | * | ||
| 184 | * @return EntityDefinition | ||
| 185 | * the EntityDefinition good to go | ||
| 186 | */ | ||
| 187 |     protected function createDefinition(Container $app, array $locales, array $crud, $name) { | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Validates the parsed entity definition. | ||
| 212 | * | ||
| 213 | * @param Container $app | ||
| 214 | * the application container | ||
| 215 | * @param array $entityDefinition | ||
| 216 | * the entity definition to validate | ||
| 217 | */ | ||
| 218 |     protected function validateEntityDefinition(Container $app, array $entityDefinition) { | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Initializes the instance. | ||
| 230 | * | ||
| 231 | * @param DataFactoryInterface $dataFactory | ||
| 232 | * the factory to create the concrete AbstractData instances | ||
| 233 | * @param string $crudFile | ||
| 234 | * the CRUD YAML file to parse | ||
| 235 | * @param FileProcessorInterface $fileProcessor | ||
| 236 | * the file processor used for file fields | ||
| 237 | * @param boolean $manageI18n | ||
| 238 | * holds whether we manage the i18n | ||
| 239 | * @param Container $app | ||
| 240 | * the application container | ||
| 241 | */ | ||
| 242 |     public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Container $app) { | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Implements ServiceProviderInterface::register() registering $app['crud']. | ||
| 267 | * $app['crud'] contains an instance of the ServiceProvider afterwards. | ||
| 268 | * | ||
| 269 | * @param Container $app | ||
| 270 | * the Container instance of the Silex application | ||
| 271 | */ | ||
| 272 |     public function register(Container $app) { | ||
| 281 | |||
| 282 | /** | ||
| 283 |      * Getter for the {@see AbstractData} instances. | ||
| 284 | * | ||
| 285 | * @param string $name | ||
| 286 | * the entity name of the desired Data instance | ||
| 287 | * | ||
| 288 | * @return AbstractData | ||
| 289 | * the AbstractData instance or null on invalid name | ||
| 290 | */ | ||
| 291 |     public function getData($name) { | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Getter for all available entity names. | ||
| 300 | * | ||
| 301 | * @return string[] | ||
| 302 | * a list of all available entity names | ||
| 303 | */ | ||
| 304 |     public function getEntities() { | ||
| 307 | |||
| 308 | /** | ||
| 309 | * Determines the Twig template to use for the given parameters depending on | ||
| 310 | * the existance of certain keys in the Container $app in this order: | ||
| 311 | * | ||
| 312 | * crud.$section.$action.$entity | ||
| 313 | * crud.$section.$action | ||
| 314 | * crud.$section | ||
| 315 | * | ||
| 316 | * If nothing exists, this string is returned: "@crud/<action>.twig" | ||
| 317 | * | ||
| 318 | * @param Container $app | ||
| 319 | * the Silex application | ||
| 320 | * @param string $section | ||
| 321 | * the section of the template, either "layout" or "template" | ||
| 322 | * @param string $action | ||
| 323 | * the current calling action like "create" or "show" | ||
| 324 | * @param string $entity | ||
| 325 | * the current calling entity | ||
| 326 | * | ||
| 327 | * @return string | ||
| 328 | * the best fitting template | ||
| 329 | */ | ||
| 330 |     public function getTemplate(Container $app, $section, $action, $entity) { | ||
| 348 | |||
| 349 | /** | ||
| 350 | * Gets whether CRUDlex manages the i18n system. | ||
| 351 | * | ||
| 352 | * @return boolean | ||
| 353 | * true if CRUDlex manages the i18n system | ||
| 354 | */ | ||
| 355 |     public function isManagingI18n() { | ||
| 358 | |||
| 359 | /** | ||
| 360 | * Sets the locale to be used. | ||
| 361 | * | ||
| 362 | * @param string $locale | ||
| 363 | * the locale to be used. | ||
| 364 | */ | ||
| 365 |     public function setLocale($locale) { | ||
| 370 | |||
| 371 | /** | ||
| 372 | * Gets the available locales. | ||
| 373 | * | ||
| 374 | * @return array | ||
| 375 | * the available locales | ||
| 376 | */ | ||
| 377 |     public function getLocales() { | ||
| 394 | |||
| 395 | } | ||
| 396 |