Completed
Push — develop ( 6eec50...8b93b9 )
by Mikaël
03:30
created

AbstractAttributeHandler::getType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
crap 3
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 88
    public function getNode()
60
    {
61 88
        return parent::getNode();
62
    }
63
    /**
64
     * @return \DOMAttr
65
     */
66 76
    public function getAttribute()
67
    {
68 76
        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 4
    public function getType()
76
    {
77 4
        $type = null;
78 4
        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 4
        return $type;
82
    }
83
    /**
84
     * @param bool $withNamespace
85
     * @param bool $withinItsType
86
     * @param string $asType
87
     * @return mixed
88
     */
89 72
    public function getValue($withNamespace = false, $withinItsType = true, $asType = self::DEFAULT_VALUE_TYPE)
90
    {
91 72
        $value = $this->getAttribute()->value;
92 72
        if ($withNamespace === false && !empty($value)) {
93 56
            $value = implode('', array_slice(explode(':', $value), -1, 1));
94 42
        }
95 72
        if ($value !== null && $withinItsType === true) {
96 72
            $value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType);
97 54
        }
98 72
        return $value;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $value; (integer|double|boolean|object|string|array|null) is incompatible with the return type of the parent method WsdlToPhp\DomHandler\AbstractNodeHandler::getValue of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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 72
    public static function getValueWithinItsType($value, $knownType = null)
120
    {
121 72
        if (is_int($value) || (!is_null($value) && in_array($knownType, array(
122 72
            'time',
123 54
            'positiveInteger',
124 54
            'unsignedLong',
125 54
            'unsignedInt',
126 54
            'short',
127 54
            'long',
128 54
            'int',
129 54
            'integer',
130 72
        ), true))) {
131
            return intval($value);
132 72
        } elseif (is_float($value) || (!is_null($value) && in_array($knownType, array(
133 72
            'float',
134 54
            'double',
135 54
            'decimal',
136 72
        ), true))) {
137
            return floatval($value);
138 72
        } elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array(
139 72
            'bool',
140 54
            'boolean',
141 72
        ), true))) {
142 16
            return ($value === 'true' || $value === true || $value === 1 || $value === '1');
143
        }
144 60
        return $value;
145
    }
146
}
147