| 
                    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) { | 
            
                            
                    | 
                        
                     | 
                     | 
                     | 
                    
                                                                                                    
                        
                         
                                                                                        
                                                                                     
                     | 
                
            
                                                        
            
                                    
            
            
                | 
                    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
                 | 
                                    
                             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) { | 
            
                            
                    | 
                        
                     | 
                     | 
                     | 
                    
                                                                                                    
                        
                         
                                                                                        
                                                                                     
                     | 
                
            
                                                        
            
                                    
            
            
                | 
                    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
                 | 
                                    
                             26                          | 
                
                 | 
                                        $handlerServices[$id] = new Reference($id);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    66
                 | 
                                    
                             26                          | 
                
                 | 
                                        $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    67
                 | 
                                    
                             26                          | 
                
                 | 
                                    } else { | 
            
            
                                                        
            
                                    
            
            
                | 
                    68
                 | 
                                    
                                                     | 
                
                 | 
                                        $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                          | 
                
                View Code Duplication | 
                        if (class_exists(ServiceLocatorTagPass::class)) { | 
            
                            
                    | 
                        
                     | 
                     | 
                     | 
                    
                                                                                                    
                        
                         
                                                                                        
                                                                                     
                     | 
                
            
                                                        
            
                                    
            
            
                | 
                    78
                 | 
                                    
                             29                          | 
                
                 | 
                            $serviceLocator = ServiceLocatorTagPass::register($container, $handlerServices);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    79
                 | 
                                    
                             29                          | 
                
                 | 
                            $container->getDefinition('jms_serializer.handler_registry')->replaceArgument(0, $serviceLocator); | 
            
            
                                                        
            
                                    
            
            
                | 
                    80
                 | 
                                    
                             29                          | 
                
                 | 
                        }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    81
                 | 
                                    
                             29                          | 
                
                 | 
                    }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    82
                 | 
                                    
                                                     | 
                
                 | 
                }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    83
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                        
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.