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

makeCallbackCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 4
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GoetasWebservices\XML\XSDReader;
4
5
use Closure;
6
use DOMElement;
7
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer;
8
use GoetasWebservices\XML\XSDReader\Schema\Type\BaseComplexType;
9
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
10
use RuntimeException;
11
12
abstract class SchemaReaderCallbackAbstraction extends AbstractSchemaReader
13
{
14
    /**
15
     * @param mixed[][] $commonMethods
16
     * @param mixed[][] $methods
17
     * @param mixed[][] $commonArguments
18
     *
19
     * @return mixed
20
     */
21 45
    protected function maybeCallCallableWithArgs(
22
        DOMElement $childNode,
23
        array $commonMethods = [],
24
        array $methods = [],
25
        array $commonArguments = []
26
    ) {
27 45
        foreach ($commonMethods as $commonMethodsSpec) {
28 45
            list($localNames, $callable, $args) = $commonMethodsSpec;
29
30
            /**
31
             * @var string[]
32
             */
33 45
            $localNames = $localNames;
34
35
            /**
36
             * @var callable
37
             */
38 45
            $callable = $callable;
39
40
            /**
41
             * @var mixed[]
42
             */
43 45
            $args = $args;
44
45 45
            if (in_array($childNode->localName, $localNames)) {
46 45
                return call_user_func_array($callable, $args);
47
            }
48 45
        }
49 45
        foreach ($commonArguments as $commonArgumentSpec) {
50
            /*
51
            * @var mixed[] $commonArgumentSpec
52
            */
53 45
            list($callables, $args) = $commonArgumentSpec;
54
55
            /**
56
             * @var callable[]
57
             */
58 45
            $callables = $callables;
59
60
            /**
61
             * @var mixed[]
62
             */
63 45
            $args = $args;
64
65 45
            if (isset($callables[$childNode->localName])) {
66 45
                return call_user_func_array(
67 45
                    $callables[$childNode->localName],
68
                    $args
69 45
                );
70
            }
71 45
        }
72 45
        if (isset($methods[$childNode->localName])) {
73 45
            list($callable, $args) = $methods[$childNode->localName];
74
75
            /**
76
             * @var callable
77
             */
78 45
            $callable = $callable;
79
80
            /**
81
             * @var mixed[]
82
             */
83 45
            $args = $args;
84
85 45
            return call_user_func_array($callable, $args);
86
        }
87 45
    }
88
89 45
    protected function maybeLoadSequenceFromElementContainer(
90
        BaseComplexType $type,
91
        DOMElement $childNode
92
    ) {
93 45
        $this->maybeLoadThingFromThing(
94 45
            $type,
95 45
            $childNode,
96 45
            ElementContainer::class,
97
            'loadSequence'
98 45
        );
99 45
    }
100
101
    /**
102
     * @param string $instanceof
103
     * @param string $passTo
104
     */
105 45
    protected function maybeLoadThingFromThing(
106
        Type $type,
107
        DOMElement $childNode,
108
        $instanceof,
109
        $passTo
110
    ) {
111 45
        if (!is_a($type, $instanceof, true)) {
112
            /**
113
             * @var string
114
             */
115
            $class = static::class;
116
            throw new RuntimeException(
117
                'Argument 1 passed to '.
118
                __METHOD__.
119
                ' needs to be an instance of '.
120
                $instanceof.
121
                ' when passed onto '.
122
                $class.
123
                '::'.
124
                $passTo.
125
                '(), '.
126
                (string) get_class($type).
127
                ' given.'
128
            );
129
        }
130
131 45
        $this->$passTo($type, $childNode);
132 45
    }
133
134
    /**
135
     * @param Closure|null $callback
136
     *
137
     * @return Closure
138
     */
139
    protected function makeCallbackCallback(
140
        Type $type,
141
        DOMElement $node,
142
        Closure $callbackCallback,
143
        $callback = null
144
    ) {
145 45
        return function (
146
        ) use (
147 45
            $type,
148 45
            $node,
149 45
            $callbackCallback,
150 45
            $callback
151
        ) {
152 45
            $this->runCallbackAgainstDOMNodeList(
153 45
                $type,
154 45
                $node,
155 45
                $callbackCallback,
156
                $callback
157 45
            );
158 45
        };
159
    }
160
161
    /**
162
     * @param Closure|null $callback
163
     */
164 45
    protected function runCallbackAgainstDOMNodeList(
165
        Type $type,
166
        DOMElement $node,
167
        Closure $againstNodeList,
168
        $callback = null
169
    ) {
170 45
        $this->fillTypeNode($type, $node, true);
171
172 45
        static::againstDOMNodeList($node, $againstNodeList);
173
174 45
        if ($callback) {
175 45
            call_user_func($callback, $type);
176 45
        }
177 45
    }
178
179 45
    protected function maybeLoadExtensionFromBaseComplexType(
180
        Type $type,
181
        DOMElement $childNode
182
    ) {
183 45
        $this->maybeLoadThingFromThing(
184 45
            $type,
185 45
            $childNode,
186 45
            BaseComplexType::class,
187
            'loadExtension'
188 45
        );
189 45
    }
190
}
191