Total Complexity | 49 |
Total Lines | 429 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Schema |
||
15 | { |
||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $elementsQualification = false; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $attributesQualification = false; |
||
25 | |||
26 | /** |
||
27 | * @var string|null |
||
28 | */ |
||
29 | protected $targetNamespace; |
||
30 | |||
31 | /** |
||
32 | * @var Schema[] |
||
33 | */ |
||
34 | protected $schemas = array(); |
||
35 | |||
36 | /** |
||
37 | * @var Type[] |
||
38 | */ |
||
39 | protected $types = array(); |
||
40 | |||
41 | /** |
||
42 | * @var ElementDef[] |
||
43 | */ |
||
44 | protected $elements = array(); |
||
45 | |||
46 | /** |
||
47 | * @var Group[] |
||
48 | */ |
||
49 | protected $groups = array(); |
||
50 | |||
51 | /** |
||
52 | * @var AttributeGroup[] |
||
53 | */ |
||
54 | protected $attributeGroups = array(); |
||
55 | |||
56 | /** |
||
57 | * @var AttributeDef[] |
||
58 | */ |
||
59 | protected $attributes = array(); |
||
60 | |||
61 | /** |
||
62 | * @var string|null |
||
63 | */ |
||
64 | protected $doc; |
||
65 | |||
66 | /** |
||
67 | * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem[] |
||
68 | */ |
||
69 | private $typeCache = array(); |
||
70 | |||
71 | /** |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function getElementsQualification() |
||
75 | { |
||
76 | return $this->elementsQualification; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param bool $elementsQualification |
||
81 | */ |
||
82 | public function setElementsQualification($elementsQualification) |
||
83 | { |
||
84 | $this->elementsQualification = $elementsQualification; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function getAttributesQualification() |
||
91 | { |
||
92 | return $this->attributesQualification; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param bool $attributesQualification |
||
97 | */ |
||
98 | public function setAttributesQualification($attributesQualification) |
||
99 | { |
||
100 | $this->attributesQualification = $attributesQualification; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return string|null |
||
105 | */ |
||
106 | public function getTargetNamespace() |
||
107 | { |
||
108 | return $this->targetNamespace; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string|null $targetNamespace |
||
113 | */ |
||
114 | public function setTargetNamespace($targetNamespace) |
||
115 | { |
||
116 | $this->targetNamespace = $targetNamespace; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return Type[] |
||
121 | */ |
||
122 | public function getTypes() |
||
123 | { |
||
124 | return $this->types; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return ElementDef[] |
||
129 | */ |
||
130 | public function getElements() |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return Schema[] |
||
137 | */ |
||
138 | public function getSchemas() |
||
139 | { |
||
140 | return $this->schemas; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return AttributeDef[] |
||
145 | */ |
||
146 | public function getAttributes() |
||
147 | { |
||
148 | return $this->attributes; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return Group[] |
||
153 | */ |
||
154 | public function getGroups() |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return string|null |
||
161 | */ |
||
162 | public function getDoc() |
||
163 | { |
||
164 | return $this->doc; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param string $doc |
||
169 | */ |
||
170 | public function setDoc($doc) |
||
173 | } |
||
174 | |||
175 | public function addType(Type $type) |
||
176 | { |
||
177 | $this->types[$type->getName()] = $type; |
||
178 | } |
||
179 | |||
180 | public function addElement(ElementDef $element) |
||
181 | { |
||
182 | $this->elements[$element->getName()] = $element; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string|null $namespace |
||
187 | */ |
||
188 | public function addSchema(Schema $schema, $namespace = null) |
||
189 | { |
||
190 | if ($namespace !== null && $schema->getTargetNamespace() !== $namespace) { |
||
191 | throw new SchemaException(sprintf("The target namespace ('%s') for schema, does not match the declared namespace '%s'", $schema->getTargetNamespace(), $namespace)); |
||
192 | } |
||
193 | |||
194 | if ($namespace !== null) { |
||
195 | $this->schemas[$namespace] = $schema; |
||
196 | } else { |
||
197 | $this->schemas[] = $schema; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | public function addAttribute(AttributeDef $attribute) |
||
202 | { |
||
203 | $this->attributes[$attribute->getName()] = $attribute; |
||
204 | } |
||
205 | |||
206 | public function addGroup(Group $group) |
||
207 | { |
||
208 | $this->groups[$group->getName()] = $group; |
||
209 | } |
||
210 | |||
211 | public function addAttributeGroup(AttributeGroup $group) |
||
212 | { |
||
213 | $this->attributeGroups[$group->getName()] = $group; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return AttributeGroup[] |
||
218 | */ |
||
219 | public function getAttributeGroups() |
||
220 | { |
||
221 | return $this->attributeGroups; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * |
||
226 | * @param string $name |
||
227 | * @return Group|false |
||
228 | */ |
||
229 | public function getGroup($name) |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * |
||
239 | * @param string $name |
||
240 | * @return ElementItem|false |
||
241 | */ |
||
242 | public function getElement($name) |
||
243 | { |
||
244 | if (isset($this->elements[$name])) { |
||
245 | return $this->elements[$name]; |
||
246 | } |
||
247 | return false; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * |
||
252 | * @param string $name |
||
253 | * @return Type|false |
||
254 | */ |
||
255 | public function getType($name) |
||
256 | { |
||
257 | if (isset($this->types[$name])) { |
||
258 | return $this->types[$name]; |
||
259 | } |
||
260 | return false; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * |
||
265 | * @param string $name |
||
266 | * @return AttributeItem|false |
||
267 | */ |
||
268 | public function getAttribute($name) |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * |
||
278 | * @param string $name |
||
279 | * @return AttributeGroup|false |
||
280 | */ |
||
281 | public function getAttributeGroup($name) |
||
282 | { |
||
283 | if (isset($this->attributeGroups[$name])) { |
||
284 | return $this->attributeGroups[$name]; |
||
285 | } |
||
286 | return false; |
||
287 | } |
||
288 | |||
289 | public function __toString() |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * |
||
296 | * @param string $getter |
||
297 | * @param string $name |
||
298 | * @param string $namespace |
||
299 | * @param bool[] $calling |
||
300 | * @param bool $throw |
||
301 | * |
||
302 | * @throws TypeNotFoundException |
||
303 | * @return SchemaItem|null |
||
304 | */ |
||
305 | protected function findSomethingNoThrow( |
||
306 | $getter, |
||
307 | $name, |
||
308 | $namespace = null, |
||
309 | array & $calling = array() |
||
310 | ) { |
||
311 | $calling[spl_object_hash($this)] = true; |
||
312 | $cid = "$getter, $name, $namespace"; |
||
313 | |||
314 | if (isset($this->typeCache[$cid])) { |
||
315 | return $this->typeCache[$cid]; |
||
316 | } |
||
317 | |||
318 | if (null === $namespace || $this->getTargetNamespace() === $namespace) { |
||
319 | /** |
||
320 | * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem|null $item |
||
321 | */ |
||
322 | $item = $this->$getter($name); |
||
323 | if ($item instanceof SchemaItem) { |
||
324 | return $this->typeCache[$cid] = $item; |
||
325 | } |
||
326 | } |
||
327 | foreach ($this->getSchemas() as $childSchema) { |
||
328 | if (!isset($calling[spl_object_hash($childSchema)])) { |
||
329 | $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling); |
||
330 | |||
331 | if ($in instanceof SchemaItem) { |
||
332 | return $this->typeCache[$cid] = $in; |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * |
||
340 | * @param string $getter |
||
341 | * @param string $name |
||
342 | * @param string $namespace |
||
343 | * @param bool[] $calling |
||
344 | * @param bool $throw |
||
345 | * |
||
346 | * @throws TypeNotFoundException |
||
347 | * @return SchemaItem |
||
348 | */ |
||
349 | protected function findSomething($getter, $name, $namespace = null, &$calling = array()) |
||
350 | { |
||
351 | $in = $this->findSomethingNoThrow( |
||
352 | $getter, |
||
353 | $name, |
||
354 | $namespace, |
||
355 | $calling |
||
356 | ); |
||
357 | |||
358 | if ($in instanceof SchemaItem) { |
||
359 | return $in; |
||
360 | } |
||
361 | |||
362 | throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name)); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * |
||
367 | * @param string $name |
||
368 | * @param string $namespace |
||
369 | * @return Type |
||
370 | */ |
||
371 | public function findType($name, $namespace = null) |
||
372 | { |
||
373 | /** |
||
374 | * @var Type $out |
||
375 | */ |
||
376 | $out = $this->findSomething('getType', $name, $namespace); |
||
377 | |||
378 | return $out; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * |
||
383 | * @param string $name |
||
384 | * @param string $namespace |
||
385 | * @return Group |
||
386 | */ |
||
387 | public function findGroup($name, $namespace = null) |
||
388 | { |
||
389 | /** |
||
390 | * @var Group $out |
||
391 | */ |
||
392 | $out = $this->findSomething('getGroup', $name, $namespace); |
||
393 | |||
394 | return $out; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * |
||
399 | * @param string $name |
||
400 | * @param string $namespace |
||
401 | * @return ElementDef |
||
402 | */ |
||
403 | public function findElement($name, $namespace = null) |
||
404 | { |
||
405 | /** |
||
406 | * @var ElementDef $out |
||
407 | */ |
||
408 | $out = $this->findSomething('getElement', $name, $namespace); |
||
409 | |||
410 | return $out; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * |
||
415 | * @param string $name |
||
416 | * @param string $namespace |
||
417 | * @return AttributeItem |
||
418 | */ |
||
419 | public function findAttribute($name, $namespace = null) |
||
420 | { |
||
421 | /** |
||
422 | * @var AttributeItem $out |
||
423 | */ |
||
424 | $out = $this->findSomething('getAttribute', $name, $namespace); |
||
425 | |||
426 | return $out; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * |
||
431 | * @param string $name |
||
432 | * @param string $namespace |
||
433 | * @return AttributeGroup |
||
434 | */ |
||
435 | public function findAttributeGroup($name, $namespace = null) |
||
443 | } |
||
444 | } |
||
445 |