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 |
||
| 26 | class LiipMonitorExtension extends Extension implements CompilerPassInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Tuple (migrationsConfiguration, tempConfiguration) for doctrine migrations check |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $migrationConfigurationsServices = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Connection object needed for correct migration loading |
||
| 37 | * |
||
| 38 | * @var Connection |
||
| 39 | */ |
||
| 40 | private $fakeConnection; |
||
| 41 | |||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | if (class_exists(Connection::class)) { |
||
| 45 | $this->fakeConnection = new Connection([], new Driver()); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Loads the services based on your application configuration. |
||
| 51 | * |
||
| 52 | * @param array $configs |
||
| 53 | * @param ContainerBuilder $container |
||
| 54 | */ |
||
| 55 | public function load(array $configs, ContainerBuilder $container) |
||
| 56 | { |
||
| 57 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
| 58 | $loader->load('runner.xml'); |
||
| 59 | $loader->load('helper.xml'); |
||
| 60 | $loader->load('commands.xml'); |
||
| 61 | |||
| 62 | $configuration = new Configuration(); |
||
| 63 | $config = $this->processConfiguration($configuration, $configs); |
||
| 64 | |||
| 65 | if (null === $config['view_template']) { |
||
| 66 | $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php'; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($config['enable_controller']) { |
||
| 70 | $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']); |
||
| 71 | $container->setParameter(sprintf('%s.failure_status_code', $this->getAlias()), $config['failure_status_code']); |
||
| 72 | $loader->load('controller.xml'); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->configureMailer($container, $loader, $config); |
||
| 76 | |||
| 77 | $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']); |
||
| 78 | |||
| 79 | // symfony3 does not define templating.helper.assets unless php templating is included |
||
| 80 | if ($container->has('templating.helper.assets')) { |
||
| 81 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 82 | $pathHelper->replaceArgument(0, 'templating.helper.assets'); |
||
| 83 | } |
||
| 84 | |||
| 85 | // symfony3 does not define templating.helper.router unless php templating is included |
||
| 86 | if ($container->has('templating.helper.router')) { |
||
| 87 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 88 | $pathHelper->replaceArgument(1, 'templating.helper.router'); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (empty($config['checks'])) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $checksLoaded = array(); |
||
| 96 | $containerParams = array(); |
||
| 97 | foreach ($config['checks']['groups'] as $group => $checks) { |
||
| 98 | if (empty($checks)) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | foreach ($checks as $check => $values) { |
||
| 103 | if (empty($values)) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $containerParams['groups'][$group][$check] = $values; |
||
| 108 | $this->setParameters($container, $check, $group, $values); |
||
| 109 | |||
| 110 | if (!in_array($check, $checksLoaded)) { |
||
| 111 | $loader->load('checks/'.$check.'.xml'); |
||
| 112 | $checksLoaded[] = $check; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams); |
||
| 118 | $this->configureDoctrineMigrationsCheck($container, $containerParams); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritDoc |
||
| 123 | */ |
||
| 124 | public function process(ContainerBuilder $container) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param ContainerBuilder $container |
||
| 139 | * @param string $checkName |
||
| 140 | * @param string $group |
||
| 141 | * @param array $values |
||
| 142 | */ |
||
| 143 | private function setParameters(ContainerBuilder $container, $checkName, $group, $values) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set up doctrine migration configuration services |
||
| 211 | * |
||
| 212 | * @param ContainerBuilder $container The container |
||
| 213 | * @param array $params Container params |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | private function configureDoctrineMigrationsCheck(ContainerBuilder $container, array $params) |
||
| 256 | |||
| 257 | private function configureMailer(ContainerBuilder $container, LoaderInterface $loader, array $config) |
||
| 258 | { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Return key-value array with migration version as key and class as a value defined in config file |
||
| 279 | * |
||
| 280 | * @param ContainerBuilder $container The container |
||
| 281 | * @param DoctrineMigrationConfiguration $config Current configuration |
||
| 282 | * @param Connection $connection Fake connections |
||
| 283 | * |
||
| 284 | * @return array[] |
||
| 285 | */ |
||
| 286 | private function getPredefinedMigrations(ContainerBuilder $container, DoctrineMigrationConfiguration $config, Connection $connection) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Creates migration configuration service definition |
||
| 316 | * |
||
| 317 | * @param ContainerBuilder $container DI Container |
||
| 318 | * @param string $connectionName Connection name for container service |
||
| 319 | * @param string $filename File name with migration configuration |
||
| 320 | * |
||
| 321 | * @return DefinitionDecorator|ChildDefinition |
||
| 322 | */ |
||
| 323 | private function createMigrationConfigurationService(ContainerBuilder $container, string $connectionName, string $filename = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Creates in-memory migration configuration for setting up container service |
||
| 399 | * |
||
| 400 | * @param ContainerBuilder $container The container |
||
| 401 | * @param Connection $connection Fake connection |
||
| 402 | * @param string $filename Migrations configuration file |
||
| 403 | * |
||
| 404 | * @return DoctrineMigrationConfiguration |
||
| 405 | */ |
||
| 406 | private function createTemporaryConfiguration( |
||
| 445 | } |
||
| 446 |
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.