Path::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 9.392
c 0
b 0
f 0
cc 1
nc 1
nop 14
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aweapi\Openapi\Objects;
6
7
use Aweapi\Openapi\Extensions;
8
use Aweapi\Openapi\ValueObject;
9
10
final class Path extends ValueObject
11
{
12
    use Properties\OptionalDescription;
13
    use Properties\OptionalServers;
14
    use Properties\OptionalExtensions;
15
    use Properties\OptionalSummary;
16
    use Properties\OptionalParameterCollection;
17
18
    public const METHOD_DELETE = 'delete';
19
20
    public const METHOD_GET = 'get';
21
22
    public const METHOD_HEAD = 'head';
23
24
    public const METHOD_OPTIONS = 'options';
25
26
    public const METHOD_PATCH = 'patch';
27
28
    public const METHOD_POST = 'post';
29
30
    public const METHOD_PUT = 'put';
31
32
    public const METHOD_TRACE = 'trace';
33
34
    private $operations;
35
36
    private $ref;
37
38 16
    public function __construct(
39
        string $summary = null,
40
        Reference $ref = null,
41
        string $description = null,
42
        Operation $getOperation = null,
43
        Operation $putOperation = null,
44
        Operation $postOperation = null,
45
        Operation $deleteOperation = null,
46
        Operation $patchOperation = null,
47
        Operation $optionsOperation = null,
48
        Operation $headOperation = null,
49
        Operation $traceOperation = null,
50
        ServerCollection $servers = null,
51
        ParameterCollection $parameters = null,
52
        Extensions $extensions = null
53
    ) {
54 16
        $this->summary = $summary;
55 16
        $this->ref = $ref;
56 16
        $this->description = $description;
57 16
        $this->operations = array_filter([
58 16
            self::METHOD_GET => $getOperation,
59 16
            self::METHOD_PUT => $putOperation,
60 16
            self::METHOD_POST => $postOperation,
61 16
            self::METHOD_DELETE => $deleteOperation,
62 16
            self::METHOD_PATCH => $patchOperation,
63 16
            self::METHOD_OPTIONS => $optionsOperation,
64 16
            self::METHOD_HEAD => $headOperation,
65 16
            self::METHOD_TRACE => $traceOperation,
66
        ]);
67 16
        $this->servers = $servers;
68 16
        $this->parameters = $parameters;
69 16
        $this->extensions = $extensions;
70
    }
71
72 1
    public function getOperation(string $method): Operation
73
    {
74 1
        return $this->operations[mb_strtolower($method)];
75
    }
76
77 9
    public function getRef(): Reference
78
    {
79 9
        return $this->ref;
80
    }
81
82 1
    public function hasOperation(string $method): bool
83
    {
84 1
        return isset($this->operations[mb_strtolower($method)]);
85
    }
86
87 15
    public function hasRef(): bool
88
    {
89 15
        return isset($this->ref);
90
    }
91
92 15
    public function jsonSerialize(): ?array
93
    {
94 15
        return $this->extendedProperties(parent::jsonSerialize());
95
    }
96
97 15
    protected function normalizeOptionalProperties(): array
98
    {
99
        $properties = [
100 15
            'summary' => $this->getNormalizedSummary(),
101 15
            'description' => $this->getNormalizedDescription(),
102 15
            'servers' => $this->getNormalizedServers(),
103 15
            'parameters' => $this->getNormalizedParameters(),
104
        ];
105
106 15
        foreach ($this->getOperations() as $method => $operation) {
107 2
            $properties[$method] = $operation->jsonSerialize();
108
        }
109
110 15
        if ($this->hasRef()) {
111 9
            return (array) $this->getRef()->jsonSerialize() + $properties;
112
        }
113
114 6
        return $properties;
115
    }
116
117
    /**
118
     * @return Operation[]
119
     */
120 15
    private function getOperations(): array
121
    {
122 15
        return $this->operations;
123
    }
124
}
125