ObjectConstructorAbstractFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 62
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 4 4 1
B createServiceWithName() 33 33 4

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
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\ObjectConstructor;
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
15
16
/**
17
 * Class ObjectConstructorAbstractFactory
18
 *
19
 * @package Nnx\JmsSerializerModule\ObjectConstructor
20
 */
21 View Code Duplication
class ObjectConstructorAbstractFactory implements AbstractFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
22
{
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     * @param                         $name
28
     * @param                         $requestedName
29
     *
30
     * @return bool|void
31
     */
32
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
33
    {
34
        return 0 === strpos($requestedName, 'nnxJmsSerializer.objectConstructor.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
44
     * @return ObjectConstructorInterface
45
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
46
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
47
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $objectConstructorName = substr($requestedName, 35);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $objectConstructorConfig = $moduleOptions->getObjectConstructor($objectConstructorName);
62
63
        $name = $objectConstructorConfig->getName();
64
        $options = $objectConstructorConfig->getOptions();
65
66
        $objectConstructor =  $serviceLocator->get(
67
            $name,
68
            $options
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
        );
70
71
        if (!$objectConstructor instanceof ObjectConstructorInterface) {
72
            $errMsg = sprintf(
73
                'Object constructor of type %s is invalid; must implement %s',
74
                (is_object($objectConstructor) ? get_class($objectConstructor) : gettype($objectConstructor)),
75
                ObjectConstructorInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $objectConstructor;
81
    }
82
}
83