Conditions | 37 |
Paths | > 20000 |
Total Lines | 158 |
Code Lines | 95 |
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 |
||
30 | public function getTypeString($schema, $path = '') |
||
31 | { |
||
32 | $schema = Schema::unboolSchema($schema); |
||
33 | |||
34 | $isOptional = false; |
||
|
|||
35 | $isObject = false; |
||
36 | $isArray = false; |
||
37 | $isBoolean = false; |
||
38 | $isString = false; |
||
39 | $isNumber = false; |
||
40 | |||
41 | $type = $schema->type; |
||
42 | if (!is_array($type)) { |
||
43 | $type = [$type]; |
||
44 | } |
||
45 | |||
46 | $or = []; |
||
47 | |||
48 | if ($schema->oneOf !== null) { |
||
49 | foreach ($schema->oneOf as $item) { |
||
50 | $or[] = $this->getTypeString($item); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if ($schema->anyOf !== null) { |
||
55 | foreach ($schema->anyOf as $item) { |
||
56 | $or[] = $this->getTypeString($item); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($schema->allOf !== null) { |
||
61 | foreach ($schema->allOf as $item) { |
||
62 | $or[] = $this->getTypeString($item); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($schema->then !== null) { |
||
67 | $or[] = $this->getTypeString($schema->then); |
||
68 | } |
||
69 | |||
70 | if ($schema->else !== null) { |
||
71 | $or[] = $this->getTypeString($schema->else); |
||
72 | } |
||
73 | |||
74 | foreach ($type as $i => $t) { |
||
75 | switch ($t) { |
||
76 | case Schema::NULL: |
||
77 | $isOptional = true; |
||
78 | break; |
||
79 | |||
80 | case Schema::OBJECT: |
||
81 | $isObject = true; |
||
82 | break; |
||
83 | |||
84 | case Schema::_ARRAY: |
||
85 | $isArray = true; |
||
86 | break; |
||
87 | |||
88 | case Schema::NUMBER: |
||
89 | case Schema::INTEGER: |
||
90 | $isNumber = true; |
||
91 | break; |
||
92 | |||
93 | case Schema::STRING: |
||
94 | $isString = true; |
||
95 | break; |
||
96 | |||
97 | case Schema::BOOLEAN: |
||
98 | $isBoolean = true; |
||
99 | break; |
||
100 | |||
101 | } |
||
102 | } |
||
103 | |||
104 | if ($isObject) { |
||
105 | $typeAdded = false; |
||
106 | |||
107 | if (!empty($schema->properties)) { |
||
108 | if ($this->processed->contains($schema)) { |
||
109 | $or [] = $this->processed->offsetGet($schema); |
||
110 | $typeAdded = true; |
||
111 | } else { |
||
112 | $typeName = $this->typeName($schema, $path); |
||
113 | $this->makeObjectTypeDef($schema, $path); |
||
114 | |||
115 | $or [] = $typeName; |
||
116 | $typeAdded = true; |
||
117 | } |
||
118 | |||
119 | } |
||
120 | |||
121 | if ($schema->additionalProperties instanceof Schema) { |
||
122 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
123 | $or [] = "object<string, $typeName>"; |
||
124 | $typeAdded = true; |
||
125 | } |
||
126 | |||
127 | if (!empty($schema->patternProperties)) { |
||
128 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
129 | if ($propertySchema instanceof Schema) { |
||
130 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
131 | $or [] = $typeName; |
||
132 | $typeAdded = true; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if (!$typeAdded) { |
||
138 | $or [] = 'object'; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | if ($isArray) { |
||
143 | $typeAdded = false; |
||
144 | |||
145 | if ($schema->items instanceof Schema) { |
||
146 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
147 | $or [] = "array<$typeName>"; |
||
148 | $typeAdded = true; |
||
149 | } |
||
150 | |||
151 | if ($schema->additionalItems instanceof Schema) { |
||
152 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
153 | $or [] = "array<$typeName>"; |
||
154 | $typeAdded = true; |
||
155 | } |
||
156 | |||
157 | if (!$typeAdded) { |
||
158 | $or [] = 'array'; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if ($isString) { |
||
163 | $or [] = 'string'; |
||
164 | } |
||
165 | |||
166 | if ($isNumber) { |
||
167 | $or [] = 'number'; |
||
168 | } |
||
169 | |||
170 | if ($isBoolean) { |
||
171 | $or [] = 'boolean'; |
||
172 | } |
||
173 | |||
174 | $res = ''; |
||
175 | foreach ($or as $item) { |
||
176 | if (!empty($item) && $item !== '*') { |
||
177 | $res .= '|' . $item; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | if ($res !== '') { |
||
182 | $res = substr($res, 1); |
||
183 | } else { |
||
184 | $res = '*'; |
||
185 | } |
||
186 | |||
187 | return $res; |
||
188 | } |
||
258 | } |