Completed
Push — master ( bd4e7a...220b0d )
by Asmir
03:05
created

Schema   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.26%

Importance

Changes 0
Metric Value
wmc 47
lcom 1
cbo 7
dl 0
loc 292
ccs 94
cts 103
cp 0.9126
rs 8.439
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getElementsQualification() 0 4 1
A setElementsQualification() 0 4 1
A getAttributesQualification() 0 4 1
A setAttributesQualification() 0 4 1
A getTargetNamespace() 0 4 1
A setTargetNamespace() 0 4 1
A getTypes() 0 4 1
A getElements() 0 4 1
A getSchemas() 0 4 1
A getAttributes() 0 4 1
A getGroups() 0 4 1
A getDoc() 0 4 1
A setDoc() 0 4 1
A addType() 0 4 1
A addElement() 0 4 1
A addSchema() 0 12 4
A addAttribute() 0 4 1
A addGroup() 0 4 1
A addAttributeGroup() 0 4 1
A getAttributeGroups() 0 4 1
A getGroup() 0 7 2
A getElement() 0 7 2
A getType() 0 7 2
A getAttribute() 0 7 2
A getAttributeGroup() 0 7 2
A __toString() 0 4 1
A findType() 0 4 1
A findGroup() 0 4 1
A findElement() 0 4 1
A findAttribute() 0 4 1
A findAttributeGroup() 0 4 1
C findSomething() 0 24 8

How to fix   Complexity   

Complex Class

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
2
namespace GoetasWebservices\XML\XSDReader\Schema;
3
4
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
5
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
6
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
7
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
8
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
9
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
10
use GoetasWebservices\XML\XSDReader\Schema\Exception\SchemaException;
11
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
12
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
13
14
class Schema
15
{
16
17
    protected $elementsQualification = false;
18
19
    protected $attributesQualification = false;
20
21
    protected $targetNamespace;
22
23
    protected $schemas = array();
24
25
    protected $types = array();
26
27
    protected $elements = array();
28
29
    protected $groups = array();
30
31
    protected $attributeGroups = array();
32
33
    protected $attributes = array();
34
35
    protected $doc;
36
37
    private $typeCache = array();
38
39
40
    public function getElementsQualification()
41
    {
42
        return $this->elementsQualification;
43
    }
44
45 43
    public function setElementsQualification($elementsQualification)
46
    {
47 43
        $this->elementsQualification = $elementsQualification;
48 43
    }
49
50
    public function getAttributesQualification()
51
    {
52
        return $this->attributesQualification;
53
    }
54
55 43
    public function setAttributesQualification($attributesQualification)
56
    {
57 43
        $this->attributesQualification = $attributesQualification;
58 43
    }
59
60 43
    public function getTargetNamespace()
61
    {
62 43
        return $this->targetNamespace;
63
    }
64
65 43
    public function setTargetNamespace($targetNamespace)
66
    {
67 43
        $this->targetNamespace = $targetNamespace;
68 43
    }
69
70 4
    public function getTypes()
71
    {
72 4
        return $this->types;
73
    }
74
75 1
    public function getElements()
76
    {
77 1
        return $this->elements;
78
    }
79
80 43
    public function getSchemas()
81
    {
82 43
        return $this->schemas;
83
    }
84
85 1
    public function getAttributes()
86
    {
87 1
        return $this->attributes;
88
    }
89
90 1
    public function getGroups()
91
    {
92 1
        return $this->groups;
93
    }
94
95
    public function getDoc()
96
    {
97
        return $this->doc;
98
    }
99
100 43
    public function setDoc($doc)
101
    {
102 43
        $this->doc = $doc;
103 43
    }
104
105 43
    public function addType(Type $type)
106
    {
107 43
        $this->types[$type->getName()] = $type;
108 43
    }
109
110 43
    public function addElement(ElementDef $element)
111
    {
112 43
        $this->elements[$element->getName()] = $element;
113 43
    }
114
115 43
    public function addSchema(Schema $schema, $namespace = null)
116
    {
117 43
        if ($namespace !== null && $schema->getTargetNamespace() !== $namespace) {
118
            throw new SchemaException(sprintf("The target namespace ('%s') for schema, does not match the declared namespace '%s'", $schema->getTargetNamespace(), $namespace));
119
        }
120
121 43
        if ($namespace !== null) {
122 43
            $this->schemas[$namespace] = $schema;
123 43
        } else {
124 43
            $this->schemas[] = $schema;
125
        }
126 43
    }
127
128 43
    public function addAttribute(AttributeDef $attribute)
129
    {
130 43
        $this->attributes[$attribute->getName()] = $attribute;
131 43
    }
132
133 43
    public function addGroup(Group $group)
134
    {
135 43
        $this->groups[$group->getName()] = $group;
136 43
    }
137
138 43
    public function addAttributeGroup(AttributeGroup $group)
139
    {
140 43
        $this->attributeGroups[$group->getName()] = $group;
141 43
    }
142
143 1
    public function getAttributeGroups()
144
    {
145 1
        return $this->attributeGroups;
146
    }
147
148
    /**
149
     *
150
     * @param string $name
151
     * @return Group|false
152
     */
153 43
    public function getGroup($name)
154
    {
155 43
        if (isset($this->groups[$name])) {
156 43
            return $this->groups[$name];
157
        }
158 1
        return false;
159
    }
160
161
    /**
162
     *
163
     * @param string $name
164
     * @return ElementItem|false
165
     */
166 43
    public function getElement($name)
167
    {
168 43
        if (isset($this->elements[$name])) {
169 43
            return $this->elements[$name];
170
        }
171 1
        return false;
172
    }
173
174
    /**
175
     *
176
     * @param string $name
177
     * @return Type|false
178
     */
179 43
    public function getType($name)
180
    {
181 43
        if (isset($this->types[$name])) {
182 43
            return $this->types[$name];
183
        }
184 1
        return false;
185
    }
186
187
    /**
188
     *
189
     * @param string $name
190
     * @return AttributeItem|false
191
     */
192 43
    public function getAttribute($name)
193
    {
194 43
        if (isset($this->attributes[$name])) {
195 43
            return $this->attributes[$name];
196
        }
197 1
        return false;
198
    }
199
200
    /**
201
     *
202
     * @param string $name
203
     * @return AttributeGroup|false
204
     */
205 43
    public function getAttributeGroup($name)
206
    {
207 43
        if (isset($this->attributeGroups[$name])) {
208 43
            return $this->attributeGroups[$name];
209
        }
210 1
        return false;
211
    }
212
213
    public function __toString()
214
    {
215
        return sprintf("Target namespaace %s", $this->getTargetNamespace());
216
    }
217
218
    /**
219
     *
220
     * @param string $getter
221
     * @param string $name
222
     * @param string $namespace
223
     * @throws TypeNotFoundException
224
     * @return \GoetasWebservices\XML\XSDReader\Schema\SchemaItem
225
     */
226 43
    protected function findSomething($getter, $name, $namespace = null, &$calling = array())
227
    {
228 43
        $calling[spl_object_hash($this)] = true;
229 43
        $cid = "$getter, $name, $namespace";
230
231 43
        if (isset($this->typeCache[$cid])) {
232 43
            return $this->typeCache[$cid];
233
        }
234
235 43
        if (null === $namespace || $this->getTargetNamespace() === $namespace) {
236 43
            if ($item = $this->$getter($name)) {
237 43
                return $this->typeCache[$cid] = $item;
238
            }
239 5
        }
240 43
        foreach ($this->getSchemas() as $childSchema) {
241 43
            if (!isset($calling[spl_object_hash($childSchema)])) {
242
                try {
243 43
                    return $this->typeCache[$cid] = $childSchema->findSomething($getter, $name, $namespace, $calling);
244 6
                } catch (TypeNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
245
                }
246 6
            }
247 6
        }
248 6
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
249
    }
250
251
    /**
252
     *
253
     * @param string $name
254
     * @param string $namespace
255
     * @return Type
256
     */
257 43
    public function findType($name, $namespace = null)
258
    {
259 43
        return $this->findSomething('getType', $name, $namespace);
260
    }
261
262
    /**
263
     *
264
     * @param string $name
265
     * @param string $namespace
266
     * @return Group
267
     */
268 43
    public function findGroup($name, $namespace = null)
269
    {
270 43
        return $this->findSomething('getGroup', $name, $namespace);
271
    }
272
273
    /**
274
     *
275
     * @param string $name
276
     * @param string $namespace
277
     * @return ElementDef
278
     */
279 43
    public function findElement($name, $namespace = null)
280
    {
281 43
        return $this->findSomething('getElement', $name, $namespace);
282
    }
283
284
    /**
285
     *
286
     * @param string $name
287
     * @param string $namespace
288
     * @return AttributeReal
289
     */
290 43
    public function findAttribute($name, $namespace = null)
291
    {
292 43
        return $this->findSomething('getAttribute', $name, $namespace);
293
    }
294
295
    /**
296
     *
297
     * @param string $name
298
     * @param string $namespace
299
     * @return AttributeGroup
300
     */
301 43
    public function findAttributeGroup($name, $namespace = null)
302
    {
303 43
        return $this->findSomething('getAttributeGroup', $name, $namespace);
304
    }
305
}
306