Passed
Push — static-analysis ( 935891...314368 )
by SignpostMarv
02:22
created

Schema::loadImportFreshCallbacks()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

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