Completed
Push — master ( 739ea2...dd79ae )
by Asmir
13s
created

CustomHandlersPass::process()   C

Complexity

Conditions 17
Paths 170

Size

Total Lines 60
Code Lines 36

Duplication

Lines 16
Ratio 26.67 %

Code Coverage

Tests 44
CRAP Score 17.0031

Importance

Changes 0
Metric Value
dl 16
loc 60
ccs 44
cts 45
cp 0.9778
rs 5.6996
c 0
b 0
f 0
cc 17
eloc 36
nc 170
nop 1
crap 17.0031

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