Conditions | 52 |
Paths | > 20000 |
Total Lines | 204 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public function getTypeString($schema, $path = '') |
||
38 | { |
||
39 | if ($schema === null) { |
||
40 | return ''; |
||
41 | } |
||
42 | |||
43 | $schema = Schema::unboolSchema($schema); |
||
44 | |||
45 | $isOptional = false; |
||
46 | $isObject = false; |
||
47 | $isArray = false; |
||
48 | $isBoolean = false; |
||
49 | $isString = false; |
||
50 | $isNumber = false; |
||
51 | |||
52 | if ($schema->const !== null) { |
||
53 | return '`' . var_export($schema->const, true) . '`'; |
||
54 | } |
||
55 | |||
56 | if (!empty($schema->enum)) { |
||
57 | $res = ''; |
||
58 | foreach ($schema->enum as $value) { |
||
59 | $res .= '`' . var_export($value, true) . '`, '; |
||
60 | } |
||
61 | return substr($res, 0, -2); |
||
62 | } |
||
63 | |||
64 | if (!empty($schema->getFromRefs())) { |
||
65 | $refs = $schema->getFromRefs(); |
||
66 | $path = $refs[0]; |
||
67 | } |
||
68 | |||
69 | $type = $schema->type; |
||
70 | if ($type === null) { |
||
|
|||
71 | $type = []; |
||
72 | |||
73 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
74 | $type[] = Schema::OBJECT; |
||
75 | } |
||
76 | |||
77 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
78 | $type[] = Schema::_ARRAY; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if (!is_array($type)) { |
||
83 | $type = [$type]; |
||
84 | } |
||
85 | |||
86 | $or = []; |
||
87 | |||
88 | if ($schema->oneOf !== null) { |
||
89 | foreach ($schema->oneOf as $i => $item) { |
||
90 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | if ($schema->anyOf !== null) { |
||
95 | foreach ($schema->anyOf as $i => $item) { |
||
96 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if ($schema->allOf !== null) { |
||
101 | foreach ($schema->allOf as $i => $item) { |
||
102 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if ($schema->then !== null) { |
||
107 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
108 | } |
||
109 | |||
110 | if ($schema->else !== null) { |
||
111 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
112 | } |
||
113 | |||
114 | foreach ($type as $i => $t) { |
||
115 | switch ($t) { |
||
116 | case Schema::NULL: |
||
117 | $isOptional = true; |
||
118 | break; |
||
119 | |||
120 | case Schema::OBJECT: |
||
121 | $isObject = true; |
||
122 | break; |
||
123 | |||
124 | case Schema::_ARRAY: |
||
125 | $isArray = true; |
||
126 | break; |
||
127 | |||
128 | case Schema::NUMBER: |
||
129 | case Schema::INTEGER: |
||
130 | $isNumber = true; |
||
131 | break; |
||
132 | |||
133 | case Schema::STRING: |
||
134 | $isString = true; |
||
135 | break; |
||
136 | |||
137 | case Schema::BOOLEAN: |
||
138 | $isBoolean = true; |
||
139 | break; |
||
140 | |||
141 | } |
||
142 | } |
||
143 | |||
144 | |||
145 | $namedTypeAdded = false; |
||
146 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
147 | if ($this->processed->contains($schema)) { |
||
148 | $or [] = $this->processed->offsetGet($schema); |
||
149 | $namedTypeAdded = true; |
||
150 | } else { |
||
151 | if ($schema instanceof Schema) { |
||
152 | $typeName = $this->typeName($schema, $path); |
||
153 | $this->makeTypeDef($schema, $path); |
||
154 | |||
155 | $or [] = $typeName; |
||
156 | $namedTypeAdded = true; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if ($isObject) { |
||
162 | $typeAdded = false; |
||
163 | |||
164 | if ($namedTypeAdded) { |
||
165 | $typeAdded = true; |
||
166 | } |
||
167 | |||
168 | if ($schema->additionalProperties instanceof Schema) { |
||
169 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
170 | $or [] = "`Map<String,`$typeName`>`"; |
||
171 | $typeAdded = true; |
||
172 | } |
||
173 | |||
174 | if (!empty($schema->patternProperties)) { |
||
175 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
176 | if ($propertySchema instanceof Schema) { |
||
177 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
178 | $or [] = $typeName; |
||
179 | $typeAdded = true; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if (!$typeAdded) { |
||
185 | $or [] = '`Object`'; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if ($isArray) { |
||
190 | $typeAdded = false; |
||
191 | |||
192 | if ($schema->items instanceof Schema) { |
||
193 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
194 | $or [] = "`Array<`$typeName`>`"; |
||
195 | $typeAdded = true; |
||
196 | } |
||
197 | |||
198 | if ($schema->additionalItems instanceof Schema) { |
||
199 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
200 | $or [] = "`Array<`$typeName`>`"; |
||
201 | $typeAdded = true; |
||
202 | } |
||
203 | |||
204 | if (!$typeAdded) { |
||
205 | $or [] = '`Array`'; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | if ($isOptional) { |
||
210 | $or [] = '`null`'; |
||
211 | } |
||
212 | |||
213 | if ($isString) { |
||
214 | $or [] = '`String`'; |
||
215 | } |
||
216 | |||
217 | if ($isNumber) { |
||
218 | $or [] = '`Number`'; |
||
219 | } |
||
220 | |||
221 | if ($isBoolean) { |
||
222 | $or [] = '`Boolean`'; |
||
223 | } |
||
224 | |||
225 | $res = ''; |
||
226 | foreach ($or as $item) { |
||
227 | if (!empty($item) && $item !== '*') { |
||
228 | $res .= ', ' . $item; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | if ($res !== '') { |
||
233 | $res = substr($res, 2); |
||
234 | } else { |
||
235 | $res = '`*`'; |
||
236 | } |
||
237 | |||
238 | $res = str_replace('``', '', $res); |
||
239 | |||
240 | return $res; |
||
241 | } |
||
435 | } |