AbstractAttributeHandler::getValueWithinItsType()   C
last analyzed

Complexity

Conditions 12
Paths 6

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 29
ccs 24
cts 24
cp 1
rs 6.9666
cc 12
nc 6
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\DomHandler;
6
7
class AbstractAttributeHandler extends AbstractNodeHandler
8
{
9
    public const DEFAULT_VALUE_TYPE = 'string';
10
11
    public const ATTRIBUTE_NAMESPACE = 'namespace';
12
13
    public const ATTRIBUTE_NAME = 'name';
14
15
    public const ATTRIBUTE_REF = 'ref';
16
17
    public const ATTRIBUTE_VALUE = 'value';
18
19
    public const ATTRIBUTE_TYPE = 'type';
20
21
    public const ATTRIBUTE_ABSTRACT = 'abstract';
22
23
    public const ATTRIBUTE_MAX_OCCURS = 'maxOccurs';
24
25
    public const ATTRIBUTE_MIN_OCCURS = 'minOccurs';
26
27
    public const ATTRIBUTE_NILLABLE = 'nillable';
28
29
    public const VALUE_UNBOUNDED = 'unbounded';
30
31
    /**
32
     * @deprecated
33
     */
34
    public const DEFAULT_OCCURENCE_VALUE = 1;
35
36
    public const DEFAULT_OCCURRENCE_VALUE = 1;
37
38 40
    public function getAttribute(): ?\DOMAttr
39
    {
40 40
        return $this->getNode() instanceof \DOMAttr ? $this->getNode() : null;
41
    }
42
43
    /**
44
     * Tries to get attribute type on the same node in order to return the value of the attribute in its type.
45
     */
46 2
    public function getType(): ?string
47
    {
48 2
        $type = null;
49 2
        if (($parent = $this->getParent()) instanceof ElementHandler && $parent->hasAttribute(self::ATTRIBUTE_TYPE)) {
50 2
            $type = $parent->getAttribute(self::ATTRIBUTE_TYPE)->getValue(false, false);
51
        }
52
53 2
        return $type;
54
    }
55
56 38
    public function getValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = self::DEFAULT_VALUE_TYPE)
57
    {
58 38
        $value = $this->getAttribute()->value;
59 38
        if (false === $withNamespace && !empty($value)) {
60 28
            $value = implode('', array_slice(explode(':', $value), -1, 1));
61
        }
62 38
        if (null !== $value && true === $withinItsType) {
63 38
            $value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType);
64
        }
65
66 38
        return $value;
67
    }
68
69 2
    public function getValueNamespace(): ?string
70
    {
71 2
        $value = $this->getAttribute()->value;
72 2
        $namespace = null;
73 2
        if (false !== strpos($value, ':')) {
74 2
            $namespace = implode('', array_slice(explode(':', $value), 0, -1));
75
        }
76
77 2
        return $namespace;
78
    }
79
80
    /**
81
     * Returns the value with good type.
82
     *
83
     * @param null|bool|float|int|string $value     the value
84
     * @param null|string                $knownType the value expected type
85
     *
86
     * @return mixed
87
     */
88 38
    public static function getValueWithinItsType($value, ?string $knownType = null)
89
    {
90 38
        if (is_int($value) || (!is_null($value) && in_array($knownType, [
91 38
            'time',
92 38
            'positiveInteger',
93 38
            'unsignedLong',
94 38
            'unsignedInt',
95 38
            'short',
96 38
            'long',
97 38
            'int',
98 38
            'integer',
99 38
        ], true))) {
100 2
            return (int) $value;
101
        }
102 38
        if (is_float($value) || (!is_null($value) && in_array($knownType, [
103 38
            'float',
104 38
            'double',
105 38
            'decimal',
106 38
        ], true))) {
107 2
            return (float) $value;
108
        }
109 38
        if (is_bool($value) || (!is_null($value) && in_array($knownType, [
110 38
            'bool',
111 38
            'boolean',
112 38
        ], true))) {
113 8
            return 'true' === $value || true === $value || '1' === $value;
114
        }
115
116 32
        return $value;
117
    }
118
}
119