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 | * Formats the given time value to a timestring defined by the $pattern  | 
            ||
| 42 | * parameter.  | 
            ||
| 43 | *  | 
            ||
| 44 | * If the value is false (like null), an empty string is  | 
            ||
| 45 | * returned. Else, the value is tried to be parsed as datetime via the  | 
            ||
| 46 | * given pattern. If that fails, it is tried to be parsed with the pattern  | 
            ||
| 47 | * 'Y-m-d H:i:s'. If that fails, the value is returned unchanged. Else, it  | 
            ||
| 48 | * is returned formatted with the given pattern. The effect is to shorten  | 
            ||
| 49 | * 'Y-m-d H:i:s' to 'Y-m-d' for example.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @param string $value  | 
            ||
| 52 | * the value to be formatted  | 
            ||
| 53 | * @param string $timezone  | 
            ||
| 54 | * the timezone of the value  | 
            ||
| 55 | * @param string $pattern  | 
            ||
| 56 | * the pattern with which the value is parsed and formatted  | 
            ||
| 57 | *  | 
            ||
| 58 | * @return string  | 
            ||
| 59 | * the formatted value  | 
            ||
| 60 | */  | 
            ||
| 61 |     protected function formatTime($value, $timezone, $pattern) { | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * Reads and returns the contents of the given Yaml file. If  | 
            ||
| 78 | * it goes wrong, it throws an exception.  | 
            ||
| 79 | *  | 
            ||
| 80 | * @param string $fileName  | 
            ||
| 81 | * the file to read  | 
            ||
| 82 | *  | 
            ||
| 83 | * @return array  | 
            ||
| 84 | * the file contents  | 
            ||
| 85 | *  | 
            ||
| 86 | * @throws \RuntimeException  | 
            ||
| 87 | * thrown if the file could not be read or parsed  | 
            ||
| 88 | */  | 
            ||
| 89 |     protected function readYaml($fileName) { | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 | * Initializes needed but yet missing service providers.  | 
            ||
| 104 | *  | 
            ||
| 105 | * @param Container $app  | 
            ||
| 106 | * the application container  | 
            ||
| 107 | */  | 
            ||
| 108 |     protected function initMissingServiceProviders(Container $app) { | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * Initializes the available locales.  | 
            ||
| 129 | *  | 
            ||
| 130 | * @param Container $app  | 
            ||
| 131 | * the application container  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return array  | 
            ||
| 134 | * the available locales  | 
            ||
| 135 | */  | 
            ||
| 136 |     protected function initLocales(Container $app) { | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Initializes the children of the data entries.  | 
            ||
| 148 | */  | 
            ||
| 149 |     protected function initChildren() { | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * Gets a map with localized entity labels from the CRUD YML.  | 
            ||
| 162 | *  | 
            ||
| 163 | * @param array $locales  | 
            ||
| 164 | * the available locales  | 
            ||
| 165 | * @param array $crud  | 
            ||
| 166 | * the CRUD entity map  | 
            ||
| 167 | *  | 
            ||
| 168 | * @return array  | 
            ||
| 169 | * the map with localized entity labels  | 
            ||
| 170 | */  | 
            ||
| 171 |     protected function getLocaleLabels($locales, $crud) { | 
            ||
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * Configures the EntityDefinition according to the given  | 
            ||
| 183 | * CRUD entity map.  | 
            ||
| 184 | *  | 
            ||
| 185 | * @param EntityDefinition $definition  | 
            ||
| 186 | * the definition to configure  | 
            ||
| 187 | * @param array $crud  | 
            ||
| 188 | * the CRUD entity map  | 
            ||
| 189 | */  | 
            ||
| 190 |     protected function configureDefinition(EntityDefinition $definition, array $crud) { | 
            ||
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * Creates and setups an EntityDefinition instance.  | 
            ||
| 210 | *  | 
            ||
| 211 | * @param Container $app  | 
            ||
| 212 | * the application container  | 
            ||
| 213 | * @param array $locales  | 
            ||
| 214 | * the available locales  | 
            ||
| 215 | * @param array $crud  | 
            ||
| 216 | * the parsed YAML of a CRUD entity  | 
            ||
| 217 | * @param string $name  | 
            ||
| 218 | * the name of the entity  | 
            ||
| 219 | *  | 
            ||
| 220 | * @return EntityDefinition  | 
            ||
| 221 | * the EntityDefinition good to go  | 
            ||
| 222 | */  | 
            ||
| 223 |     protected function createDefinition(Container $app, array $locales, array $crud, $name) { | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * Validates the parsed entity definition.  | 
            ||
| 248 | *  | 
            ||
| 249 | * @param Container $app  | 
            ||
| 250 | * the application container  | 
            ||
| 251 | * @param array $entityDefinition  | 
            ||
| 252 | * the entity definition to validate  | 
            ||
| 253 | */  | 
            ||
| 254 |     protected function validateEntityDefinition(Container $app, array $entityDefinition) { | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * Initializes the instance.  | 
            ||
| 266 | *  | 
            ||
| 267 | * @param DataFactoryInterface $dataFactory  | 
            ||
| 268 | * the factory to create the concrete AbstractData instances  | 
            ||
| 269 | * @param string $crudFile  | 
            ||
| 270 | * the CRUD YAML file to parse  | 
            ||
| 271 | * @param FileProcessorInterface $fileProcessor  | 
            ||
| 272 | * the file processor used for file fields  | 
            ||
| 273 | * @param boolean $manageI18n  | 
            ||
| 274 | * holds whether we manage the i18n  | 
            ||
| 275 | * @param Container $app  | 
            ||
| 276 | * the application container  | 
            ||
| 277 | */  | 
            ||
| 278 |     public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Container $app) { | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * Implements ServiceProviderInterface::register() registering $app['crud'].  | 
            ||
| 303 | * $app['crud'] contains an instance of the ServiceProvider afterwards.  | 
            ||
| 304 | *  | 
            ||
| 305 | * @param Container $app  | 
            ||
| 306 | * the Container instance of the Silex application  | 
            ||
| 307 | */  | 
            ||
| 308 |     public function register(Container $app) { | 
            ||
| 317 | |||
| 318 | /**  | 
            ||
| 319 |      * Getter for the {@see AbstractData} instances. | 
            ||
| 320 | *  | 
            ||
| 321 | * @param string $name  | 
            ||
| 322 | * the entity name of the desired Data instance  | 
            ||
| 323 | *  | 
            ||
| 324 | * @return AbstractData  | 
            ||
| 325 | * the AbstractData instance or null on invalid name  | 
            ||
| 326 | */  | 
            ||
| 327 |     public function getData($name) { | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * Getter for all available entity names.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @return string[]  | 
            ||
| 338 | * a list of all available entity names  | 
            ||
| 339 | */  | 
            ||
| 340 |     public function getEntities() { | 
            ||
| 343 | |||
| 344 | /**  | 
            ||
| 345 | * Formats the given value to a date of the format 'Y-m-d'.  | 
            ||
| 346 | *  | 
            ||
| 347 | * @param string $value  | 
            ||
| 348 | * the value, might be of the format 'Y-m-d H:i' or 'Y-m-d'  | 
            ||
| 349 | * @param boolean $isUTC  | 
            ||
| 350 | * whether the given value is in UTC  | 
            ||
| 351 | *  | 
            ||
| 352 | * @return string  | 
            ||
| 353 | * the formatted result or an empty string on null value  | 
            ||
| 354 | */  | 
            ||
| 355 |     public function formatDate($value, $isUTC) { | 
            ||
| 359 | |||
| 360 | /**  | 
            ||
| 361 | * Formats the given value to a date of the format 'Y-m-d H:i'.  | 
            ||
| 362 | *  | 
            ||
| 363 | * @param string $value  | 
            ||
| 364 | * the value, might be of the format 'Y-m-d H:i'  | 
            ||
| 365 | * @param boolean $isUTC  | 
            ||
| 366 | * whether the given value is in UTC  | 
            ||
| 367 | *  | 
            ||
| 368 | * @return string  | 
            ||
| 369 | * the formatted result or an empty string on null value  | 
            ||
| 370 | */  | 
            ||
| 371 |     public function formatDateTime($value, $isUTC) { | 
            ||
| 375 | |||
| 376 | /**  | 
            ||
| 377 | * Calls PHPs  | 
            ||
| 378 |      * {@link http://php.net/manual/en/function.basename.php basename} and | 
            ||
| 379 | * returns it's result.  | 
            ||
| 380 | *  | 
            ||
| 381 | * @param string $value  | 
            ||
| 382 | * the value to be handed to basename  | 
            ||
| 383 | *  | 
            ||
| 384 | * @return string  | 
            ||
| 385 | * the result of basename  | 
            ||
| 386 | */  | 
            ||
| 387 |     public function basename($value) { | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Determines the Twig template to use for the given parameters depending on  | 
            ||
| 393 | * the existance of certain keys in the Container $app in this order:  | 
            ||
| 394 | *  | 
            ||
| 395 | * crud.$section.$action.$entity  | 
            ||
| 396 | * crud.$section.$action  | 
            ||
| 397 | * crud.$section  | 
            ||
| 398 | *  | 
            ||
| 399 | * If nothing exists, this string is returned: "@crud/<action>.twig"  | 
            ||
| 400 | *  | 
            ||
| 401 | * @param Container $app  | 
            ||
| 402 | * the Silex application  | 
            ||
| 403 | * @param string $section  | 
            ||
| 404 | * the section of the template, either "layout" or "template"  | 
            ||
| 405 | * @param string $action  | 
            ||
| 406 | * the current calling action like "create" or "show"  | 
            ||
| 407 | * @param string $entity  | 
            ||
| 408 | * the current calling entity  | 
            ||
| 409 | *  | 
            ||
| 410 | * @return string  | 
            ||
| 411 | * the best fitting template  | 
            ||
| 412 | */  | 
            ||
| 413 |     public function getTemplate(Container $app, $section, $action, $entity) { | 
            ||
| 431 | |||
| 432 | /**  | 
            ||
| 433 | * Gets whether CRUDlex manages the i18n system.  | 
            ||
| 434 | *  | 
            ||
| 435 | * @return boolean  | 
            ||
| 436 | * true if CRUDlex manages the i18n system  | 
            ||
| 437 | */  | 
            ||
| 438 |     public function isManagingI18n() { | 
            ||
| 441 | |||
| 442 | /**  | 
            ||
| 443 | * Sets the locale to be used.  | 
            ||
| 444 | *  | 
            ||
| 445 | * @param string $locale  | 
            ||
| 446 | * the locale to be used.  | 
            ||
| 447 | */  | 
            ||
| 448 |     public function setLocale($locale) { | 
            ||
| 453 | |||
| 454 | /**  | 
            ||
| 455 | * Gets the available locales.  | 
            ||
| 456 | *  | 
            ||
| 457 | * @return array  | 
            ||
| 458 | * the available locales  | 
            ||
| 459 | */  | 
            ||
| 460 |     public function getLocales() { | 
            ||
| 477 | |||
| 478 | }  | 
            ||
| 479 |