Completed
Push — master ( d54425...3acddd )
by Randy
03:25
created

HydrateProcedure::visitElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Attribute\Attribute;
6
use Dgame\Soap\Attribute\XmlAttribute;
7
use Dgame\Soap\Element;
8
use Dgame\Soap\XmlElement;
9
use Dgame\Soap\XmlNode;
10
11
/**
12
 * Class HydrateProcedure
13
 * @package Dgame\Soap\Hydrator
14
 */
15
final class HydrateProcedure implements VisitorInterface
16
{
17
    /**
18
     * @var Hydrate
19
     */
20
    private $hydrate;
21
    /**
22
     * @var ClassMapper
23
     */
24
    private $mapper;
25
26
    /**
27
     * Hydrat constructor.
28
     *
29
     * @param ClassMapper $mapper
30
     */
31 6
    public function __construct(ClassMapper $mapper)
32
    {
33 6
        $this->mapper = $mapper;
34 6
    }
35
36
    /**
37
     * @return Hydrate
38
     */
39 6
    public function getHydrate(): Hydrate
40
    {
41 6
        return $this->hydrate;
42
    }
43
44
    /**
45
     * @return bool
46
     */
47 6
    public function isValid(): bool
48
    {
49 6
        return $this->hydrate !== null;
50
    }
51
52
    /**
53
     * @param Element $element
54
     */
55
    public function visitElement(Element $element)
56
    {
57
        $this->visit($element);
58
    }
59
60
    /**
61
     * @param XmlElement $element
62
     */
63 6
    public function visitXmlElement(XmlElement $element)
64
    {
65 6
        $this->visit($element);
66 6
    }
67
68
    /**
69
     * @param XmlNode $node
70
     */
71 6
    public function visitXmlNode(XmlNode $node)
72
    {
73 6
        $this->visit($node);
74 6
        $this->visitChildrenOf($node);
75 6
    }
76
77
    /**
78
     * @param Attribute $attribute
79
     */
80
    public function visitAttribute(Attribute $attribute)
81
    {
82
        $this->assignAttribute($attribute);
83
    }
84
85
    /**
86
     * @param XmlAttribute $attribute
87
     */
88 4
    public function visitXmlAttribute(XmlAttribute $attribute)
89
    {
90 4
        $this->assignAttribute($attribute);
91 4
    }
92
93
    /**
94
     * @param Attribute $attribute
95
     */
96 4
    private function assignAttribute(Attribute $attribute)
97
    {
98 4
        if ($attribute->hasValue()) {
99 4
            $this->hydrate->assign($attribute);
100
        }
101 4
    }
102
103
    /**
104
     * @param Element $element
105
     */
106 6
    private function visit(Element $element)
107
    {
108 6
        $this->hydrate = $this->mapper->new($element->getName());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->mapper->new($element->getName()) can also be of type object<self>. However, the property $hydrate is declared as type object<Dgame\Soap\Hydrator\Hydrate>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109 6
        if ($this->isValid()) {
110 6
            $this->visitAttributesOf($element);
111
        }
112 6
    }
113
114
    /**
115
     * @param Element $element
116
     */
117 6
    private function visitAttributesOf(Element $element)
118
    {
119 6
        foreach ($element->getAttributes() as $attribute) {
120 4
            $attribute->accept($this);
121
        }
122
123 6
        if ($element->hasValue()) {
124 2
            $this->hydrate->assignValue('value', $element->getValue());
125
        }
126 6
    }
127
128
    /**
129
     * @param XmlNode $node
130
     */
131 6
    private function visitChildrenOf(XmlNode $node)
132
    {
133 6
        foreach ($node->getChildren() as $child) {
134 6
            $this->visitChild($child);
135
        }
136 6
    }
137
138
    /**
139
     * @param Element $element
140
     */
141 6
    private function visitChild(Element $element)
142
    {
143 6
        $procedure = new self($this->mapper);
144 6
        $element->accept($procedure);
145
146 6
        if ($this->isValid()) {
147 5
            $this->appendOrAssign($procedure, $element);
0 ignored issues
show
Documentation introduced by
$procedure is of type object<Dgame\Soap\Hydrator\HydrateProcedure>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
        } else {
149 3
            $this->skipTo($procedure);
0 ignored issues
show
Documentation introduced by
$procedure is of type object<Dgame\Soap\Hydrator\HydrateProcedure>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
        }
151 6
    }
152
153
    /**
154
     * @param HydrateProcedure $procedure
0 ignored issues
show
Documentation introduced by
Should the type for parameter $procedure not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
155
     * @param Element          $element
156
     */
157 5
    private function appendOrAssign(self $procedure, Element $element)
158
    {
159 5
        if ($procedure->isValid()) {
160 4
            $this->hydrate->append($procedure->getHydrate());
161
        } else {
162 5
            $this->hydrate->assign($element);
163
        }
164 5
    }
165
166
    /**
167
     * @param HydrateProcedure $procedure
0 ignored issues
show
Documentation introduced by
Should the type for parameter $procedure not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
168
     */
169 3
    private function skipTo(self $procedure)
170
    {
171 3
        if ($procedure->isValid()) {
172 3
            $this->hydrate = $procedure->getHydrate();
173
        }
174
    }
175
}