Completed
Push — master ( 10a146...62fa38 )
by Thomas
8s
created

Schema::parse()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5.0144

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 22
cts 24
cp 0.9167
rs 8.439
cc 5
eloc 20
nc 8
nop 1
crap 5.0144
1
<?php
2
namespace gossi\swagger;
3
4
use gossi\swagger\parts\TypePart;
5
use gossi\swagger\parts\DescriptionPart;
6
use gossi\swagger\parts\ItemsPart;
7
use gossi\swagger\parts\ExtensionPart;
8
use gossi\swagger\parts\ExternalDocsPart;
9
use phootwork\collection\CollectionUtils;
10
use gossi\swagger\parts\RefPart;
11
use phootwork\collection\ArrayList;
12
use gossi\swagger\collections\Definitions;
13
use phootwork\lang\Arrayable;
14
use phootwork\collection\Map;
15
16
class Schema extends AbstractModel implements Arrayable {
17
	
18
	use RefPart;
19
	use TypePart;
20
	use DescriptionPart;
21
	use ItemsPart;
22
	use ExternalDocsPart;
23
	use ExtensionPart;
24
	
25
	/** @var string */
26
	private $discriminator;
27
	
28
	/** @var boolean */
29
	private $readOnly = false;
30
	
31
	/** @var string */
32
	private $title;
33
	
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|boolean */
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 9
		$this->parse($contents === null ? new Map() : $contents);
54 9
	}
55
	
56 9
	private function parse($contents = []) {
57 9
		$data = CollectionUtils::toMap($contents);
58
59 9
		$this->title = $data->get('title');
60 9
		$this->discriminator = $data->get('discriminator');
61 9
		$this->readOnly = $data->has('readOnly') && $data->get('readOnly');
62 9
		$this->example = $data->get('example');
63 9
		$this->required = $data->get('required');
64 9
		$this->properties = new Definitions($data->get('properties'));
65 9
		if ($data->has('additionalProperties')) {
66
			$this->additionalProperties = new Schema($data->get('additionalProperties'));
67
		}
68
		
69 9
		$this->allOf = new ArrayList();
70 9
		if ($data->has('allOf')) {
71 3
			foreach ($data->get('allOf') as $schema) {
72 3
				$this->allOf->add(new Schema($schema));
73 3
			}
74 3
		}
75
		
76
		// parts
77 9
		$this->parseRef($data);
78 9
		$this->parseType($data);
79 9
		$this->parseDescription($data);
80 9
		$this->parseItems($data);
81 9
		$this->parseExternalDocs($data);
82 9
		$this->parseExtensions($data);
83 9
	}
84
	
85 6
	public function toArray() {
86 6
		return $this->export('title', 'discriminator', 'description', 'readOnly', 'example', 
87 6
				'externalDocs', $this->getTypeExportFields(), 'items', 'required',
88 6
				'properties', 'additionalProperties', 'allOf');
89
	}
90
	
91
	/**
92
	 *
93
	 * @return boolean|array
94
	 */
95
	public function getRequired() {
96
		return $this->required;
97
	}
98
	
99
	/**
100
	 *
101
	 * @param boolean|array $required
102
	 * @return $this
103
	 */
104
	public function setRequired($required) {
105
		$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...
106
		return $this;
107
	}
108
	
109
	/**
110
	 *
111
	 * @return string
112
	 */
113
	public function getDiscriminator() {
114
		return $this->discriminator;
115
	}
116
	
117
	/**
118
	 *
119
	 * @param string $discriminator
120
	 */
121
	public function setDiscriminator($discriminator) {
122
		$this->discriminator = $discriminator;
123
		return $this;
124
	}
125
	
126
	/**
127
	 *
128
	 * @return boolean
129
	 */
130
	public function isReadOnly() {
131
		return $this->readOnly;
132
	}
133
	
134
	/**
135
	 *
136
	 * @param boolean $readOnly
137
	 */
138
	public function setReadOnly($readOnly) {
139
		$this->readOnly = $readOnly;
140
		return $this;
141
	}
142
	
143
	/**
144
	 *
145
	 * @return string
146
	 */
147
	public function getExample() {
148
		return $this->example;
149
	}
150
	
151
	/**
152
	 *
153
	 * @param string $example
154
	 */
155
	public function setExample($example) {
156
		$this->example = $example;
157
		return $this;
158
	}
159
	
160
	/**
161
	 *
162
	 * @return string
163
	 */
164
	public function getTitle() {
165
		return $this->title;
166
	}
167
	
168
	/**
169
	 *
170
	 * @param string $title
171
	 * @return $this
172
	 */
173
	public function setTitle($title) {
174
		$this->title = $title;
175
		return $this;
176
	}
177
	
178
	/**
179
	 *
180
	 * @return Definitions
181
	 */
182
	public function getProperties() {
183
		return $this->properties;
184
	}
185
	
186
	/**
187
	 *
188
	 * @return ArrayList
189
	 */
190
	public function getAllOf() {
191
		return $this->allOf;
192
	}
193
	
194
	/**
195
	 *
196
	 * @return Schema
197
	 */
198
	public function getAdditionalProperties() {
199
		if ($this->additionalProperties === null) {
200
			$this->additionalProperties = new Schema();
201
		}
202
		return $this->additionalProperties;
203
	}
204
205
}