Completed
Push — master ( 935a4f...80b369 )
by Veaceslav
01:53
created

Path::__construct()   A

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 9
    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 9
        $this->summary = $summary;
55 9
        $this->ref = $ref;
56 9
        $this->description = $description;
57 9
        $this->operations = array_filter([
58 9
            self::METHOD_GET => $getOperation,
59 9
            self::METHOD_PUT => $putOperation,
60 9
            self::METHOD_POST => $postOperation,
61 9
            self::METHOD_DELETE => $deleteOperation,
62 9
            self::METHOD_PATCH => $patchOperation,
63 9
            self::METHOD_OPTIONS => $optionsOperation,
64 9
            self::METHOD_HEAD => $headOperation,
65 9
            self::METHOD_TRACE => $traceOperation,
66
        ]);
67 9
        $this->servers = $servers;
68 9
        $this->parameters = $parameters;
69 9
        $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 5
    public function getRef(): Reference
78
    {
79 5
        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 8
    public function hasRef(): bool
88
    {
89 8
        return isset($this->ref);
90
    }
91
92 8
    public function jsonSerialize(): ?array
93
    {
94 8
        return $this->extendedProperties(parent::jsonSerialize());
95
    }
96
97 8
    protected function normalizeOptionalProperties(): array
98
    {
99
        $properties = [
100 8
            'summary' => $this->getNormalizedSummary(),
101 8
            'description' => $this->getNormalizedDescription(),
102 8
            'servers' => $this->getNormalizedServers(),
103 8
            'parameters' => $this->getNormalizedParameters(),
104
        ];
105
106 8
        foreach ($this->getOperations() as $method => $operation) {
107 1
            $properties[$method] = $operation->jsonSerialize();
108
        }
109
110 8
        if ($this->hasRef()) {
111 5
            return (array) $this->getRef()->jsonSerialize() + $properties;
112
        }
113
114 3
        return $properties;
115
    }
116
117
    /**
118
     * @return Operation[]
119
     */
120 8
    private function getOperations(): array
121
    {
122 8
        return $this->operations;
123
    }
124
}
125