Completed
Push — develop ( 5697fc...ff1568 )
by Mikaël
01:59
created

AbstractAttributeHandler::getValueNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace WsdlToPhp\DomHandler;
4
5
class AbstractAttributeHandler extends AbstractNodeHandler
6
{
7
    /**
8
     * @var string
9
     */
10
    const DEFAULT_VALUE_TYPE = 'string';
11
    /**
12
     * @var string
13
     */
14
    const ATTRIBUTE_NAMESPACE = 'namespace';
15
    /**
16
     * @var string
17
     */
18
    const ATTRIBUTE_NAME = 'name';
19
    /**
20
     * @var string
21
     */
22
    const ATTRIBUTE_REF = 'ref';
23
    /**
24
     * @var string
25
     */
26
    const ATTRIBUTE_VALUE = 'value';
27
    /**
28
     * @var string
29
     */
30
    const ATTRIBUTE_TYPE = 'type';
31
    /**
32
     * @var string
33
     */
34
    const ATTRIBUTE_ABSTRACT = 'abstract';
35
    /**
36
     * @var string
37
     */
38
    const ATTRIBUTE_MAX_OCCURS = 'maxOccurs';
39
    /**
40
     * @var string
41
     */
42
    const ATTRIBUTE_MIN_OCCURS = 'minOccurs';
43
    /**
44
     * @var string
45
     */
46
    const ATTRIBUTE_NILLABLE = 'nillable';
47
    /**
48
     * @var string
49
     */
50
    const VALUE_UNBOUNDED = 'unbounded';
51
    /**
52
     * @var string
53
     */
54
    const DEFAULT_OCCURENCE_VALUE = 1;
55
    /**
56
     * @see \WsdlToPhp\DomHandler\AbstractNodeHandler::getNode()
57
     * @return \DOMAttr
58
     */
59 92
    public function getNode()
60
    {
61 92
        return parent::getNode();
62
    }
63
    /**
64
     * @return \DOMAttr
65
     */
66 80
    public function getAttribute()
67
    {
68 80
        return $this->getNode();
69
    }
70
    /**
71
     * Tries to get attribute type on the same node
72
     * in order to return the value of the attribute in its type
73
     * @return string|null
74
     */
75 8
    public function getType()
76
    {
77 8
        $type = null;
78 8
        if (($parent = $this->getParent()) instanceof ElementHandler && $parent->hasAttribute(self::ATTRIBUTE_TYPE)) {
79 4
            $type = $parent->getAttribute(self::ATTRIBUTE_TYPE)->getValue(false, false);
0 ignored issues
show
Unused Code introduced by
The call to ElementHandler::getValue() has too many arguments starting with false.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to NodeHandler::getValue() has too many arguments starting with false.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80 3
        }
81 8
        return $type;
82
    }
83
    /**
84
     * @param bool $withNamespace
85
     * @param bool $withinItsType
86
     * @param string $asType
87
     * @return mixed
88
     */
89 76
    public function getValue($withNamespace = false, $withinItsType = true, $asType = self::DEFAULT_VALUE_TYPE)
90
    {
91 76
        $value = $this->getAttribute()->value;
92 76
        if ($withNamespace === false && !empty($value)) {
93 56
            $value = implode('', array_slice(explode(':', $value), -1, 1));
94 42
        }
95 76
        if ($value !== null && $withinItsType === true) {
96 76
            $value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType);
97 57
        }
98 76
        return $value;
99
    }
100
    /**
101
     * @return null|string
102
     */
103 4
    public function getValueNamespace()
104
    {
105 4
        $value = $this->getAttribute()->value;
106 4
        $namespace = null;
107 4
        if (strpos($value, ':') !== false) {
108 4
            $namespace = implode('', array_slice(explode(':', $value), 0, -1));
109 3
        }
110 4
        return $namespace;
111
    }
112
113
    /**
114
     * Returns the value with good type
115
     * @param mixed $value the value
116
     * @param string $knownType the value
117
     * @return mixed
118
     */
119 76
    public static function getValueWithinItsType($value, $knownType = null)
120
    {
121 76
        if (is_int($value) || (!is_null($value) && in_array($knownType, array(
122 76
            'time',
123 57
            'positiveInteger',
124 57
            'unsignedLong',
125 57
            'unsignedInt',
126 57
            'short',
127 57
            'long',
128 57
            'int',
129 57
            'integer',
130 76
        ), true))) {
131 4
            return intval($value);
132 76
        } elseif (is_float($value) || (!is_null($value) && in_array($knownType, array(
133 76
            'float',
134 57
            'double',
135 57
            'decimal',
136 76
        ), true))) {
137 4
            return floatval($value);
138 76
        } elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array(
139 76
            'bool',
140 57
            'boolean',
141 76
        ), true))) {
142 16
            return ($value === 'true' || $value === true || $value === 1 || $value === '1');
143
        }
144 64
        return $value;
145
    }
146
}
147