Wsdl   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 44
c 2
b 1
f 0
dl 0
loc 100
ccs 53
cts 53
cp 1
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getElementsByName() 0 5 1
A addExternalSchema() 0 5 1
A getElementsByNameAndAttributes() 0 25 4
A getExternalSchemas() 0 3 1
A getElementByNameAndAttributes() 0 6 1
A getElementByName() 0 5 1
A useParentMethodAndExternals() 0 9 4
A useExternalSchemas() 0 18 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsdlHandler;
6
7
use WsdlToPhp\WsdlHandler\Tag\AbstractTag;
8
9
class Wsdl extends AbstractDocument
10
{
11
    protected array $externalSchemas = [];
12
13 10
    public function addExternalSchema(Schema $schema): Wsdl
14
    {
15 10
        $this->externalSchemas[] = $schema;
16
17 10
        return $this;
18
    }
19
20 40
    public function getExternalSchemas(): array
21
    {
22 40
        return $this->externalSchemas;
23
    }
24
25 26
    public function getElementByName(string $name, bool $includeExternals = false): ?AbstractTag
26
    {
27 26
        return $this->useParentMethodAndExternals(__FUNCTION__, [
28 26
            $name,
29 26
        ], $includeExternals, true);
30
    }
31
32 64
    public function getElementByNameAndAttributes(string $name, array $attributes, bool $includeExternals = false): ?AbstractTag
33
    {
34 64
        return $this->useParentMethodAndExternals(__FUNCTION__, [
35 64
            $name,
36 64
            $attributes,
37 64
        ], $includeExternals, true);
38
    }
39
40 102
    public function getElementsByName(string $name, bool $includeExternals = false): array
41
    {
42 102
        return $this->useParentMethodAndExternals(__FUNCTION__, [
43 102
            $name,
44 102
        ], $includeExternals);
45
    }
46
47 78
    public function getElementsByNameAndAttributes(string $name, array $attributes, ?\DOMNode $node = null, bool $includeExternals = false): array
48
    {
49 78
        if ($includeExternals) {
50 6
            $elements = $this->useParentMethodAndExternals(__FUNCTION__, [
51 6
                $name,
52 6
                $attributes,
53 6
                $node,
54 6
            ]);
55
56
            /** @var Schema $externalSchema */
57 6
            foreach ($this->getExternalSchemas() as $index => $externalSchema) {
58 6
                if (0 < ($nodes = $externalSchema->searchTagsByXpath($name, $attributes, $node))->count()) {
59 6
                    $elements = array_merge($elements, $this->getElementsHandlers($nodes));
60
                }
61
            }
62 6
            $elements = array_unique($elements, SORT_REGULAR);
63
        } else {
64 74
            $elements = $this->useParentMethodAndExternals(__FUNCTION__, [
65 74
                $name,
66 74
                $attributes,
67 74
                $node,
68 74
            ], $includeExternals);
69
        }
70
71 78
        return $elements;
72
    }
73
74
    /**
75
     * Handles any method that exists within the parent class,
76
     * in addition it handles the case when we want to use the external schemas to search in.
77
     *
78
     * @return mixed
79
     */
80 114
    protected function useParentMethodAndExternals(string $method, array $parameters, bool $includeExternals = false, bool $returnOne = false)
81
    {
82 114
        $result = parent::{$method}(...$parameters);
83 114
84 114
        if ($includeExternals && (!$returnOne || empty($result))) {
85 114
            $result = $this->useExternalSchemas($method, $parameters, $result, $returnOne);
86
        }
87 114
88 10
        return $result;
89
    }
90
91 114
    protected function useExternalSchemas(string $method, array $parameters, ?array $parentResult, bool $returnOne = false)
92
    {
93
        $result = $parentResult;
94 10
95
        foreach ($this->getExternalSchemas() as $externalSchema) {
96 10
            $externalResult = call_user_func_array([$externalSchema, $method], $parameters);
97
98 10
            if ($returnOne && !is_null($externalResult)) {
99 10
                $result = $externalResult;
100 10
101 10
                break;
102 10
            }
103
            if (is_array($externalResult) && !empty($externalResult)) {
104 10
                $result = array_merge($result, $externalResult);
105 6
            }
106
        }
107 6
108
        return $result;
109 8
    }
110
}
111