Completed
Push — master ( d37f4d...5acbf0 )
by Asmir
10s
created

CustomHandlersPass   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 71
Duplicated Lines 25.35 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 94.23%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 5
dl 18
loc 71
ccs 49
cts 52
cp 0.9423
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 18 68 20

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JMS\SerializerBundle\DependencyInjection\Compiler;
4
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\HandlerRegistry;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class CustomHandlersPass implements CompilerPassInterface
13
{
14 33
    public function process(ContainerBuilder $container)
15
    {
16 33
        $handlers = array();
17 33
        $handlerServices = array();
18 33
        foreach ($container->findTaggedServiceIds('jms_serializer.handler') as $id => $tags) {
19 5
            foreach ($tags as $attrs) {
20 5
                if (!isset($attrs['type'], $attrs['format'])) {
21 1
                    throw new \RuntimeException(sprintf('Each tag named "jms_serializer.handler" of service "%s" must have at least two attributes: "type" and "format".', $id));
22
                }
23
24 4
                $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
25 4
                if (isset($attrs['direction'])) {
26 2
                    if (!defined($directionConstant = 'JMS\Serializer\GraphNavigator::DIRECTION_' . strtoupper($attrs['direction']))) {
27 1
                        throw new \RuntimeException(sprintf('The direction "%s" of tag "jms_serializer.handler" of service "%s" does not exist.', $attrs['direction'], $id));
28
                    }
29
30 1
                    $directions = array(constant($directionConstant));
31 1
                }
32
33 3 View Code Duplication
                foreach ($directions as $direction) {
0 ignored issues
show
Duplication introduced by
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...
34 3
                    $method = isset($attrs['method']) ? $attrs['method'] : HandlerRegistry::getDefaultMethod($direction, $attrs['type'], $attrs['format']);
35 3
                    if (class_exists(ServiceLocatorTagPass::class) || $container->getDefinition($id)->isPublic()) {
36 3
                        $handlerServices[$id] = new Reference($id);
37 3
                        $handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
38 3
                    } else {
39
                        $handlers[$direction][$attrs['type']][$attrs['format']] = array(new Reference($id), $method);
40
                    }
41 3
                }
42 3
            }
43 31
        }
44
45 31
        foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) {
46 28
            $class = $container->getDefinition($id)->getClass();
47 28
            $ref = new \ReflectionClass($class);
48 28
            if (!$ref->implementsInterface('JMS\Serializer\Handler\SubscribingHandlerInterface')) {
49 1
                throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id));
50
            }
51
52 27
            foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) {
53 27
                if (!isset($methodData['format'], $methodData['type'])) {
54
                    throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.', $id));
55
                }
56
57 27
                $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
58 27
                if (isset($methodData['direction'])) {
59 27
                    $directions = array($methodData['direction']);
60 27
                }
61
62 27 View Code Duplication
                foreach ($directions as $direction) {
0 ignored issues
show
Duplication introduced by
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...
63 27
                    $method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
64 27
                    if (class_exists(ServiceLocatorTagPass::class) || $container->getDefinition($id)->isPublic()) {
65 27
                        $handlerServices[$id] = new Reference($id);
66 27
                        $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
67 27
                    } else {
68
                        $handlers[$direction][$methodData['type']][$methodData['format']] = array(new Reference($id), $method);
69
                    }
70 27
                }
71 27
            }
72 30
        }
73
74 30
        $container->findDefinition('jms_serializer.handler_registry')
75 30
            ->addArgument($handlers);
76
77 30
        if (class_exists(ServiceLocatorTagPass::class)) {
78 30
            $serviceLocator = ServiceLocatorTagPass::register($container, $handlerServices);
79 30
            $container->findDefinition('jms_serializer.handler_registry')->replaceArgument(0, $serviceLocator);
80 30
        }
81 30
    }
82
}
83