Completed
Push — master ( 27a5d2...ad3b69 )
by John
20s
created

SerializerTypeDefinitionMap   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 28.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 0
dl 34
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDefinitionByOperationId() 0 8 2
A getDefinitionByType() 8 8 2
A getDefinitionByFqcn() 10 10 2
A getFqcn() 0 16 4
A getTypeNameByOperationId() 8 8 2
A getType() 8 8 2

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 declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\SwaggerBundle\Serialize\TypeResolver;
9
10
use KleijnWeb\SwaggerBundle\Document\Specification;
11
12
/**
13
 * @author John Kleijn <[email protected]>
14
 */
15
class SerializerTypeDefinitionMap
16
{
17
    /**
18
     * @var array
19
     */
20
    private $typeNames = [];
21
22
    /**
23
     * @var array
24
     */
25
    private $definitions;
26
27
    /**
28
     * SerializerTypeDefinitionMap constructor.
29
     *
30
     * @param array $typeNames
31
     * @param array $definitions
32
     */
33
    public function __construct(array $definitions, $typeNames)
34
    {
35
        $this->typeNames   = $typeNames;
36
        $this->definitions = $definitions;
37
    }
38
39
    /**
40
     * @param string $operationId
41
     *
42
     * @return \stdClass
43
     */
44
    public function getDefinitionByOperationId(string $operationId): \stdClass
45
    {
46
        if (!isset($this->definitions["_op_$operationId"])) {
47
            throw new \InvalidArgumentException("Operation '$operationId' not in definition map");
48
        }
49
50
        return $this->definitions["_op_$operationId"];
51
    }
52
53
    /**
54
     * @param string $type
55
     *
56
     * @return \stdClass
57
     */
58 View Code Duplication
    public function getDefinitionByType(string $type): \stdClass
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60
        if (!isset($this->definitions[$type])) {
61
            throw new \InvalidArgumentException("Type '$type' not in definition map");
62
        }
63
64
        return $this->definitions[$type];
65
    }
66
67
    /**
68
     * @param string $fqdn
69
     *
70
     * @return \stdClass
71
     */
72 View Code Duplication
    public function getDefinitionByFqcn(string $fqdn): \stdClass
0 ignored issues
show
Duplication introduced by
This method 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...
73
    {
74
        $type = $this->getType($fqdn);
75
76
        if (!isset($this->definitions[$type])) {
77
            throw new \InvalidArgumentException("Type '$type' not in definition map");
78
        }
79
80
        return $this->definitions[$type];
81
    }
82
83
    /**
84
     * @param string $type
85
     *
86
     * @return string
87
     */
88
    public function getFqcn(string $type): string
89
    {
90
        $lookup = [];
91
        foreach ($this->typeNames as $key => $typeName) {
92
            if (0 === strpos($key, "_op_")) {
93
                continue;
94
            }
95
            $lookup[$typeName] = $key;
96
        }
97
98
        if (!isset($lookup[$type])) {
99
            throw new \InvalidArgumentException("Class '$type' not in type map");
100
        }
101
102
        return $lookup[$type];
103
    }
104
105
    /**
106
     * @param string $operationId
107
     *
108
     * @return string
109
     */
110 View Code Duplication
    public function getTypeNameByOperationId(string $operationId): string
0 ignored issues
show
Duplication introduced by
This method 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...
111
    {
112
        if (!isset($this->typeNames["_op_$operationId"])) {
113
            throw new \InvalidArgumentException("Operation '$operationId' not in type map");
114
        }
115
116
        return $this->typeNames["_op_$operationId"];
117
    }
118
119
    /**
120
     * @param string $fqdn
121
     *
122
     * @return string
123
     */
124 View Code Duplication
    private function getType(string $fqdn): string
0 ignored issues
show
Duplication introduced by
This method 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...
125
    {
126
        if (!isset($this->typeNames[$fqdn])) {
127
            throw new \InvalidArgumentException("Type '$fqdn' not in type map");
128
        }
129
130
        return $this->typeNames[$fqdn];
131
    }
132
}
133