Parameter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 13
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
class Parameter extends ValueObject implements ParameterAggregate
11
{
12
    use Properties\OptionalDeprecationStatus;
13
    use Properties\OptionalDescription;
14
    use Properties\OptionalExamples;
15
    use Properties\OptionalMediaTypeContent;
16
    use Properties\OptionalExtensions;
17
18
    private $allowEmptyValue;
19
20
    private $allowReserved;
21
22
    private $explode;
23
24
    private $in;
25
26
    private $name;
27
28
    private $required;
29
30
    private $schema;
31
32
    private $style;
33
34 27
    public function __construct(
35
        string $name,
36
        string $in,
37
        SchemaAggregate $schema = null,
38
        MediaTypes $content = null,
39
        string $description = null,
40
        bool $required = null,
41
        bool $deprecated = false,
42
        bool $allowEmptyValue = false,
43
        string $style = null,
44
        bool $explode = null,
45
        bool $allowReserved = false,
46
        Examples $examples = null,
47
        Extensions $extensions = null
48
    ) {
49 27
        $this->name = $name;
50 27
        $this->in = $in;
51 27
        $this->schema = $schema;
52 27
        $this->content = $content;
53 27
        $this->description = $description;
54 27
        $this->required = $required;
55 27
        $this->deprecated = $deprecated;
56 27
        $this->allowEmptyValue = $allowEmptyValue;
57 27
        $this->style = $style;
58
        // TODO: The defaults is based on other fields.
59 27
        $this->explode = (bool) $explode;
60 27
        $this->allowReserved = $allowReserved;
61 27
        $this->examples = $examples;
62 27
        $this->extensions = $extensions;
63
    }
64
65 25
    public function getIn(): string
66
    {
67 25
        return $this->in;
68
    }
69
70 25
    public function getName(): string
71
    {
72 25
        return $this->name;
73
    }
74
75 10
    public function getSchema(): SchemaAggregate
76
    {
77 10
        return $this->schema;
78
    }
79
80 4
    public function getStyle(): string
81
    {
82 4
        return $this->style;
83
    }
84
85 25
    public function hasSchema(): bool
86
    {
87 25
        return isset($this->schema);
88
    }
89
90 25
    public function hasStyle(): bool
91
    {
92 25
        return isset($this->style);
93
    }
94
95 25
    public function isAllowEmptyValue(): bool
96
    {
97 25
        return $this->allowEmptyValue;
98
    }
99
100 25
    public function isAllowReserved(): bool
101
    {
102 25
        return $this->allowReserved;
103
    }
104
105 25
    public function isExplode(): bool
106
    {
107 25
        return $this->explode;
108
    }
109
110 4
    public function isRequired(): bool
111
    {
112 4
        return $this->required === true;
113
    }
114
115 25
    public function jsonSerialize(): ?array
116
    {
117 25
        return $this->extendedProperties(parent::jsonSerialize());
118
    }
119
120 25
    protected function normalizeOptionalProperties(): array
121
    {
122
        return [
123 25
            'schema' => $this->hasSchema() ? $this->getSchema()->jsonSerialize() : null,
124 25
            'content' => $this->getNormalizedContent(),
125 25
            'description' => $this->getNormalizedDescription(),
126 25
            'required' => $this->isRequiredSet() ? $this->isRequired() : null,
127 25
            'deprecated' => $this->isDeprecated() ?: null,
128 25
            'allowEmptyValue' => $this->isAllowEmptyValue() ?: null,
129 25
            'style' => $this->hasStyle() ? $this->getStyle() : null,
130 25
            'explode' => $this->isExplode() ?: null,
131 25
            'allowReserved' => $this->isAllowReserved() ?: null,
132 25
            'examples' => $this->hasExamples() ? $this->getExamples()->jsonSerialize() : null,
133
        ];
134
    }
135
136 25
    protected function normalizeRequiredProperties(): array
137
    {
138
        return [
139 25
            'name' => $this->getName(),
140 25
            'in' => $this->getIn(),
141
        ];
142
    }
143
144 25
    private function isRequiredSet(): bool
145
    {
146 25
        return isset($this->required);
147
    }
148
}
149