Passed
Push — static-analysis ( f539d2...7025cd )
by SignpostMarv
03:14
created

Schema::findSomethingNoThrow()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 4
rs 8.5806
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Schema;
3
4
use Closure;
5
use DOMElement;
6
use RuntimeException;
7
use GoetasWebservices\XML\XSDReader\AbstractSchemaReader;
8
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction;
9
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
10
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
11
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
12
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
13
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
14
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
15
use GoetasWebservices\XML\XSDReader\Schema\Exception\SchemaException;
16
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
17
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
18
use GoetasWebservices\XML\XSDReader\Utils\UrlUtils;
19
20
class Schema extends AbstractSchema
21
{
22
    /**
23
    * {@inheritdoc}
24
    */
25 135
    protected function findSomethingNoThrow(
26
        $getter,
27
        $name,
28
        $namespace = null,
29
        array & $calling = array()
30
    ) {
31 135
        $calling[spl_object_hash($this)] = true;
32 135
        $cid = "$getter, $name, $namespace";
33
34 135
        if (isset($this->typeCache[$cid])) {
35 135
            return $this->typeCache[$cid];
36
        } elseif (
37 135
            $this->getTargetNamespace() === $namespace &&
38 135
            (($item = $this->$getter($name)) instanceof SchemaItem)
39 45
        ) {
40 135
            return $this->typeCache[$cid] = $item;
41
        }
42
43 135
        return $this->findSomethingNoThrowSchemas(
44 135
            $this->getSchemas(),
45 135
            $cid,
46 135
            $getter,
47 135
            $name,
48 135
            $namespace,
49 90
            $calling
50 45
        );
51
    }
52
53
54
    /**
55
    * {@inheritdoc}
56
    */
57 135
    protected function findSomethingNoThrowSchemas(
58
        array $schemas,
59
        $cid,
60
        $getter,
61
        $name,
62
        $namespace = null,
63
        array & $calling = array()
64
    ) {
65 135
        foreach ($schemas as $childSchema) {
66 135
            if (!isset($calling[spl_object_hash($childSchema)])) {
67 135
                $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling);
68
69 135
                if ($in instanceof SchemaItem) {
70 135
                    return $this->typeCache[$cid] = $in;
71
                }
72 7
            }
73 7
        }
74 21
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 135
    protected function findSomething($getter, $name, $namespace = null, &$calling = array())
80
    {
81 135
        $in = $this->findSomethingNoThrow(
82 135
            $getter,
83 135
            $name,
84 135
            $namespace,
85 90
            $calling
86 45
        );
87
88 135
        if ($in instanceof SchemaItem) {
89 135
            return $in;
90
        }
91
92 15
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
93
    }
94
95
    /**
96
    * {@inheritdoc}
97
    */
98 135
    protected static function loadImportFreshKeys(
99
        SchemaReaderLoadAbstraction $reader,
100
        $namespace,
101
        $file
102
    ) {
103 135
        $globalSchemaInfo = $reader->getGlobalSchemaInfo();
104
105 135
        $keys = [];
106
107 135
        if (isset($globalSchemaInfo[$namespace])) {
108 135
            $keys[] = $globalSchemaInfo[$namespace];
109 45
        }
110
111 135
        $keys[] = $reader->getNamespaceSpecificFileIndex(
112 135
            $file,
113 90
            $namespace
114 45
        );
115
116 135
        $keys[] = $file;
117
118 135
        return $keys;
119
    }
120
121
    /**
122
    * {@inheritdoc}
123
    */
124 3
    protected static function loadImportFreshCallbacksNewSchema(
125
        $namespace,
126
        SchemaReaderLoadAbstraction $reader,
127
        Schema $schema,
128
        $file
129
    ) {
130 3
        $newSchema = Schema::setLoadedFile(
131 3
            $file,
132 3
            ($namespace ? new Schema() : $schema)
133 1
        );
134
135 3
        if ($namespace) {
136
            $newSchema->addSchema($reader->getGlobalSchema());
137
            $schema->addSchema($newSchema);
138
        }
139
140 3
        return $newSchema;
141
    }
142
143
    /**
144
    * {@inheritdoc}
145
    */
146 3
    protected static function loadImportFreshCallbacks(
147
        $namespace,
148
        SchemaReaderLoadAbstraction $reader,
149
        Schema $schema,
150
        $file
151
    ) {
152 3
        return $reader->schemaNode(
153 3
            static::loadImportFreshCallbacksNewSchema(
154 3
                $namespace,
155 3
                $reader,
156 3
                $schema,
157 2
                $file
158 1
            ),
159 3
            $reader->getDOM(
160 3
                $reader->hasKnownSchemaLocation($file)
161 1
                    ? $reader->getKnownSchemaLocation($file)
162 2
                    : $file
163 3
            )->documentElement,
164 2
            $schema
165 1
        );
166
    }
167
168
    /**
169
    * {@inheritdoc}
170
    */
171
    protected static function loadImportFresh(
172
        $namespace,
173
        SchemaReaderLoadAbstraction $reader,
174
        Schema $schema,
175
        $file
176
    ) {
177 3
        return function () use ($namespace, $reader, $schema, $file) {
178
            foreach (
179 3
                static::loadImportFreshCallbacks(
180 3
                    $namespace,
181 3
                    $reader,
182 3
                    $schema,
183 2
                    $file
184 1
                ) as $callback
185 1
            ) {
186 3
                $callback();
187 1
            }
188 3
        };
189
    }
190
}
191