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 |
||
| 24 | class ServiceProvider implements ServiceProviderInterface { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Holds the {@see AbstractData} instances. |
||
| 28 | */ |
||
| 29 | protected $datas; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Holds whether we manage the i18n. |
||
| 33 | */ |
||
| 34 | protected $manageI18n; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Formats the given time value to a timestring defined by the $pattern |
||
| 38 | * parameter. |
||
| 39 | * |
||
| 40 | * If the value is false (like null), an empty string is |
||
| 41 | * returned. Else, the value is tried to be parsed as datetime via the |
||
| 42 | * given pattern. If that fails, it is tried to be parsed with the pattern |
||
| 43 | * 'Y-m-d H:i:s'. If that fails, the value is returned unchanged. Else, it |
||
| 44 | * is returned formatted with the given pattern. The effect is to shorten |
||
| 45 | * 'Y-m-d H:i:s' to 'Y-m-d' for example. |
||
| 46 | * |
||
| 47 | * @param string $value |
||
| 48 | * the value to be formatted |
||
| 49 | * @param string $timezone |
||
| 50 | * the timezone of the value |
||
| 51 | * @param string $pattern |
||
| 52 | * the pattern with which the value is parsed and formatted |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | * the formatted value |
||
| 56 | */ |
||
| 57 | protected function formatTime($value, $timezone, $pattern) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Reads and returns the contents of the given Yaml file. If |
||
| 74 | * it goes wrong, it throws an exception. |
||
| 75 | * |
||
| 76 | * @param string $fileName |
||
| 77 | * the file to read |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | * the file contents |
||
| 81 | */ |
||
| 82 | protected function readYaml($fileName) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Initializes needed but yet missing service providers. |
||
| 97 | * |
||
| 98 | * @param Container $app |
||
| 99 | * the application container |
||
| 100 | */ |
||
| 101 | protected function initMissingServiceProviders(Container $app) { |
||
| 102 | |||
| 103 | if (!$app->offsetExists('translator')) { |
||
| 104 | $app->register(new \Silex\Provider\LocaleServiceProvider()); |
||
| 105 | $app->register(new \Silex\Provider\TranslationServiceProvider(), [ |
||
| 106 | 'locale_fallbacks' => ['en'], |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!$app->offsetExists('session')) { |
||
| 111 | $app->register(new \Silex\Provider\SessionServiceProvider()); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (!$app->offsetExists('twig')) { |
||
| 115 | $app->register(new \Silex\Provider\TwigServiceProvider()); |
||
| 116 | $app['twig.loader.filesystem']->addPath(__DIR__.'/../views/', 'crud'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Initializes the available locales. |
||
| 122 | * |
||
| 123 | * @param Container $app |
||
| 124 | * the application container |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | * the available locales |
||
| 128 | */ |
||
| 129 | protected function initLocales(Container $app) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Initializes the children of the data entries. |
||
| 141 | */ |
||
| 142 | protected function initChildren() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 155 | * |
||
| 156 | * @param array $locales |
||
| 157 | * the available locales |
||
| 158 | * @param array $crud |
||
| 159 | * the CRUD entity map |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | * the map with localized entity labels |
||
| 163 | */ |
||
| 164 | protected function getLocaleLabels($locales, $crud) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Configures the EntityDefinition according to the given |
||
| 176 | * CRUD entity map. |
||
| 177 | * |
||
| 178 | * @param EntityDefinition $definition |
||
| 179 | * the definition to configure |
||
| 180 | * @param array $crud |
||
| 181 | * the CRUD entity map |
||
| 182 | */ |
||
| 183 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Creates and setups an EntityDefinition instance. |
||
| 203 | * |
||
| 204 | * @param Container $app |
||
| 205 | * the application container |
||
| 206 | * @param array $locales |
||
| 207 | * the available locales |
||
| 208 | * @param array $crud |
||
| 209 | * the parsed YAML of a CRUD entity |
||
| 210 | * @param string $name |
||
| 211 | * the name of the entity |
||
| 212 | * |
||
| 213 | * @return EntityDefinition |
||
| 214 | * the EntityDefinition good to go |
||
| 215 | */ |
||
| 216 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Validates the parsed entity definition. |
||
| 241 | * |
||
| 242 | * @param Container $app |
||
| 243 | * the application container |
||
| 244 | * @param array $entityDefinition |
||
| 245 | * the entity definition to validate |
||
| 246 | */ |
||
| 247 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Initializes the instance. |
||
| 259 | * |
||
| 260 | * @param DataFactoryInterface $dataFactory |
||
| 261 | * the factory to create the concrete AbstractData instances |
||
| 262 | * @param string $crudFile |
||
| 263 | * the CRUD YAML file to parse |
||
| 264 | * @param FileProcessorInterface $fileProcessor |
||
| 265 | * the file processor used for file fields |
||
| 266 | * @param boolean $manageI18n |
||
| 267 | * holds whether we manage the i18n |
||
| 268 | * @param Container $app |
||
| 269 | * the application container |
||
| 270 | */ |
||
| 271 | public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Container $app) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 293 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 294 | * |
||
| 295 | * @param Container $app |
||
| 296 | * the Container instance of the Silex application |
||
| 297 | */ |
||
| 298 | public function register(Container $app) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Getter for the {@see AbstractData} instances. |
||
| 310 | * |
||
| 311 | * @param string $name |
||
| 312 | * the entity name of the desired Data instance |
||
| 313 | * |
||
| 314 | * @return AbstractData |
||
| 315 | * the AbstractData instance or null on invalid name |
||
| 316 | */ |
||
| 317 | public function getData($name) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Getter for all available entity names. |
||
| 326 | * |
||
| 327 | * @return string[] |
||
| 328 | * a list of all available entity names |
||
| 329 | */ |
||
| 330 | public function getEntities() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Formats the given value to a date of the format 'Y-m-d'. |
||
| 336 | * |
||
| 337 | * @param string $value |
||
| 338 | * the value, might be of the format 'Y-m-d H:i' or 'Y-m-d' |
||
| 339 | * @param boolean $isUTC |
||
| 340 | * whether the given value is in UTC |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | * the formatted result or an empty string on null value |
||
| 344 | */ |
||
| 345 | public function formatDate($value, $isUTC) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Formats the given value to a date of the format 'Y-m-d H:i'. |
||
| 352 | * |
||
| 353 | * @param string $value |
||
| 354 | * the value, might be of the format 'Y-m-d H:i' |
||
| 355 | * @param boolean $isUTC |
||
| 356 | * whether the given value is in UTC |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | * the formatted result or an empty string on null value |
||
| 360 | */ |
||
| 361 | public function formatDateTime($value, $isUTC) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Calls PHPs |
||
| 368 | * {@link http://php.net/manual/en/function.basename.php basename} and |
||
| 369 | * returns it's result. |
||
| 370 | * |
||
| 371 | * @param string $value |
||
| 372 | * the value to be handed to basename |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | * the result of basename |
||
| 376 | */ |
||
| 377 | public function basename($value) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Determines the Twig template to use for the given parameters depending on |
||
| 383 | * the existance of certain keys in the Container $app in this order: |
||
| 384 | * |
||
| 385 | * crud.$section.$action.$entity |
||
| 386 | * crud.$section.$action |
||
| 387 | * crud.$section |
||
| 388 | * |
||
| 389 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 390 | * |
||
| 391 | * @param Container $app |
||
| 392 | * the Silex application |
||
| 393 | * @param string $section |
||
| 394 | * the section of the template, either "layout" or "template" |
||
| 395 | * @param string $action |
||
| 396 | * the current calling action like "create" or "show" |
||
| 397 | * @param string $entity |
||
| 398 | * the current calling entity |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | * the best fitting template |
||
| 402 | */ |
||
| 403 | public function getTemplate(Container $app, $section, $action, $entity) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets whether CRUDlex manages the i18n system. |
||
| 424 | * |
||
| 425 | * @return boolean |
||
| 426 | * true if CRUDlex manages the i18n system |
||
| 427 | */ |
||
| 428 | public function isManagingI18n() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Sets the locale to be used. |
||
| 434 | * |
||
| 435 | * @param string $locale |
||
| 436 | * the locale to be used. |
||
| 437 | */ |
||
| 438 | public function setLocale($locale) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Gets the available locales. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | * the available locales |
||
| 449 | */ |
||
| 450 | public function getLocales() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Gets a language name in the given language. |
||
| 470 | * |
||
| 471 | * @param string $language |
||
| 472 | * the language code of the desired language name |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | * the language name in the given language or null if not available |
||
| 476 | */ |
||
| 477 | public function getLanguageName($language) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Formats a float to not display in scientific notation. |
||
| 483 | * |
||
| 484 | * @param float $float |
||
| 485 | * the float to format |
||
| 486 | * |
||
| 487 | * @return double|string |
||
| 488 | * the formated float |
||
| 489 | */ |
||
| 490 | public function formatFloat($float) { |
||
| 506 | |||
| 507 | } |
||
| 508 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.