Completed
Push — master ( 5dfaac...d37f4d )
by Asmir
16:56 queued 07:42
created

CustomHandlersPass::process()   D

Complexity

Conditions 20
Paths 314

Size

Total Lines 68
Code Lines 42

Duplication

Lines 18
Ratio 26.47 %

Code Coverage

Tests 48
CRAP Score 20.1819

Importance

Changes 0
Metric Value
dl 18
loc 68
ccs 48
cts 52
cp 0.9231
rs 4.2686
c 0
b 0
f 0
cc 20
eloc 42
nc 314
nop 1
crap 20.1819

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 32
    public function process(ContainerBuilder $container)
15
    {
16 32
        $handlers = array();
17 32
        $handlerServices = array();
18 32
        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 2
                        $handlerServices[$id] = new Reference($id);
37 2
                        $handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
38 2
                    } else {
39 1
                        $handlers[$direction][$attrs['type']][$attrs['format']] = array(new Reference($id), $method);
40
                    }
41 3
                }
42 3
            }
43 30
        }
44
45 30
        foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) {
46 27
            $class = $container->getDefinition($id)->getClass();
47 27
            $ref = new \ReflectionClass($class);
48 27
            if (!$ref->implementsInterface('JMS\Serializer\Handler\SubscribingHandlerInterface')) {
49 1
                throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id));
50
            }
51
52 26
            foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) {
53 26
                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 26
                $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
58 26
                if (isset($methodData['direction'])) {
59 26
                    $directions = array($methodData['direction']);
60 26
                }
61
62 26 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 26
                    $method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
64 26
                    if (class_exists(ServiceLocatorTagPass::class) || $container->getDefinition($id)->isPublic()) {
65 25
                        $handlerServices[$id] = new Reference($id);
66 25
                        $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
67 25
                    } else {
68 1
                        $handlers[$direction][$methodData['type']][$methodData['format']] = array(new Reference($id), $method);
69
                    }
70 26
                }
71 26
            }
72 29
        }
73
74 29
        $container->findDefinition('jms_serializer.handler_registry')
75 29
            ->addArgument($handlers);
76
77 29
        if (class_exists(ServiceLocatorTagPass::class)) {
78
            $serviceLocator = ServiceLocatorTagPass::register($container, $handlerServices);
79
            $container->findDefinition('jms_serializer.handler_registry')->replaceArgument(0, $serviceLocator);
80
        }
81 29
    }
82
}
83