Completed
Pull Request — master (#542)
by Asmir
16:51
created

CustomHandlersPass::process()   C

Complexity

Conditions 15
Paths 82

Size

Total Lines 52
Code Lines 30

Duplication

Lines 8
Ratio 15.38 %

Code Coverage

Tests 23
CRAP Score 30.5412

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 8
loc 52
ccs 23
cts 39
cp 0.5897
rs 5.9385
cc 15
eloc 30
nc 82
nop 1
crap 30.5412

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