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) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param array $config |
||
| 86 | */ |
||
| 87 | protected function checkConfiguration(array $config) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param ContainerBuilder $container |
||
| 104 | * @param array $config |
||
| 105 | */ |
||
| 106 | protected function configureListeners(ContainerBuilder $container, array $config) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param ContainerBuilder $container |
||
| 128 | * @param array $config |
||
| 129 | */ |
||
| 130 | public function configureClass(ContainerBuilder $container, $config) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param ContainerBuilder $container |
||
| 141 | * @param array $config |
||
| 142 | */ |
||
| 143 | public function configureAdmin(ContainerBuilder $container, $config) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param ContainerBuilder $container |
||
| 152 | * @param array $config |
||
| 153 | */ |
||
| 154 | public function registerParameters(ContainerBuilder $container, $config) |
||
| 155 | { |
||
| 156 | $container->setParameter('sonata.notification.message.class', $config['class']['message']); |
||
| 157 | $container->setParameter('sonata.notification.admin.message.entity', $config['class']['message']); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param ContainerBuilder $container |
||
| 162 | * @param array $config |
||
| 163 | */ |
||
| 164 | public function configureBackends(ContainerBuilder $container, $config) |
||
| 165 | { |
||
| 166 | // set the default value, will be erase if required |
||
| 167 | $container->setAlias('sonata.notification.manager.message', 'sonata.notification.manager.message.default'); |
||
| 168 | |||
| 169 | if (isset($config['backends']['rabbitmq']) && $config['backend'] === 'sonata.notification.backend.rabbitmq') { |
||
| 170 | $this->configureRabbitmq($container, $config); |
||
| 171 | |||
| 172 | $container->removeDefinition('sonata.notification.backend.doctrine'); |
||
| 173 | } else { |
||
| 174 | $container->removeDefinition('sonata.notification.backend.rabbitmq'); |
||
| 175 | } |
||
| 176 | |||
| 177 | if (isset($config['backends']['doctrine']) && $config['backend'] === 'sonata.notification.backend.doctrine') { |
||
| 178 | $checkLevel = array( |
||
| 179 | MessageInterface::STATE_DONE => $config['backends']['doctrine']['states']['done'], |
||
| 180 | MessageInterface::STATE_ERROR => $config['backends']['doctrine']['states']['error'], |
||
| 181 | MessageInterface::STATE_IN_PROGRESS => $config['backends']['doctrine']['states']['in_progress'], |
||
| 182 | MessageInterface::STATE_OPEN => $config['backends']['doctrine']['states']['open'], |
||
| 183 | ); |
||
| 184 | |||
| 185 | $pause = $config['backends']['doctrine']['pause']; |
||
| 186 | $maxAge = $config['backends']['doctrine']['max_age']; |
||
| 187 | $batchSize = $config['backends']['doctrine']['batch_size']; |
||
| 188 | $container->setAlias('sonata.notification.manager.message', $config['backends']['doctrine']['message_manager']); |
||
| 189 | |||
| 190 | $this->configureDoctrineBackends($container, $config, $checkLevel, $pause, $maxAge, $batchSize); |
||
|
|
|||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ContainerBuilder $container |
||
| 196 | * @param array $config |
||
| 197 | * @param bool $checkLevel |
||
| 198 | * @param int $pause |
||
| 199 | * @param int $maxAge |
||
| 200 | * @param int $batchSize |
||
| 201 | * |
||
| 202 | * @throws \RuntimeException |
||
| 203 | */ |
||
| 204 | protected function configureDoctrineBackends(ContainerBuilder $container, array $config, $checkLevel, $pause, $maxAge, $batchSize) |
||
| 205 | { |
||
| 206 | $queues = $config['queues']; |
||
| 207 | $qBackends = array(); |
||
| 208 | |||
| 209 | $definition = $container->getDefinition('sonata.notification.backend.doctrine'); |
||
| 210 | |||
| 211 | // no queue defined, set a default one |
||
| 212 | if (count($queues) == 0) { |
||
| 213 | $queues = array(array( |
||
| 214 | 'queue' => 'default', |
||
| 215 | 'default' => true, |
||
| 216 | 'types' => array(), |
||
| 217 | )); |
||
| 218 | } |
||
| 219 | |||
| 220 | $defaultSet = false; |
||
| 221 | $declaredQueues = array(); |
||
| 222 | |||
| 223 | foreach ($queues as $pos => &$queue) { |
||
| 224 | if (in_array($queue['queue'], $declaredQueues)) { |
||
| 225 | throw new \RuntimeException('The doctrine backend does not support 2 identicals queue name, please rename one queue'); |
||
| 226 | } |
||
| 227 | |||
| 228 | $declaredQueues[] = $queue['queue']; |
||
| 229 | |||
| 230 | // make the configuration compatible with old code and rabbitmq |
||
| 231 | if (isset($queue['routing_key']) && strlen($queue['routing_key']) > 0) { |
||
| 232 | $queue['types'] = array($queue['routing_key']); |
||
| 233 | } |
||
| 234 | |||
| 235 | if (empty($queue['types']) && $queue['default'] === false) { |
||
| 236 | throw new \RuntimeException('You cannot declared a doctrine queue with no type defined with default = false'); |
||
| 237 | } |
||
| 238 | |||
| 239 | if (!empty($queue['types']) && $queue['default'] === true) { |
||
| 240 | throw new \RuntimeException('You cannot declared a doctrine queue with types defined with default = true'); |
||
| 241 | } |
||
| 242 | |||
| 243 | $id = $this->createDoctrineQueueBackend($container, $definition->getArgument(0), $checkLevel, $pause, $maxAge, $batchSize, $queue['queue'], $queue['types']); |
||
| 244 | $qBackends[$pos] = array( |
||
| 245 | 'types' => $queue['types'], |
||
| 246 | 'backend' => new Reference($id), |
||
| 247 | ); |
||
| 248 | |||
| 249 | if ($queue['default'] === true) { |
||
| 250 | if ($defaultSet === true) { |
||
| 251 | throw new \RuntimeException('You can only set one doctrine default queue in your sonata notification configuration.'); |
||
| 252 | } |
||
| 253 | |||
| 254 | $defaultSet = true; |
||
| 255 | $defaultQueue = $queue['queue']; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | if ($defaultSet === false) { |
||
| 260 | throw new \RuntimeException('You need to specify a valid default queue for the doctrine backend!'); |
||
| 261 | } |
||
| 262 | |||
| 263 | $definition |
||
| 264 | ->replaceArgument(1, $queues) |
||
| 265 | ->replaceArgument(2, $defaultQueue) |
||
| 266 | ->replaceArgument(3, $qBackends) |
||
| 267 | ; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param ContainerBuilder $container |
||
| 272 | * @param string $manager |
||
| 273 | * @param bool $checkLevel |
||
| 274 | * @param int $pause |
||
| 275 | * @param int $maxAge |
||
| 276 | * @param int $batchSize |
||
| 277 | * @param string $key |
||
| 278 | * @param array $types |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | protected function createDoctrineQueueBackend(ContainerBuilder $container, $manager, $checkLevel, $pause, $maxAge, $batchSize, $key, array $types = array()) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param ContainerBuilder $container |
||
| 300 | * @param array $config |
||
| 301 | */ |
||
| 302 | protected function configureRabbitmq(ContainerBuilder $container, array $config) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param ContainerBuilder $container |
||
| 359 | * @param string $exchange |
||
| 360 | * @param string $name |
||
| 361 | * @param string $recover |
||
| 362 | * @param string $key |
||
| 363 | * @param string $deadLetterExchange |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | protected function createAMQPBackend(ContainerBuilder $container, $exchange, $name, $recover, $key = '', $deadLetterExchange = null) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array $config |
||
| 380 | */ |
||
| 381 | public function registerDoctrineMapping(array $config) |
||
| 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: