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 |
||
| 22 | class LiipMonitorExtension extends Extension implements CompilerPassInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Tuple (migrationsConfiguration, tempConfiguration) for doctrine migrations check. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $migrationConfigurationsServices = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Connection object needed for correct migration loading. |
||
| 33 | * |
||
| 34 | * @var Connection |
||
| 35 | */ |
||
| 36 | private $fakeConnection; |
||
| 37 | |||
| 38 | 49 | public function __construct() |
|
| 39 | { |
||
| 40 | 49 | if (class_exists(Connection::class)) { |
|
| 41 | 49 | $this->fakeConnection = new Connection([], new Driver()); |
|
| 42 | } |
||
| 43 | 49 | } |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Loads the services based on your application configuration. |
||
| 47 | */ |
||
| 48 | 49 | public function load(array $configs, ContainerBuilder $container) |
|
| 49 | { |
||
| 50 | 49 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 51 | 49 | $loader->load('runner.xml'); |
|
| 52 | 49 | $loader->load('helper.xml'); |
|
| 53 | 49 | $loader->load('commands.xml'); |
|
| 54 | |||
| 55 | 49 | $configuration = new Configuration(); |
|
| 56 | 49 | $config = $this->processConfiguration($configuration, $configs); |
|
| 57 | |||
| 58 | 44 | if (null === $config['view_template']) { |
|
| 59 | 44 | $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php'; |
|
| 60 | } |
||
| 61 | |||
| 62 | 44 | if ($config['enable_controller']) { |
|
| 63 | 2 | $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']); |
|
| 64 | 2 | $container->setParameter(sprintf('%s.failure_status_code', $this->getAlias()), $config['failure_status_code']); |
|
| 65 | 2 | $loader->load('controller.xml'); |
|
| 66 | } |
||
| 67 | |||
| 68 | 44 | $this->configureMailer($container, $loader, $config); |
|
| 69 | |||
| 70 | 44 | $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']); |
|
| 71 | |||
| 72 | // symfony3 does not define templating.helper.assets unless php templating is included |
||
| 73 | 44 | if ($container->has('templating.helper.assets')) { |
|
| 74 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 75 | $pathHelper->replaceArgument(0, 'templating.helper.assets'); |
||
| 76 | } |
||
| 77 | |||
| 78 | // symfony3 does not define templating.helper.router unless php templating is included |
||
| 79 | 44 | if ($container->has('templating.helper.router')) { |
|
| 80 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 81 | $pathHelper->replaceArgument(1, 'templating.helper.router'); |
||
| 82 | } |
||
| 83 | |||
| 84 | 44 | if (empty($config['checks'])) { |
|
| 85 | 7 | return; |
|
| 86 | } |
||
| 87 | |||
| 88 | 37 | $checksLoaded = []; |
|
| 89 | 37 | $containerParams = []; |
|
| 90 | 37 | foreach ($config['checks']['groups'] as $group => $checks) { |
|
| 91 | 37 | if (empty($checks)) { |
|
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | 37 | foreach ($checks as $check => $values) { |
|
| 96 | 37 | if (empty($values)) { |
|
| 97 | 37 | continue; |
|
| 98 | } |
||
| 99 | |||
| 100 | 37 | $containerParams['groups'][$group][$check] = $values; |
|
| 101 | 37 | $this->setParameters($container, $check, $group, $values); |
|
| 102 | |||
| 103 | 37 | if (!in_array($check, $checksLoaded)) { |
|
| 104 | 37 | $loader->load('checks/'.$check.'.xml'); |
|
| 105 | 37 | $checksLoaded[] = $check; |
|
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | 37 | $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams); |
|
| 111 | 37 | $this->configureDoctrineMigrationsCheck($container, $containerParams); |
|
| 112 | 37 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | 40 | public function process(ContainerBuilder $container) |
|
| 118 | { |
||
| 119 | 40 | foreach ($this->migrationConfigurationsServices as $services) { |
|
| 120 | list($configurationService, $configuration) = $services; |
||
| 121 | /** @var Definition $configurationService */ |
||
| 122 | /** @var DoctrineMigrationConfiguration $configuration */ |
||
| 123 | $versions = $this->getPredefinedMigrations($container, $configuration, $this->fakeConnection); |
||
| 124 | if ($versions) { |
||
|
|
|||
| 125 | $configurationService->addMethodCall('registerMigrations', [$versions]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | 40 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $checkName |
||
| 132 | * @param string $group |
||
| 133 | * @param array $values |
||
| 134 | */ |
||
| 135 | 37 | private function setParameters(ContainerBuilder $container, $checkName, $group, $values) |
|
| 136 | { |
||
| 137 | 37 | $prefix = sprintf('%s.check.%s', $this->getAlias(), $checkName); |
|
| 138 | 37 | switch ($checkName) { |
|
| 139 | 37 | case 'class_exists': |
|
| 140 | 36 | case 'cpu_performance': |
|
| 141 | 35 | case 'php_extensions': |
|
| 142 | 32 | case 'php_version': |
|
| 143 | 31 | case 'php_flags': |
|
| 144 | 30 | case 'readable_directory': |
|
| 145 | 29 | case 'writable_directory': |
|
| 146 | 28 | case 'process_running': |
|
| 147 | 24 | case 'doctrine_dbal': |
|
| 148 | 20 | case 'doctrine_mongodb': |
|
| 149 | 20 | case 'http_service': |
|
| 150 | 19 | case 'guzzle_http_service': |
|
| 151 | 18 | case 'memcache': |
|
| 152 | 17 | case 'memcached': |
|
| 153 | 17 | case 'redis': |
|
| 154 | 16 | case 'rabbit_mq': |
|
| 155 | 15 | case 'stream_wrapper_exists': |
|
| 156 | 14 | case 'file_ini': |
|
| 157 | 13 | case 'file_json': |
|
| 158 | 12 | case 'file_xml': |
|
| 159 | 11 | case 'file_yaml': |
|
| 160 | 10 | case 'expressions': |
|
| 161 | 28 | $container->setParameter($prefix.'.'.$group, $values); |
|
| 162 | 28 | break; |
|
| 163 | |||
| 164 | 9 | case 'symfony_version': |
|
| 165 | 1 | break; |
|
| 166 | |||
| 167 | 8 | case 'opcache_memory': |
|
| 168 | 1 | if (!class_exists('ZendDiagnostics\Check\OpCacheMemory')) { |
|
| 169 | throw new \InvalidArgumentException('Please require at least "v1.0.4" of "ZendDiagnostics"'); |
||
| 170 | } |
||
| 171 | 1 | break; |
|
| 172 | |||
| 173 | 7 | case 'doctrine_migrations': |
|
| 174 | if (!class_exists('ZendDiagnostics\Check\DoctrineMigration')) { |
||
| 175 | throw new \InvalidArgumentException('Please require at least "v1.0.6" of "ZendDiagnostics"'); |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!class_exists('Doctrine\Migrations\Configuration\Configuration')) { |
||
| 179 | throw new \InvalidArgumentException('Please require at least "v2.0.0" of "Doctrine Migrations Library"'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $container->setParameter($prefix.'.'.$group, $values); |
||
| 183 | break; |
||
| 184 | |||
| 185 | 7 | case 'pdo_connections': |
|
| 186 | 1 | if (!class_exists('ZendDiagnostics\Check\PDOCheck')) { |
|
| 187 | throw new \InvalidArgumentException('Please require at least "v1.0.5" of "ZendDiagnostics"'); |
||
| 188 | } |
||
| 189 | 1 | $container->setParameter($prefix.'.'.$group, $values); |
|
| 190 | 1 | break; |
|
| 191 | } |
||
| 192 | |||
| 193 | 37 | if (is_array($values)) { |
|
| 194 | 33 | foreach ($values as $key => $value) { |
|
| 195 | 33 | $container->setParameter($prefix.'.'.$key.'.'.$group, $value); |
|
| 196 | } |
||
| 197 | } |
||
| 198 | 37 | } |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Set up doctrine migration configuration services. |
||
| 202 | * |
||
| 203 | * @param ContainerBuilder $container The container |
||
| 204 | * @param array $params Container params |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 37 | private function configureDoctrineMigrationsCheck(ContainerBuilder $container, array $params) |
|
| 209 | { |
||
| 210 | 37 | if (!$container->hasDefinition('liip_monitor.check.doctrine_migrations') || !isset($params['groups'])) { |
|
| 211 | 37 | return; |
|
| 212 | } |
||
| 213 | |||
| 214 | foreach ($params['groups'] as $groupName => $groupChecks) { |
||
| 215 | if (!isset($groupChecks['doctrine_migrations'])) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | $services = []; |
||
| 220 | foreach ($groupChecks['doctrine_migrations'] as $key => $config) { |
||
| 221 | try { |
||
| 222 | $serviceConfiguration = |
||
| 223 | $this->createMigrationConfigurationService($container, $config['connection'], $config['configuration_file'] ?? null); |
||
| 224 | |||
| 225 | $serviceId = sprintf('liip_monitor.check.doctrine_migrations.configuration.%s.%s', $groupName, $key); |
||
| 226 | $container->setDefinition($serviceId, $serviceConfiguration); |
||
| 227 | |||
| 228 | $services[$key] = $serviceId; |
||
| 229 | } catch (MigrationException $e) { |
||
| 230 | throw new MigrationException(sprintf('Invalid doctrine migration check under "%s.%s": %s', $groupName, $key, $e->getMessage()), $e->getCode(), $e); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $parameter = sprintf('%s.check.%s.%s', $this->getAlias(), 'doctrine_migrations', $groupName); |
||
| 235 | $container->setParameter($parameter, $services); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | 44 | private function configureMailer(ContainerBuilder $container, LoaderInterface $loader, array $config) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Return key-value array with migration version as key and class as a value defined in config file. |
||
| 254 | * |
||
| 255 | * @param ContainerBuilder $container The container |
||
| 256 | * @param DoctrineMigrationConfiguration $config Current configuration |
||
| 257 | * @param Connection $connection Fake connections |
||
| 258 | * |
||
| 259 | * @return array[] |
||
| 260 | */ |
||
| 261 | private function getPredefinedMigrations(ContainerBuilder $container, DoctrineMigrationConfiguration $config, Connection $connection) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Creates migration configuration service definition. |
||
| 291 | * |
||
| 292 | * @param ContainerBuilder $container DI Container |
||
| 293 | * @param string $connectionName Connection name for container service |
||
| 294 | * @param string $filename File name with migration configuration |
||
| 295 | * |
||
| 296 | * @return DefinitionDecorator|ChildDefinition |
||
| 297 | */ |
||
| 298 | private function createMigrationConfigurationService(ContainerBuilder $container, string $connectionName, string $filename = null) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Creates in-memory migration configuration for setting up container service. |
||
| 374 | * |
||
| 375 | * @param ContainerBuilder $container The container |
||
| 376 | * @param Connection $connection Fake connection |
||
| 377 | * @param string $filename Migrations configuration file |
||
| 378 | */ |
||
| 379 | private function createTemporaryConfiguration( |
||
| 418 | } |
||
| 419 |
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.