Conditions | 53 |
Paths | > 20000 |
Total Lines | 208 |
Code Lines | 122 |
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 |
||
44 | public function getTypeString($schema, $path = '') |
||
45 | { |
||
46 | if ($schema === null) { |
||
47 | return ''; |
||
48 | } |
||
49 | |||
50 | $schema = Schema::unboolSchema($schema); |
||
51 | |||
52 | $isOptional = false; |
||
53 | $isObject = false; |
||
54 | $isArray = false; |
||
55 | $isBoolean = false; |
||
56 | $isString = false; |
||
57 | $isNumber = false; |
||
58 | |||
59 | if ($schema->const !== null) { |
||
60 | return '`' . var_export($schema->const, true) . '`'; |
||
61 | } |
||
62 | |||
63 | if (!empty($schema->enum)) { |
||
64 | $res = ''; |
||
65 | foreach ($schema->enum as $value) { |
||
66 | $res .= '<br>`' . var_export($value, true) . '`, '; |
||
67 | } |
||
68 | return substr($res, 4, -2); |
||
69 | } |
||
70 | |||
71 | if (!empty($schema->getFromRefs())) { |
||
72 | $refs = $schema->getFromRefs(); |
||
73 | $path = $refs[0]; |
||
74 | } |
||
75 | |||
76 | $type = $schema->type; |
||
77 | if ($type === null) { |
||
|
|||
78 | $type = []; |
||
79 | |||
80 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
81 | $type[] = Schema::OBJECT; |
||
82 | } |
||
83 | |||
84 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
85 | $type[] = Schema::_ARRAY; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if (!is_array($type)) { |
||
90 | $type = [$type]; |
||
91 | } |
||
92 | |||
93 | $or = []; |
||
94 | |||
95 | if ($schema->oneOf !== null) { |
||
96 | foreach ($schema->oneOf as $i => $item) { |
||
97 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ($schema->anyOf !== null) { |
||
102 | foreach ($schema->anyOf as $i => $item) { |
||
103 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if ($schema->allOf !== null) { |
||
108 | foreach ($schema->allOf as $i => $item) { |
||
109 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($schema->then !== null) { |
||
114 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
115 | } |
||
116 | |||
117 | if ($schema->else !== null) { |
||
118 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
119 | } |
||
120 | |||
121 | foreach ($type as $i => $t) { |
||
122 | switch ($t) { |
||
123 | case Schema::NULL: |
||
124 | $isOptional = true; |
||
125 | break; |
||
126 | |||
127 | case Schema::OBJECT: |
||
128 | $isObject = true; |
||
129 | break; |
||
130 | |||
131 | case Schema::_ARRAY: |
||
132 | $isArray = true; |
||
133 | break; |
||
134 | |||
135 | case Schema::NUMBER: |
||
136 | case Schema::INTEGER: |
||
137 | $isNumber = true; |
||
138 | break; |
||
139 | |||
140 | case Schema::STRING: |
||
141 | $isString = true; |
||
142 | break; |
||
143 | |||
144 | case Schema::BOOLEAN: |
||
145 | $isBoolean = true; |
||
146 | break; |
||
147 | |||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | $namedTypeAdded = false; |
||
153 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
154 | if ($this->processed->contains($schema)) { |
||
155 | $or [] = $this->processed->offsetGet($schema); |
||
156 | $namedTypeAdded = true; |
||
157 | } else { |
||
158 | if ($schema instanceof Schema) { |
||
159 | $typeName = $this->typeName($schema, $path); |
||
160 | $this->makeTypeDef($schema, $path); |
||
161 | |||
162 | $or [] = $typeName; |
||
163 | $namedTypeAdded = true; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | if ($isObject) { |
||
169 | $typeAdded = false; |
||
170 | |||
171 | if ($namedTypeAdded) { |
||
172 | $typeAdded = true; |
||
173 | } |
||
174 | |||
175 | if ($schema->additionalProperties instanceof Schema) { |
||
176 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
177 | $or [] = "`Map<String,`$typeName`>`"; |
||
178 | $typeAdded = true; |
||
179 | } |
||
180 | |||
181 | if (!empty($schema->patternProperties)) { |
||
182 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
183 | if ($propertySchema instanceof Schema) { |
||
184 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
185 | $or [] = $typeName; |
||
186 | $typeAdded = true; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if (!$typeAdded) { |
||
192 | $or [] = '`Object`'; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | if ($isArray) { |
||
197 | $typeAdded = false; |
||
198 | |||
199 | if ($schema->items instanceof Schema) { |
||
200 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
201 | $or [] = "`Array<`$typeName`>`"; |
||
202 | $typeAdded = true; |
||
203 | } |
||
204 | |||
205 | if ($schema->additionalItems instanceof Schema) { |
||
206 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
207 | $or [] = "`Array<`$typeName`>`"; |
||
208 | $typeAdded = true; |
||
209 | } |
||
210 | |||
211 | if (!$typeAdded) { |
||
212 | $or [] = '`Array`'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ($isOptional) { |
||
217 | $or [] = '`null`'; |
||
218 | } |
||
219 | |||
220 | if ($isString) { |
||
221 | $or [] = '`String`'; |
||
222 | } |
||
223 | |||
224 | if ($isNumber) { |
||
225 | $or [] = '`Number`'; |
||
226 | } |
||
227 | |||
228 | if ($isBoolean) { |
||
229 | $or [] = '`Boolean`'; |
||
230 | } |
||
231 | |||
232 | if ($schema->format !== null) { |
||
233 | $or [] = 'Format: `' . $schema->format . '`'; |
||
234 | } |
||
235 | |||
236 | $res = ''; |
||
237 | foreach ($or as $item) { |
||
238 | if (!empty($item) && $item !== '*') { |
||
239 | $res .= ', ' . $item; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | if ($res !== '') { |
||
244 | $res = substr($res, 2); |
||
245 | } else { |
||
246 | $res = '`*`'; |
||
247 | } |
||
248 | |||
249 | $res = str_replace('``', '', $res); |
||
250 | |||
251 | return $res; |
||
252 | } |
||
482 | } |