Body   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 29.73 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.5%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 33
loc 111
ccs 39
cts 40
cp 0.975
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A setDenormalizationType() 0 5 1
A setDenormalizationGroup() 0 5 1
A setParameterName() 0 5 1
A setOptional() 0 5 1
A isSeveralSupported() 0 4 1
A apply() 0 7 1
A resolveDenormalizationType() 18 18 3
A resolveIfBodyIsOptional() 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
2
declare(strict_types=1);
3
4
namespace Paysera\Bundle\ApiBundle\Annotation;
5
6
use Paysera\Bundle\ApiBundle\Entity\RestRequestOptions;
7
use Paysera\Bundle\ApiBundle\Exception\ConfigurationException;
8
use Paysera\Bundle\ApiBundle\Service\Annotation\ReflectionMethodWrapper;
9
10
/**
11
 * @Annotation
12
 * @Target({"METHOD"})
13
 */
14
class Body implements RestAnnotationInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $parameterName;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $denormalizationType;
25
26
    /**
27
     * @var string|null
28
     */
29
    private $denormalizationGroup;
30
31
    /**
32
     * @var bool|null
33
     */
34
    private $optional;
35
36 97 View Code Duplication
    public function __construct(array $options)
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...
37
    {
38 97
        $this->setParameterName($options['parameterName']);
39 97
        $this->setDenormalizationType($options['denormalizationType'] ?? null);
40 97
        $this->setDenormalizationGroup($options['denormalizationGroup'] ?? null);
41 97
        $this->setOptional($options['optional'] ?? null);
42 97
    }
43
44
    /**
45
     * @param string|null $denormalizationType
46
     * @return $this
47
     */
48 97
    private function setDenormalizationType($denormalizationType): self
49
    {
50 97
        $this->denormalizationType = $denormalizationType;
51 97
        return $this;
52
    }
53
54
    /**
55
     * @param string|null $denormalizationGroup
56
     * @return $this
57
     */
58 97
    public function setDenormalizationGroup($denormalizationGroup): self
59
    {
60 97
        $this->denormalizationGroup = $denormalizationGroup;
61 97
        return $this;
62
    }
63
64
    /**
65
     * @param string $parameterName
66
     * @return $this
67
     */
68 97
    private function setParameterName(string $parameterName): self
69
    {
70 97
        $this->parameterName = $parameterName;
71 97
        return $this;
72
    }
73
74
    /**
75
     * @param bool|null $optional
76
     * @return $this
77
     */
78 97
    private function setOptional($optional): self
79
    {
80 97
        $this->optional = $optional;
81 97
        return $this;
82
    }
83
84 97
    public function isSeveralSupported(): bool
85
    {
86 97
        return false;
87
    }
88
89 97
    public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod)
90
    {
91 97
        $options->setBodyParameterName($this->parameterName);
92 97
        $options->setBodyDenormalizationType($this->resolveDenormalizationType($reflectionMethod));
93 96
        $options->setBodyDenormalizationGroup($this->denormalizationGroup);
94 96
        $options->setBodyOptional($this->resolveIfBodyIsOptional($reflectionMethod));
95 96
    }
96
97 97 View Code Duplication
    private function resolveDenormalizationType(ReflectionMethodWrapper $reflectionMethod): 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...
98
    {
99 97
        if ($this->denormalizationType !== null) {
100 96
            return $this->denormalizationType;
101
        }
102
103
        try {
104 97
            $typeName = $reflectionMethod->getNonBuiltInTypeForParameter($this->parameterName);
105 1
        } catch (ConfigurationException $exception) {
106 1
            throw new ConfigurationException(sprintf(
107 1
                'Denormalization type could not be guessed for %s in %s',
108 1
                '$' . $this->parameterName,
109 1
                $reflectionMethod->getFriendlyName()
110
            ));
111
        }
112
113 96
        return $typeName;
114
    }
115
116 96 View Code Duplication
    private function resolveIfBodyIsOptional(ReflectionMethodWrapper $reflectionMethod): bool
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...
117
    {
118 96
        if ($this->optional !== null) {
119
            return $this->optional;
120
        }
121
122 96
        return $reflectionMethod->getParameterByName($this->parameterName)->isDefaultValueAvailable();
123
    }
124
}
125