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 |
||
| 30 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Holds the {@see AbstractData} instances. |
||
| 34 | */ |
||
| 35 | protected $datas; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Initializes needed but yet missing service providers. |
||
| 39 | * |
||
| 40 | * @param Container $app |
||
| 41 | * the application container |
||
| 42 | */ |
||
| 43 | protected function initMissingServiceProviders(Container $app) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initializes the available locales. |
||
| 64 | * |
||
| 65 | * @param Container $app |
||
| 66 | * the application container |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | * the available locales |
||
| 70 | */ |
||
| 71 | protected function initLocales(Container $app) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Initializes the children of the data entries. |
||
| 83 | */ |
||
| 84 | protected function initChildren() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 97 | * |
||
| 98 | * @param array $locales |
||
| 99 | * the available locales |
||
| 100 | * @param array $crud |
||
| 101 | * the CRUD entity map |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | * the map with localized entity labels |
||
| 105 | */ |
||
| 106 | protected function getLocaleLabels($locales, $crud) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Configures the EntityDefinition according to the given |
||
| 118 | * CRUD entity map. |
||
| 119 | * |
||
| 120 | * @param EntityDefinition $definition |
||
| 121 | * the definition to configure |
||
| 122 | * @param array $crud |
||
| 123 | * the CRUD entity map |
||
| 124 | */ |
||
| 125 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Creates and setups an EntityDefinition instance. |
||
| 145 | * |
||
| 146 | * @param Container $app |
||
| 147 | * the application container |
||
| 148 | * @param array $locales |
||
| 149 | * the available locales |
||
| 150 | * @param array $crud |
||
| 151 | * the parsed YAML of a CRUD entity |
||
| 152 | * @param string $name |
||
| 153 | * the name of the entity |
||
| 154 | * |
||
| 155 | * @return EntityDefinition |
||
| 156 | * the EntityDefinition good to go |
||
| 157 | */ |
||
| 158 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Validates the parsed entity definition. |
||
| 183 | * |
||
| 184 | * @param Container $app |
||
| 185 | * the application container |
||
| 186 | * @param array $entityDefinition |
||
| 187 | * the entity definition to validate |
||
| 188 | */ |
||
| 189 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Initializes the instance. |
||
| 201 | * |
||
| 202 | * @param DataFactoryInterface $dataFactory |
||
| 203 | * the factory to create the concrete AbstractData instances |
||
| 204 | * @param string $crudFile |
||
| 205 | * the CRUD YAML file to parse |
||
| 206 | * @param string|null $crudFileCachingDirectory |
||
| 207 | * the writable directory to store the CRUD YAML file cache |
||
| 208 | * @param FileProcessorInterface $fileProcessor |
||
| 209 | * the file processor used for file fields |
||
| 210 | * @param Container $app |
||
| 211 | * the application container |
||
| 212 | */ |
||
| 213 | public function init(DataFactoryInterface $dataFactory, $crudFile, $crudFileCachingDirectory, FileProcessorInterface $fileProcessor, Container $app) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 233 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 234 | * |
||
| 235 | * @param Container $app |
||
| 236 | * the Container instance of the Silex application |
||
| 237 | */ |
||
| 238 | public function register(Container $app) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Initializes the crud service right after boot. |
||
| 250 | * |
||
| 251 | * @param Application $app |
||
| 252 | * the Container instance of the Silex application |
||
| 253 | */ |
||
| 254 | public function boot(Application $app) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Getter for the {@see AbstractData} instances. |
||
| 262 | * |
||
| 263 | * @param string $name |
||
| 264 | * the entity name of the desired Data instance |
||
| 265 | * |
||
| 266 | * @return AbstractData |
||
| 267 | * the AbstractData instance or null on invalid name |
||
| 268 | */ |
||
| 269 | public function getData($name) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Getter for all available entity names. |
||
| 278 | * |
||
| 279 | * @return string[] |
||
| 280 | * a list of all available entity names |
||
| 281 | */ |
||
| 282 | public function getEntities() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Determines the Twig template to use for the given parameters depending on |
||
| 288 | * the existance of certain keys in the Container $app in this order: |
||
| 289 | * |
||
| 290 | * crud.$section.$action.$entity |
||
| 291 | * crud.$section.$action |
||
| 292 | * crud.$section |
||
| 293 | * |
||
| 294 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 295 | * |
||
| 296 | * @param Container $app |
||
| 297 | * the Silex application |
||
| 298 | * @param string $section |
||
| 299 | * the section of the template, either "layout" or "template" |
||
| 300 | * @param string $action |
||
| 301 | * the current calling action like "create" or "show" |
||
| 302 | * @param string $entity |
||
| 303 | * the current calling entity |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | * the best fitting template |
||
| 307 | */ |
||
| 308 | public function getTemplate(Container $app, $section, $action, $entity) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Sets the locale to be used. |
||
| 329 | * |
||
| 330 | * @param string $locale |
||
| 331 | * the locale to be used. |
||
| 332 | */ |
||
| 333 | public function setLocale($locale) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets the available locales. |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | * the available locales |
||
| 344 | */ |
||
| 345 | public function getLocales() { |
||
| 362 | |||
| 363 | } |
||
| 364 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.