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 |
||
| 13 | class LiipMonitorExtension extends Extension implements CompilerPassInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Loader for doctrine migrations to support both v2 and v3 major versions. |
||
| 17 | * |
||
| 18 | * @var DoctrineMigrationsLoader |
||
| 19 | */ |
||
| 20 | private $migrationsLoader; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * LiipMonitorExtension constructor. |
||
| 24 | */ |
||
| 25 | 49 | public function __construct() |
|
| 26 | { |
||
| 27 | 49 | $this->migrationsLoader = new DoctrineMigrationsLoader(); |
|
| 28 | 49 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Loads the services based on your application configuration. |
||
| 32 | */ |
||
| 33 | 49 | public function load(array $configs, ContainerBuilder $container) |
|
| 34 | { |
||
| 35 | 49 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 36 | 49 | $loader->load('runner.xml'); |
|
| 37 | 49 | $loader->load('helper.xml'); |
|
| 38 | 49 | $loader->load('commands.xml'); |
|
| 39 | |||
| 40 | 49 | $configuration = $this->getConfiguration($configs, $container); |
|
| 41 | 49 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 42 | |||
| 43 | 44 | if (null === $config['view_template']) { |
|
| 44 | 44 | $config['view_template'] = __DIR__.'/../Resources/views/health/index.html.php'; |
|
| 45 | } |
||
| 46 | |||
| 47 | 44 | if ($config['enable_controller']) { |
|
| 48 | 2 | $container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']); |
|
| 49 | 2 | $container->setParameter(sprintf('%s.failure_status_code', $this->getAlias()), $config['failure_status_code']); |
|
| 50 | 2 | $loader->load('controller.xml'); |
|
| 51 | } |
||
| 52 | |||
| 53 | 44 | $this->configureMailer($container, $config); |
|
| 54 | |||
| 55 | 44 | $container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']); |
|
| 56 | |||
| 57 | // symfony3 does not define templating.helper.assets unless php templating is included |
||
| 58 | 44 | if ($container->has('templating.helper.assets')) { |
|
| 59 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 60 | $pathHelper->replaceArgument(0, 'templating.helper.assets'); |
||
| 61 | } |
||
| 62 | |||
| 63 | // symfony3 does not define templating.helper.router unless php templating is included |
||
| 64 | 44 | if ($container->has('templating.helper.router')) { |
|
| 65 | $pathHelper = $container->getDefinition('liip_monitor.helper'); |
||
| 66 | $pathHelper->replaceArgument(1, 'templating.helper.router'); |
||
| 67 | } |
||
| 68 | |||
| 69 | 44 | if (empty($config['checks'])) { |
|
| 70 | 7 | return; |
|
| 71 | } |
||
| 72 | |||
| 73 | 37 | $checksLoaded = []; |
|
| 74 | 37 | $containerParams = []; |
|
| 75 | 37 | foreach ($config['checks']['groups'] as $group => $checks) { |
|
| 76 | 37 | if (empty($checks)) { |
|
| 77 | continue; |
||
| 78 | } |
||
| 79 | |||
| 80 | 37 | foreach ($checks as $check => $values) { |
|
| 81 | 37 | if (empty($values)) { |
|
| 82 | 37 | continue; |
|
| 83 | } |
||
| 84 | |||
| 85 | 37 | $containerParams['groups'][$group][$check] = $values; |
|
| 86 | 37 | $this->setParameters($container, $check, $group, $values); |
|
| 87 | |||
| 88 | 37 | if (!in_array($check, $checksLoaded)) { |
|
| 89 | 37 | $loader->load('checks/'.$check.'.xml'); |
|
| 90 | 37 | $checksLoaded[] = $check; |
|
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | 37 | $container->setParameter(sprintf('%s.checks', $this->getAlias()), $containerParams); |
|
| 96 | 37 | $this->configureDoctrineMigrationsCheck($container, $containerParams); |
|
| 97 | 37 | } |
|
| 98 | |||
| 99 | 40 | public function process(ContainerBuilder $container) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $checkName |
||
| 106 | * @param string $group |
||
| 107 | * @param array $values |
||
| 108 | */ |
||
| 109 | 37 | private function setParameters(ContainerBuilder $container, $checkName, $group, $values) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Set up doctrine migration configuration services. |
||
| 161 | * |
||
| 162 | * @param ContainerBuilder $container The container |
||
| 163 | * @param array $params Container params |
||
| 164 | * |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | 37 | private function configureDoctrineMigrationsCheck(ContainerBuilder $container, array $params) |
|
| 188 | |||
| 189 | 44 | private function configureMailer(ContainerBuilder $container, array $config) |
|
| 199 | } |
||
| 200 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: