Completed
Push — master ( f18951...94bfb9 )
by Randy
03:34
created

HydrateProcedure::appendOrAssign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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
use function Dgame\Conditional\debug;
11
12
/**
13
 * Class HydrateProcedure
14
 * @package Dgame\Soap\Hydrator
15
 */
16
final class HydrateProcedure implements VisitorInterface
17
{
18
    const DEBUG_LABEL = 'Debug_Hydrate_Procedure';
19
20
    /**
21
     * @var string
22
     */
23
    private $tab;
24
    /**
25
     * @var Hydrate
26
     */
27
    private $hydrate;
28
    /**
29
     * @var ClassMapper
30
     */
31
    private $mapper;
32
33
    /**
34
     * Hydrat constructor.
35
     *
36
     * @param ClassMapper $mapper
37
     * @param string|null $tab
38
     */
39 6
    public function __construct(ClassMapper $mapper, string $tab = null)
40
    {
41 6
        $this->mapper = $mapper;
42 6
        $this->tab    = "\t" . $tab;
43 6
    }
44
45
    /**
46
     * @return Hydrate
47
     */
48 6
    public function getHydrate(): Hydrate
49
    {
50 6
        return $this->hydrate;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 6
    public function isValid(): bool
57
    {
58 6
        return $this->hydrate !== null;
59
    }
60
61
    /**
62
     * @param Element $element
63
     */
64
    public function visitElement(Element $element)
65
    {
66
        debug(self::DEBUG_LABEL)->output($this->tab . ' - Element: ' . $element->getName());
67
68
        $this->visit($element);
69
    }
70
71
    /**
72
     * @param XmlElement $element
73
     */
74 6
    public function visitXmlElement(XmlElement $element)
75
    {
76 6
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlElement: ' . $element->getName());
77
78 6
        $this->visit($element);
79 6
    }
80
81
    /**
82
     * @param XmlNode $node
83
     */
84 6
    public function visitXmlNode(XmlNode $node)
85
    {
86 6
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlNode: ' . $node->getName());
87
88 6
        $this->visit($node);
89 6
        $this->visitChildrenOf($node);
90 6
    }
91
92
    /**
93
     * @param Attribute $attribute
94
     */
95
    public function visitAttribute(Attribute $attribute)
96
    {
97
        $this->assignAttribute($attribute);
98
    }
99
100
    /**
101
     * @param XmlAttribute $attribute
102
     */
103 4
    public function visitXmlAttribute(XmlAttribute $attribute)
104
    {
105 4
        $this->assignAttribute($attribute);
106 4
    }
107
108
    /**
109
     * @param Attribute $attribute
110
     */
111 4
    private function assignAttribute(Attribute $attribute)
112
    {
113 4
        if ($attribute->hasValue()) {
114 4
            $this->hydrate->assign($attribute);
115
        }
116 4
    }
117
118
    /**
119
     * @param Element $element
120
     */
121 6
    private function visit(Element $element)
122
    {
123 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...
124 6
        if ($this->isValid()) {
125 6
            $this->visitAttributesOf($element);
126
        }
127 6
    }
128
129
    /**
130
     * @param Element $element
131
     */
132 6
    private function visitAttributesOf(Element $element)
133
    {
134 6
        foreach ($element->getAttributes() as $attribute) {
135 4
            $attribute->accept($this);
136
        }
137
138 6
        if ($element->hasValue()) {
139 2
            $this->hydrate->assignValue('value', $element->getValue());
140
        }
141 6
    }
142
143
    /**
144
     * @param XmlNode $node
145
     */
146 6
    private function visitChildrenOf(XmlNode $node)
147
    {
148 6
        foreach ($node->getChildren() as $child) {
149 6
            $this->visitChild($child);
150
        }
151 6
    }
152
153
    /**
154
     * @param Element $element
155
     */
156 6
    private function visitChild(Element $element)
157
    {
158 6
        $procedure = new self($this->mapper, $this->tab);
159 6
        $element->accept($procedure);
160
161 6
        if ($this->isValid()) {
162 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...
163
        } else {
164 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...
165
        }
166 6
    }
167
168
    /**
169
     * @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...
170
     * @param Element          $element
171
     */
172 5
    private function appendOrAssign(self $procedure, Element $element)
173
    {
174 5
        if ($procedure->isValid()) {
175 4
            $this->hydrate->append($procedure->getHydrate());
176
        } else {
177 5
            $this->hydrate->assign($element);
178
        }
179 5
    }
180
181
    /**
182
     * @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...
183
     */
184 3
    private function skipTo(self $procedure)
185
    {
186 3
        if ($procedure->isValid()) {
187 3
            $this->hydrate = $procedure->getHydrate();
188
        }
189
    }
190
}