Complex classes like LiipMonitorExtension 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 LiipMonitorExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class LiipMonitorExtension extends Extension |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Loads the services based on your application configuration. |
||
| 21 | * |
||
| 22 | * @param array $configs |
||
| 23 | * @param ContainerBuilder $container |
||
| 24 | */ |
||
| 25 | public function load(array $configs, ContainerBuilder $container) |
||
| 26 | { |
||
| 27 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
| 28 | $loader->load('runner.xml'); |
||
| 29 | $loader->load('helper.xml'); |
||
| 30 | |||
| 31 | $configuration = new Configuration(); |
||
| 32 | $config = $this->processConfiguration($configuration, $configs); |
||
| 33 | |||
| 34 | if (null === $config['view_template']) { |
||
| 35 | $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php'; |
||
| 36 | } |
||
| 37 | |||
| 38 | if ($config['enable_controller']) { |
||
| 39 | $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']); |
||
| 40 | $loader->load('controller.xml'); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($config['mailer']['enabled']) { |
||
| 44 | $loader->load('helper/swift_mailer.xml'); |
||
| 45 | |||
| 46 | foreach ($config['mailer'] as $key => $value) { |
||
| 47 | $container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']); |
||
| 52 | |||
| 53 | // symfony3 does not define templating.helper.assets unless php templating is included |
||
| 54 | if ($container->has('templating.helper.assets')) { |
||
| 55 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 56 | $pathHelper->replaceArgument(0, 'templating.helper.assets'); |
||
| 57 | } |
||
| 58 | |||
| 59 | // symfony3 does not define templating.helper.router unless php templating is included |
||
| 60 | if ($container->has('templating.helper.router')) { |
||
| 61 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 62 | $pathHelper->replaceArgument(1, 'templating.helper.router'); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (empty($config['checks'])) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | $checksLoaded = array(); |
||
| 70 | $containerParams = array(); |
||
| 71 | foreach ($config['checks']['groups'] as $group => $checks) { |
||
| 72 | if (empty($checks)) { |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | |||
| 76 | foreach ($checks as $check => $values) { |
||
| 77 | if (empty($values)) { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | $containerParams['groups'][$group][$check] = $values; |
||
| 82 | $this->setParameters($container, $check, $group, $values); |
||
| 83 | |||
| 84 | if (!in_array($check, $checksLoaded)) { |
||
| 85 | $loader->load('checks/'.$check.'.xml'); |
||
| 86 | $checksLoaded[] = $check; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams); |
||
| 92 | $this->configureDoctrineMigrationsCheck($container, $containerParams); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param ContainerBuilder $container |
||
| 97 | * @param string $checkName |
||
| 98 | * @param string $group |
||
| 99 | * @param array $values |
||
| 100 | */ |
||
| 101 | private function setParameters(ContainerBuilder $container, $checkName, $group, $values) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set up doctrine migration configuration services |
||
| 172 | * |
||
| 173 | * @param ContainerBuilder $container The container |
||
| 174 | * @param array $params Container params |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | private function configureDoctrineMigrationsCheck(ContainerBuilder $container, array $params) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return key-value array with migration version as key and class as a value defined in config file |
||
| 207 | * |
||
| 208 | * @param AbstractFileConfiguration $config Current configuration |
||
| 209 | * @param Connection $connection Fake connections |
||
| 210 | * |
||
| 211 | * @return array[] |
||
| 212 | */ |
||
| 213 | private function getPredefinedMigrations(AbstractFileConfiguration $config, Connection $connection) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Creates migration configuration service definition |
||
| 233 | * |
||
| 234 | * @param ContainerBuilder $container DI Container |
||
| 235 | * @param string $filename File name with migration configuration |
||
| 236 | * @param string $connectionName Connection name for container service |
||
| 237 | * |
||
| 238 | * @return DefinitionDecorator|ChildDefinition |
||
| 239 | */ |
||
| 240 | private function createMigrationConfigurationService(ContainerBuilder $container, $filename, $connectionName) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Creates in-memory migration configuration for setting up container service |
||
| 323 | * |
||
| 324 | * @param ContainerBuilder $container The container |
||
| 325 | * @param Connection $connection Fake connection |
||
| 326 | * @param string $filename Migrations configuration file |
||
| 327 | * |
||
| 328 | * @return AbstractFileConfiguration |
||
| 329 | */ |
||
| 330 | private function createTemporaryConfiguration(ContainerBuilder $container, Connection $connection, $filename) |
||
| 361 | } |
||
| 362 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.