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

CustomHandlersPass   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 55
Duplicated Lines 14.55 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 58.97%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 15
c 2
b 1
f 0
lcom 0
cbo 3
dl 8
loc 55
ccs 23
cts 39
cp 0.5897
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 8 52 15

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\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