AbstractTag   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 97.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 110
ccs 48
cts 49
cp 0.9796
rs 9.76
wmc 33

13 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAttributeTargetNamespace() 0 3 1
A getSuitableParentTags() 0 8 1
A getDomDocumentHandler() 0 3 1
A getValueAttributeValue() 0 3 2
A hasAttributeValue() 0 3 1
C getSuitableParent() 0 17 12
A getStrictParent() 0 11 3
A getAttributeName() 0 3 2
A getTargetNamespaceAttributeValue() 0 3 2
A hasAttributeName() 0 3 1
A getTargetNamespace() 0 10 4
A getAttributeRef() 0 3 2
A hasAttributeRef() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsdlHandler\Tag;
6
7
use WsdlToPhp\DomHandler\AbstractAttributeHandler as Attribute;
8
use WsdlToPhp\DomHandler\AbstractNodeHandler;
9
use WsdlToPhp\DomHandler\ElementHandler;
10
use WsdlToPhp\DomHandler\NodeHandler;
11
use WsdlToPhp\WsdlHandler\AbstractDocument;
12
13
abstract class AbstractTag extends ElementHandler
14
{
15
    public const MAX_DEEP = 5;
16
17 102
    public function getDomDocumentHandler(): AbstractDocument
18
    {
19 102
        return parent::getDomDocumentHandler();
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getDomDocumentHandler() returns the type WsdlToPhp\DomHandler\AbstractDomDocumentHandler which includes types incompatible with the type-hinted return WsdlToPhp\WsdlHandler\AbstractDocument.
Loading history...
20
    }
21
22
    /**
23
     * This method aims to get the parent element that matches a valid Wsdl element (aka struct).
24
     *
25
     * @return null|NodeHandler
26
     */
27 32
    public function getSuitableParent(bool $checkName = true, array $additionalTags = [], int $maxDeep = self::MAX_DEEP, bool $strict = false): ?AbstractNodeHandler
28
    {
29 32
        $parentNode = null;
30 32
        if ($this->getParent() instanceof AbstractNodeHandler) {
0 ignored issues
show
introduced by
$this->getParent() is always a sub-type of WsdlToPhp\DomHandler\AbstractNodeHandler.
Loading history...
31 32
            $parentTags = $strict ? $additionalTags : $this->getSuitableParentTags($additionalTags);
32 32
            $parentNode = $this->getParent()->getNode();
33 32
            while ($maxDeep-- > 0 && ($parentNode instanceof \DOMElement) && !empty($parentNode->nodeName) && (!preg_match('/'.implode('|', $parentTags).'/i', $parentNode->nodeName) || ($checkName && preg_match('/'.implode('|', $parentTags).'/i', $parentNode->nodeName) && (!$parentNode->hasAttribute('name') || '' === $parentNode->getAttribute('name'))))) {
34 12
                $parentNode = $parentNode->parentNode;
35
            }
36 32
            if ($parentNode instanceof \DOMElement) {
37 28
                $parentNode = $this->getDomDocumentHandler()->getHandler($parentNode);
38
            } else {
39 8
                $parentNode = null;
40
            }
41
        }
42
43 32
        return $parentNode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parentNode also could return the type WsdlToPhp\DomHandler\ElementHandler which is incompatible with the documented return type WsdlToPhp\DomHandler\NodeHandler|null.
Loading history...
44
    }
45
46 4
    public function hasAttributeName(): bool
47
    {
48 4
        return $this->hasAttribute(Attribute::ATTRIBUTE_NAME);
49
    }
50
51 2
    public function hasAttributeRef(): bool
52
    {
53 2
        return $this->hasAttribute(Attribute::ATTRIBUTE_REF);
54
    }
55
56 22
    public function getAttributeName(): string
57
    {
58 22
        return $this->getAttribute(Attribute::ATTRIBUTE_NAME) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_NAME)->getValue() : '';
59
    }
60
61 4
    public function getAttributeRef(): string
62
    {
63 4
        return $this->getAttribute(Attribute::ATTRIBUTE_REF) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_REF)->getValue() : '';
64
    }
65
66 4
    public function hasAttributeValue(): bool
67
    {
68 4
        return $this->hasAttribute(Attribute::ATTRIBUTE_VALUE);
69
    }
70
71 4
    public function getValueAttributeValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = null)
72
    {
73 4
        return $this->getAttribute(Attribute::ATTRIBUTE_VALUE) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_VALUE)->getValue($withNamespace, $withinItsType, $asType) : '';
74
    }
75
76 8
    public function hasAttributeTargetNamespace(): bool
77
    {
78 8
        return $this->hasAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE);
79
    }
80
81 8
    public function getTargetNamespaceAttributeValue()
82
    {
83 8
        return $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE) instanceof Attribute ? $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE)->getValue(true) : '';
84
    }
85
86
    /**
87
     * Retrieve element targetNamespace applicable value,
88
     * from targetNamespace attribute depending on the current Tag.
89
     */
90 6
    public function getTargetNamespace(): string
91
    {
92 6
        $schema = $this instanceof TagSchema ? $this : $this->getStrictParent(AbstractDocument::TAG_SCHEMA);
93 6
        if ($schema instanceof TagSchema && $schema->hasAttributeTargetNamespace()) {
94 6
            $namespace = $schema->getTargetNamespaceAttributeValue();
95
        } else {
96
            $namespace = $this->getDomDocumentHandler()->getAttributeTargetNamespaceValue();
97
        }
98
99 6
        return $namespace;
100
    }
101
102 18
    protected function getSuitableParentTags(array $additionalTags = []): array
103
    {
104 18
        return array_merge([
105 18
            AbstractDocument::TAG_ELEMENT,
106 18
            AbstractDocument::TAG_ATTRIBUTE,
107 18
            AbstractDocument::TAG_SIMPLE_TYPE,
108 18
            AbstractDocument::TAG_COMPLEX_TYPE,
109 18
        ], $additionalTags);
110
    }
111
112 14
    protected function getStrictParent(string $name, bool $checkName = false): ?AbstractNodeHandler
113
    {
114 14
        $parent = $this->getSuitableParent($checkName, [
115 14
            $name,
116 14
        ], self::MAX_DEEP, true);
117
118 14
        if ($parent instanceof AbstractNodeHandler && $parent->getName() === $name) {
119 12
            return $parent;
120
        }
121
122 4
        return null;
123
    }
124
}
125