Passed
Push — develop ( 3b8723...2c211f )
by Mikaël
46:18 queued 14s
created

AbstractTag::hasAttributeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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