Complex classes like SonataNotificationExtension 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 SonataNotificationExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class SonataNotificationExtension extends Extension |
||
29 | { |
||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $amqpCounter = 0; |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function load(array $configs, ContainerBuilder $container) |
||
39 | { |
||
40 | $configuration = new Configuration(); |
||
41 | $config = $this->processConfiguration($configuration, $configs); |
||
42 | |||
43 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
44 | |||
45 | $loader->load('core.xml'); |
||
46 | $loader->load('doctrine_orm.xml'); |
||
47 | $loader->load('backend.xml'); |
||
48 | $loader->load('consumer.xml'); |
||
49 | $loader->load('selector.xml'); |
||
50 | $loader->load('event.xml'); |
||
51 | |||
52 | if ($config['consumers']['register_default']) { |
||
53 | $loader->load('default_consumers.xml'); |
||
54 | } |
||
55 | |||
56 | $bundles = $container->getParameter('kernel.bundles'); |
||
57 | |||
58 | if (isset($bundles['FOSRestBundle']) && isset($bundles['NelmioApiDocBundle'])) { |
||
59 | $loader->load('api_controllers.xml'); |
||
60 | $loader->load('api_form.xml'); |
||
61 | } |
||
62 | |||
63 | if ($config['admin']['enabled'] && isset($bundles['SonataDoctrineORMAdminBundle'])) { // for now, only support for ORM |
||
64 | $loader->load('admin.xml'); |
||
65 | } |
||
66 | |||
67 | if (isset($bundles['LiipMonitorBundle'])) { |
||
68 | $loader->load('checkmonitor.xml'); |
||
69 | } |
||
70 | |||
71 | $this->checkConfiguration($config); |
||
72 | |||
73 | $container->setAlias('sonata.notification.backend', $config['backend']); |
||
74 | $container->setParameter('sonata.notification.backend', $config['backend']); |
||
75 | |||
76 | $this->registerDoctrineMapping($config); |
||
77 | $this->registerParameters($container, $config); |
||
78 | $this->configureBackends($container, $config); |
||
79 | $this->configureClass($container, $config); |
||
80 | $this->configureListeners($container, $config); |
||
81 | $this->configureAdmin($container, $config); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param ContainerBuilder $container |
||
86 | * @param array $config |
||
87 | */ |
||
88 | public function configureClass(ContainerBuilder $container, $config) |
||
96 | |||
97 | /** |
||
98 | * @param ContainerBuilder $container |
||
99 | * @param array $config |
||
100 | */ |
||
101 | public function configureAdmin(ContainerBuilder $container, $config) |
||
107 | |||
108 | /** |
||
109 | * @param ContainerBuilder $container |
||
110 | * @param array $config |
||
111 | */ |
||
112 | public function registerParameters(ContainerBuilder $container, $config) |
||
117 | |||
118 | /** |
||
119 | * @param ContainerBuilder $container |
||
120 | * @param array $config |
||
121 | */ |
||
122 | public function configureBackends(ContainerBuilder $container, $config) |
||
151 | |||
152 | /** |
||
153 | * @param array $config |
||
154 | */ |
||
155 | public function registerDoctrineMapping(array $config) |
||
167 | |||
168 | /** |
||
169 | * @param array $config |
||
170 | */ |
||
171 | protected function checkConfiguration(array $config) |
||
185 | |||
186 | /** |
||
187 | * @param ContainerBuilder $container |
||
188 | * @param array $config |
||
189 | */ |
||
190 | protected function configureListeners(ContainerBuilder $container, array $config) |
||
191 | { |
||
192 | $ids = $config['iteration_listeners']; |
||
193 | |||
194 | // this one clean the unit of work after every iteration |
||
195 | // it must be set on any backend ... |
||
196 | $ids[] = 'sonata.notification.event.doctrine_optimize'; |
||
197 | |||
198 | if (isset($config['backends']['doctrine']) && $config['backends']['doctrine']['batch_size'] > 1) { |
||
199 | // if the backend is doctrine and the batch size > 1, then |
||
200 | // the unit of work must be cleaned wisely to avoid any issue |
||
201 | // while persisting entities |
||
202 | $ids = array( |
||
203 | 'sonata.notification.event.doctrine_backend_optimize', |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | $container->setParameter('sonata.notification.event.iteration_listeners', $ids); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param ContainerBuilder $container |
||
212 | * @param array $config |
||
213 | * @param bool $checkLevel |
||
214 | * @param int $pause |
||
215 | * @param int $maxAge |
||
216 | * @param int $batchSize |
||
217 | * |
||
218 | * @throws \RuntimeException |
||
219 | */ |
||
220 | protected function configureDoctrineBackends(ContainerBuilder $container, array $config, $checkLevel, $pause, $maxAge, $batchSize) |
||
285 | |||
286 | /** |
||
287 | * @param ContainerBuilder $container |
||
288 | * @param string $manager |
||
289 | * @param bool $checkLevel |
||
290 | * @param int $pause |
||
291 | * @param int $maxAge |
||
292 | * @param int $batchSize |
||
293 | * @param string $key |
||
294 | * @param array $types |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | protected function createDoctrineQueueBackend(ContainerBuilder $container, $manager, $checkLevel, $pause, $maxAge, $batchSize, $key, array $types = array()) |
||
313 | |||
314 | /** |
||
315 | * @param ContainerBuilder $container |
||
316 | * @param array $config |
||
317 | */ |
||
318 | protected function configureRabbitmq(ContainerBuilder $container, array $config) |
||
372 | |||
373 | /** |
||
374 | * @param ContainerBuilder $container |
||
375 | * @param string $exchange |
||
376 | * @param string $name |
||
377 | * @param string $recover |
||
378 | * @param string $key |
||
379 | * @param string $deadLetterExchange |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | protected function createAMQPBackend(ContainerBuilder $container, $exchange, $name, $recover, $key = '', $deadLetterExchange = null) |
||
393 | } |
||
394 |
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: