PathAttribute   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 8.77 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 10
loc 114
ccs 36
cts 42
cp 0.8571
rs 10
c 0
b 0
f 0

9 Methods

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