TagPart::getFinalNamespace()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsdlHandler\Tag;
6
7
use WsdlToPhp\DomHandler\AttributeHandler;
8
use WsdlToPhp\WsdlHandler\AbstractDocument;
9
10
class TagPart extends Tag
11
{
12
    public const ATTRIBUTE_ELEMENT = 'element';
13
    public const ATTRIBUTE_TYPE = 'type';
14
15
    /**
16
     * @return null|AttributeHandler|int|string
17
     */
18 14
    public function getAttributeElement(bool $returnValue = true)
19
    {
20 14
        return $this->getAttributeMixedValue(self::ATTRIBUTE_ELEMENT, $returnValue);
21
    }
22
23 8
    public function getMatchingElement(): ?TagElement
24
    {
25 8
        $element = null;
26 8
        $elementName = $this->getAttributeElement();
27 8
        if (!empty($elementName)) {
28 8
            $element = $this->getDomDocumentHandler()->getElementByNameAndAttributes(AbstractDocument::TAG_ELEMENT, [
29 8
                'name' => $elementName,
30 8
            ], true);
0 ignored issues
show
Unused Code introduced by
The call to WsdlToPhp\DomHandler\Abs...ntByNameAndAttributes() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            $element = $this->getDomDocumentHandler()->/** @scrutinizer ignore-call */ getElementByNameAndAttributes(AbstractDocument::TAG_ELEMENT, [

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
        }
32
33 8
        return $element;
34
    }
35
36
    /**
37
     * @return null|AttributeHandler|int|string
38
     */
39 12
    public function getAttributeType(bool $returnValue = true)
40
    {
41 12
        return $this->getAttributeMixedValue(self::ATTRIBUTE_TYPE, $returnValue);
42
    }
43
44 4
    public function getFinalType(): string
45
    {
46 4
        $type = $this->getAttributeType();
47 4
        if (empty($type)) {
48 4
            $element = $this->getMatchingElement();
49 4
            if ($element instanceof TagElement && $element->hasAttribute(self::ATTRIBUTE_TYPE)) {
50 2
                $type = $element->getAttribute(self::ATTRIBUTE_TYPE)->getValue();
51
            } else {
52 2
                $type = $this->getAttributeElement();
53
            }
54
        }
55
56 4
        return $type;
57
    }
58
59 2
    public function getFinalName(): string
60
    {
61 2
        $name = $this->getAttributeType();
62 2
        if (empty($name)) {
63 2
            $name = $this->getAttributeElement();
64
        }
65
66 2
        return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type WsdlToPhp\DomHandler\AttributeHandler|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
67
    }
68
69 6
    public function getFinalNamespace(): ?string
70
    {
71 6
        $attribute = $this->getAttributeType(false);
72 6
        if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
73
            return $namespace;
74
        }
75
76 6
        $attribute = $this->getAttributeElement(false);
77 6
        if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
78 4
            return $namespace;
79
        }
80
81 2
        return null;
82
    }
83
84
    /**
85
     * @return null|AttributeHandler|int|string
86
     */
87 14
    protected function getAttributeMixedValue(string $attributeName, bool $returnValue = true)
88
    {
89 14
        $value = $this->getAttribute($attributeName);
90 14
        if ($returnValue) {
91 10
            $value = $value instanceof AttributeHandler ? $value->getValue() : null;
92
        }
93
94 14
        return $value;
95
    }
96
}
97