Completed
Push — master ( dd40bf...739ea2 )
by Asmir
18:26 queued 13:49
created

RegisterEventListenersAndSubscribersPass.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace JMS\SerializerBundle\DependencyInjection\Compiler;
4
5
use JMS\Serializer\EventDispatcher\EventDispatcher;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
11
{
12 29
    public function process(ContainerBuilder $container)
13
    {
14 29
        $listeners = array();
15 29
        foreach ($container->findTaggedServiceIds('jms_serializer.event_listener') as $id => $tags) {
16
17 4
            foreach ($tags as $attributes) {
18 4
                if (!isset($attributes['event'])) {
19 1
                    throw new \RuntimeException(sprintf('The tag "jms_serializer.event_listener" of service "%s" requires an attribute named "event".', $id));
20 1
                }
21
22 3
                $class = isset($attributes['class'])
23 3
                    ? strtolower($container->getParameterBag()->resolveValue($attributes['class']))
24 3
                    : null;
25
26 3
                $format = isset($attributes['format']) ? $attributes['format'] : null;
27 3
                $method = isset($attributes['method']) ? $attributes['method'] : EventDispatcher::getDefaultMethodName($attributes['event']);
28 3
                $priority = isset($attributes['priority']) ? (integer)$attributes['priority'] : 0;
29
30 3 View Code Duplication
                if ($container->getDefinition($id)->isPublic()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31 2
                    $listeners[$attributes['event']][$priority][] = array(array($id, $method), $class, $format);
32 2
                } else {
33 1
                    $listeners[$attributes['event']][$priority][] = array(array(new Reference($id), $method), $class, $format);
34
                }
35 3
            }
36 28
        }
37
38 28
        foreach ($container->findTaggedServiceIds('jms_serializer.event_subscriber') as $id => $tags) {
39 25
            $subscriberClass = $container->getDefinition($id)->getClass();
40
41 25
            $subscriberClassReflectionObj = new \ReflectionClass($subscriberClass);
42
43 25
            if (!$subscriberClassReflectionObj->implementsInterface('JMS\Serializer\EventDispatcher\EventSubscriberInterface')) {
44 1
                throw new \RuntimeException(sprintf('The service "%s" (class: %s) does not implement the EventSubscriberInterface.', $id, $subscriberClass));
45
            }
46
47 24
            foreach (call_user_func(array($subscriberClass, 'getSubscribedEvents')) as $eventData) {
48 24
                if (!isset($eventData['event'])) {
49
                    throw new \RuntimeException(sprintf('The service "%s" (class: %s) must return an event for each subscribed event.', $id, $subscriberClass));
50
                }
51
52 24
                $class = isset($eventData['class']) ? strtolower($eventData['class']) : null;
53 24
                $format = isset($eventData['format']) ? $eventData['format'] : null;
54 24
                $method = isset($eventData['method']) ? $eventData['method'] : EventDispatcher::getDefaultMethodName($eventData['event']);
55 24
                $priority = isset($eventData['priority']) ? (integer)$eventData['priority'] : 0;
56
57 24 View Code Duplication
                if ($container->getDefinition($id)->isPublic()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 23
                    $listeners[$eventData['event']][$priority][] = array(array($id, $method), $class, $format);
59 23
                } else {
60 1
                    $listeners[$eventData['event']][$priority][] = array(array(new Reference($id), $method), $class, $format);
61
                }
62 24
            }
63 27
        }
64
65 27
        if ($listeners) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $listeners of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
66 27
            array_walk($listeners, function (&$value, $key) {
0 ignored issues
show
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67 27
                ksort($value);
68 27
            });
69
70 27
            foreach ($listeners as &$events) {
71 27
                $events = call_user_func_array('array_merge', $events);
72 27
            }
73
74 27
            $container->getDefinition('jms_serializer.event_dispatcher')
75 27
                ->addMethodCall('setListeners', array($listeners));
76 27
        }
77 27
    }
78
}
79