Completed
Push — master ( 111bcd...99ca87 )
by Andrey
26:30
created

createServiceWithName()   C

Complexity

Conditions 12
Paths 162

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 60
rs 5.7642
cc 12
eloc 33
nc 162
nop 3

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
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\Visitor;
7
8
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
use Zend\ServiceManager\AbstractFactoryInterface;
10
use Zend\ServiceManager\AbstractPluginManager;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Nnx\JmsSerializerModule\Options\ModuleOptions;
13
use JMS\Serializer\Construction\ObjectConstructorInterface;
14
use PhpCollection\Map;
15
use ReflectionClass;
16
use JMS\Serializer\VisitorInterface;
17
18
/**
19
 * Class VisitorsMapAbstractFactory
20
 *
21
 * @package Nnx\JmsSerializerModule\Visitor
22
 */
23
class VisitorsMapAbstractFactory implements AbstractFactoryInterface
24
{
25
    /**
26
     * @inheritdoc
27
     *
28
     * @param ServiceLocatorInterface $serviceLocator
29
     * @param                         $name
30
     * @param                         $requestedName
31
     *
32
     * @return bool|void
33
     */
34
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
35
    {
36
        return 0 === strpos($requestedName, 'nnxJmsSerializer.serializationVisitors.') || 0 === strpos($requestedName, 'nnxJmsSerializer.deserializationVisitors.default');
37
    }
38
39
    /**
40
     * @inheritdoc
41
     *
42
     * @param ServiceLocatorInterface $serviceLocator
43
     * @param                         $name
44
     * @param                         $requestedName
45
     *
46
     * @return ObjectConstructorInterface
47
     * @throws \Nnx\JmsSerializerModule\Visitor\Exception\RuntimeException
48
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
49
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
50
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
51
     */
52
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
53
    {
54
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
55
56
        $name = null;
57
58
        $isSerializationVisitors = 0 === strpos($requestedName, 'nnxJmsSerializer.serializationVisitors.');
59
        $isDeserializationVisitors = 0 === strpos($requestedName, 'nnxJmsSerializer.deserializationVisitors.default');
60
61
        if ($isSerializationVisitors) {
62
            $name = substr($requestedName, 39);
63
        } elseif ($isDeserializationVisitors) {
64
            $name = substr($requestedName, 41);
65
        }
66
67
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
68
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
69
70
        /** @var ModuleOptions $moduleOptions */
71
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
72
73
74
        $list = [];
75
76
        if ($isSerializationVisitors) {
77
            $list = $moduleOptions->getSerializationVisitor($name);
78
        } elseif ($isDeserializationVisitors) {
79
            $list = $moduleOptions->getDeserializationVisitor($name);
80
        }
81
82
        $map = new Map();
83
84
85
        foreach ($list as $format => $visitorName) {
0 ignored issues
show
Bug introduced by
The expression $list of type object<Nnx\JmsSerializer...ns\PluginOptions>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
86
            $visitor = null;
87
            if (is_string($visitorName)) {
88
                if ($serviceLocator->has($visitorName)) {
89
                    $visitor = $serviceLocator->get($visitorName);
90
                } elseif (class_exists($visitorName)) {
91
                    $r = new ReflectionClass($visitorName);
92
                    $visitor = $r->newInstance();
93
                }
94
            }
95
96
            if (!$visitor instanceof VisitorInterface) {
97
                $errMsg = sprintf(
98
                    'Visitor of type %s is invalid; must implement %s',
99
                    (is_object($visitor) ? get_class($visitor) : gettype($visitor)),
100
                    VisitorInterface::class
101
                );
102
                throw new Exception\RuntimeException($errMsg);
103
            }
104
105
106
            $map->set($format, $visitor);
107
        }
108
109
110
        return $map;
111
    }
112
}
113