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 |
||
| 29 | class ServiceProvider implements ServiceProviderInterface { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Holds the {@see AbstractData} instances. |
||
| 33 | */ |
||
| 34 | protected $datas; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Holds whether we manage the i18n. |
||
| 38 | */ |
||
| 39 | protected $manageI18n; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Formats the given time value to a timestring defined by the $pattern |
||
| 43 | * parameter. |
||
| 44 | * |
||
| 45 | * If the value is false (like null), an empty string is |
||
| 46 | * returned. Else, the value is tried to be parsed as datetime via the |
||
| 47 | * given pattern. If that fails, it is tried to be parsed with the pattern |
||
| 48 | * 'Y-m-d H:i:s'. If that fails, the value is returned unchanged. Else, it |
||
| 49 | * is returned formatted with the given pattern. The effect is to shorten |
||
| 50 | * 'Y-m-d H:i:s' to 'Y-m-d' for example. |
||
| 51 | * |
||
| 52 | * @param string $value |
||
| 53 | * the value to be formatted |
||
| 54 | * @param string $timezone |
||
| 55 | * the timezone of the value |
||
| 56 | * @param string $pattern |
||
| 57 | * the pattern with which the value is parsed and formatted |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | * the formatted value |
||
| 61 | */ |
||
| 62 | protected function formatTime($value, $timezone, $pattern) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Reads and returns the contents of the given Yaml file. If |
||
| 79 | * it goes wrong, it throws an exception. |
||
| 80 | * |
||
| 81 | * @param string $fileName |
||
| 82 | * the file to read |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | * the file contents |
||
| 86 | * |
||
| 87 | * @throws \RuntimeException |
||
| 88 | * thrown if the file could not be read or parsed |
||
| 89 | */ |
||
| 90 | protected function readYaml($fileName) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Initializes needed but yet missing service providers. |
||
| 105 | * |
||
| 106 | * @param Container $app |
||
| 107 | * the application container |
||
| 108 | */ |
||
| 109 | protected function initMissingServiceProviders(Container $app) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Initializes the available locales. |
||
| 130 | * |
||
| 131 | * @param Container $app |
||
| 132 | * the application container |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | * the available locales |
||
| 136 | */ |
||
| 137 | protected function initLocales(Container $app) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Initializes the children of the data entries. |
||
| 149 | */ |
||
| 150 | protected function initChildren() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 163 | * |
||
| 164 | * @param array $locales |
||
| 165 | * the available locales |
||
| 166 | * @param array $crud |
||
| 167 | * the CRUD entity map |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | * the map with localized entity labels |
||
| 171 | */ |
||
| 172 | protected function getLocaleLabels($locales, $crud) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Configures the EntityDefinition according to the given |
||
| 184 | * CRUD entity map. |
||
| 185 | * |
||
| 186 | * @param EntityDefinition $definition |
||
| 187 | * the definition to configure |
||
| 188 | * @param array $crud |
||
| 189 | * the CRUD entity map |
||
| 190 | */ |
||
| 191 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Creates and setups an EntityDefinition instance. |
||
| 211 | * |
||
| 212 | * @param Container $app |
||
| 213 | * the application container |
||
| 214 | * @param array $locales |
||
| 215 | * the available locales |
||
| 216 | * @param array $crud |
||
| 217 | * the parsed YAML of a CRUD entity |
||
| 218 | * @param string $name |
||
| 219 | * the name of the entity |
||
| 220 | * |
||
| 221 | * @return EntityDefinition |
||
| 222 | * the EntityDefinition good to go |
||
| 223 | */ |
||
| 224 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Validates the parsed entity definition. |
||
| 249 | * |
||
| 250 | * @param Container $app |
||
| 251 | * the application container |
||
| 252 | * @param array $entityDefinition |
||
| 253 | * the entity definition to validate |
||
| 254 | */ |
||
| 255 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Initializes the instance. |
||
| 267 | * |
||
| 268 | * @param DataFactoryInterface $dataFactory |
||
| 269 | * the factory to create the concrete AbstractData instances |
||
| 270 | * @param string $crudFile |
||
| 271 | * the CRUD YAML file to parse |
||
| 272 | * @param FileProcessorInterface $fileProcessor |
||
| 273 | * the file processor used for file fields |
||
| 274 | * @param boolean $manageI18n |
||
| 275 | * holds whether we manage the i18n |
||
| 276 | * @param Container $app |
||
| 277 | * the application container |
||
| 278 | */ |
||
| 279 | public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Container $app) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 301 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 302 | * |
||
| 303 | * @param Container $app |
||
| 304 | * the Container instance of the Silex application |
||
| 305 | */ |
||
| 306 | public function register(Container $app) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Getter for the {@see AbstractData} instances. |
||
| 318 | * |
||
| 319 | * @param string $name |
||
| 320 | * the entity name of the desired Data instance |
||
| 321 | * |
||
| 322 | * @return AbstractData |
||
| 323 | * the AbstractData instance or null on invalid name |
||
| 324 | */ |
||
| 325 | public function getData($name) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Getter for all available entity names. |
||
| 334 | * |
||
| 335 | * @return string[] |
||
| 336 | * a list of all available entity names |
||
| 337 | */ |
||
| 338 | public function getEntities() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Formats the given value to a date of the format 'Y-m-d'. |
||
| 344 | * |
||
| 345 | * @param string $value |
||
| 346 | * the value, might be of the format 'Y-m-d H:i' or 'Y-m-d' |
||
| 347 | * @param boolean $isUTC |
||
| 348 | * whether the given value is in UTC |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | * the formatted result or an empty string on null value |
||
| 352 | */ |
||
| 353 | public function formatDate($value, $isUTC) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Formats the given value to a date of the format 'Y-m-d H:i'. |
||
| 360 | * |
||
| 361 | * @param string $value |
||
| 362 | * the value, might be of the format 'Y-m-d H:i' |
||
| 363 | * @param boolean $isUTC |
||
| 364 | * whether the given value is in UTC |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | * the formatted result or an empty string on null value |
||
| 368 | */ |
||
| 369 | public function formatDateTime($value, $isUTC) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Calls PHPs |
||
| 376 | * {@link http://php.net/manual/en/function.basename.php basename} and |
||
| 377 | * returns it's result. |
||
| 378 | * |
||
| 379 | * @param string $value |
||
| 380 | * the value to be handed to basename |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | * the result of basename |
||
| 384 | */ |
||
| 385 | public function basename($value) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Determines the Twig template to use for the given parameters depending on |
||
| 391 | * the existance of certain keys in the Container $app in this order: |
||
| 392 | * |
||
| 393 | * crud.$section.$action.$entity |
||
| 394 | * crud.$section.$action |
||
| 395 | * crud.$section |
||
| 396 | * |
||
| 397 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 398 | * |
||
| 399 | * @param Container $app |
||
| 400 | * the Silex application |
||
| 401 | * @param string $section |
||
| 402 | * the section of the template, either "layout" or "template" |
||
| 403 | * @param string $action |
||
| 404 | * the current calling action like "create" or "show" |
||
| 405 | * @param string $entity |
||
| 406 | * the current calling entity |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | * the best fitting template |
||
| 410 | */ |
||
| 411 | public function getTemplate(Container $app, $section, $action, $entity) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Gets whether CRUDlex manages the i18n system. |
||
| 432 | * |
||
| 433 | * @return boolean |
||
| 434 | * true if CRUDlex manages the i18n system |
||
| 435 | */ |
||
| 436 | public function isManagingI18n() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Sets the locale to be used. |
||
| 442 | * |
||
| 443 | * @param string $locale |
||
| 444 | * the locale to be used. |
||
| 445 | */ |
||
| 446 | public function setLocale($locale) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Gets the available locales. |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | * the available locales |
||
| 457 | */ |
||
| 458 | public function getLocales() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Gets a language name in the given language. |
||
| 478 | * |
||
| 479 | * @param string $language |
||
| 480 | * the language code of the desired language name |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | * the language name in the given language or null if not available |
||
| 484 | */ |
||
| 485 | public function getLanguageName($language) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Formats a float to not display in scientific notation. |
||
| 491 | * |
||
| 492 | * @param float $float |
||
| 493 | * the float to format |
||
| 494 | * |
||
| 495 | * @return double|string |
||
| 496 | * the formated float |
||
| 497 | */ |
||
| 498 | public function formatFloat($float) { |
||
| 514 | |||
| 515 | public function arrayColumn($array, $key) { |
||
| 518 | |||
| 519 | } |
||
| 520 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.