Completed
Pull Request — master (#3)
by Guilh
02:22
created

Schema::getDiscriminator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace gossi\swagger;
4
5
use gossi\swagger\parts\TypePart;
6
use gossi\swagger\parts\DescriptionPart;
7
use gossi\swagger\parts\ItemsPart;
8
use gossi\swagger\parts\ExtensionPart;
9
use gossi\swagger\parts\ExternalDocsPart;
10
use phootwork\collection\CollectionUtils;
11
use gossi\swagger\parts\RefPart;
12
use phootwork\collection\ArrayList;
13
use gossi\swagger\collections\Definitions;
14
use phootwork\lang\Arrayable;
15
use phootwork\collection\Map;
16
17
class Schema extends AbstractModel implements Arrayable
18
{
19
    use RefPart;
20
    use TypePart;
21
    use DescriptionPart;
22
    use ItemsPart;
23
    use ExternalDocsPart;
24
    use ExtensionPart;
25
26
    /** @var string */
27
    private $discriminator;
28
29
    /** @var bool */
30
    private $readOnly = false;
31
32
    /** @var string */
33
    private $title;
34
35
    private $xml;
0 ignored issues
show
Unused Code introduced by
The property $xml is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /** @var string */
38
    private $example;
39
40
    /** @var ArrayList|bool */
41
    private $required;
42
43
    /** @var Definitions */
44
    private $properties;
45
46
    /** @var ArrayList */
47
    private $allOf;
48
49
    /** @var Schema */
50
    private $additionalProperties;
51
52 9
    public function __construct($contents = null)
53
    {
54 9
        $this->parse($contents === null ? new Map() : $contents);
55 9
    }
56
57 9
    private function parse($contents = [])
58
    {
59 9
        $data = CollectionUtils::toMap($contents);
60
61 9
        $this->title = $data->get('title');
62 9
        $this->discriminator = $data->get('discriminator');
63 9
        $this->readOnly = $data->has('readOnly') && $data->get('readOnly');
64 9
        $this->example = $data->get('example');
65 9
        $this->required = $data->get('required');
66 9
        $this->properties = new Definitions($data->get('properties'));
67 9
        if ($data->has('additionalProperties')) {
68
            $this->additionalProperties = new self($data->get('additionalProperties'));
69 9
        }
70
71 9
        $this->allOf = new ArrayList();
72 9
        if ($data->has('allOf')) {
73 3
            foreach ($data->get('allOf') as $schema) {
74 3
                $this->allOf->add(new self($schema));
75 3
            }
76 3
        }
77
78
        // parts
79 9
        $this->parseRef($data);
80 9
        $this->parseType($data);
81 9
        $this->parseDescription($data);
82 9
        $this->parseItems($data);
83 9
        $this->parseExternalDocs($data);
84 9
        $this->parseExtensions($data);
85 9
    }
86
87 6
    public function toArray()
88
    {
89 6
        return $this->export('title', 'discriminator', 'description', 'readOnly', 'example',
90 6
                'externalDocs', $this->getTypeExportFields(), 'items', 'required',
91 6
                'properties', 'additionalProperties', 'allOf');
92
    }
93
94
    /**
95
     * @return bool|array
96
     */
97
    public function getRequired()
98
    {
99
        return $this->required;
100
    }
101
102
    /**
103
     * @param bool|array $required
104
     *
105
     * @return $this
106
     */
107
    public function setRequired($required)
108
    {
109
        $this->required = $required;
0 ignored issues
show
Documentation Bug introduced by
It seems like $required can also be of type array. However, the property $required is declared as type object<phootwork\collection\ArrayList>|boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getDiscriminator()
118
    {
119
        return $this->discriminator;
120
    }
121
122
    /**
123
     * @param string $discriminator
124
     */
125
    public function setDiscriminator($discriminator)
126
    {
127
        $this->discriminator = $discriminator;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isReadOnly()
136
    {
137
        return $this->readOnly;
138
    }
139
140
    /**
141
     * @param bool $readOnly
142
     */
143
    public function setReadOnly($readOnly)
144
    {
145
        $this->readOnly = $readOnly;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getExample()
154
    {
155
        return $this->example;
156
    }
157
158
    /**
159
     * @param string $example
160
     */
161
    public function setExample($example)
162
    {
163
        $this->example = $example;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getTitle()
172
    {
173
        return $this->title;
174
    }
175
176
    /**
177
     * @param string $title
178
     *
179
     * @return $this
180
     */
181
    public function setTitle($title)
182
    {
183
        $this->title = $title;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return Definitions
190
     */
191
    public function getProperties()
192
    {
193
        return $this->properties;
194
    }
195
196
    /**
197
     * @return ArrayList
198
     */
199
    public function getAllOf()
200
    {
201
        return $this->allOf;
202
    }
203
204
    /**
205
     * @return Schema
206
     */
207
    public function getAdditionalProperties()
208
    {
209
        if ($this->additionalProperties === null) {
210
            $this->additionalProperties = new self();
211
        }
212
213
        return $this->additionalProperties;
214
    }
215
}
216