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

TagPart::getFinalName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 1
b 0
f 0
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
    const ATTRIBUTE_ELEMENT = 'element';
13
    const ATTRIBUTE_TYPE = 'type';
14
15
    /**
16
     * @return null|AttributeHandler|int|string
17
     */
18 10
    public function getAttributeElement(bool $returnValue = true)
19
    {
20 10
        return $this->getAttributeMixedValue(self::ATTRIBUTE_ELEMENT, $returnValue);
21
    }
22
23
    public function getMatchingElement(): ?TagElement
24
    {
25
        $element = null;
26 10
        $elementName = $this->getAttributeElement();
27
        if (!empty($elementName)) {
28 10
            $element = $this->getDomDocumentHandler()->getElementByNameAndAttributes(AbstractDocument::TAG_ELEMENT, [
29
                'name' => $elementName,
30
            ], 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 4
        }
32
33 4
        return $element;
34 4
    }
35 4
36 4
    /**
37 4
     * @return null|AttributeHandler|int|string
38 4
     */
39 4
    public function getAttributeType(bool $returnValue = true)
40 4
    {
41 2
        return $this->getAttributeMixedValue(self::ATTRIBUTE_TYPE, $returnValue);
42
    }
43 2
44
    public function getFinalType(): string
45
    {
46
        $type = $this->getAttributeType();
47
        if (empty($type)) {
48 4
            $element = $this->getMatchingElement();
49
            if ($element instanceof TagElement && $element->hasAttribute(self::ATTRIBUTE_TYPE)) {
50
                $type = $element->getAttribute(self::ATTRIBUTE_TYPE)->getValue();
51 2
            } else {
52
                $type = $this->getAttributeElement();
53 2
            }
54 2
        }
55 2
56
        return $type;
57
    }
58 2
59
    public function getFinalName(): string
60
    {
61 4
        $name = $this->getAttributeType();
62
        if (empty($name)) {
63 4
            $name = $this->getAttributeElement();
64 4
        }
65
66
        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 4
69 4
    public function getFinalNamespace(): ?string
70 4
    {
71
        $attribute = $this->getAttributeType(false);
72
        if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
73
            return $namespace;
74
        }
75
76
        $attribute = $this->getAttributeElement(false);
77
        if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
78
            return $namespace;
79 10
        }
80
81 10
        return null;
82 10
    }
83 6
84
    /**
85
     * @return null|AttributeHandler|int|string
86 10
     */
87
    protected function getAttributeMixedValue(string $attributeName, bool $returnValue = true)
88
    {
89
        $value = $this->getAttribute($attributeName);
90
        if ($returnValue) {
91
            $value = $value instanceof AttributeHandler ? $value->getValue() : null;
0 ignored issues
show
introduced by
$value is always a sub-type of WsdlToPhp\DomHandler\AttributeHandler.
Loading history...
92
        }
93
94
        return $value;
95
    }
96
}
97