Conditions | 12 |
Paths | 162 |
Total Lines | 60 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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) { |
||
|
|||
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 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.