Test Failed
Branch master (62528d)
by Zangra
14:14 queued 12:04
created

Option::createFromXML()   B

Complexity

Conditions 9
Paths 36

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 35
rs 8.0555
cc 9
nc 36
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
5
6
use SimpleXMLElement;
7
8
/**
9
 * Class Option
10
 */
11
class Option
12
{
13
    private string $visibility;
14
    private int $price;
15
    private string $name;
16
    /** @var Characteristic[] */
17
    private array $characteristics = [];
18
19
    public static function createFromXML(SimpleXMLElement $xml): self
20
    {
21
        // Ex: <option visibility="NOT_VISIBLE_BY_CONSUMER_OPTIONAL" price="0" name="Cash on delivery"/>
22
        $attr     = $xml->attributes();
23
        $children = $xml->children();
24
25
        $self = new self();
26
27
        // visibility (supporte l’ancienne faute "visiblity")
28
        if (isset($attr['visibility'])) {
29
            $self->setVisibility((string) $attr['visibility']);
30
        } elseif (isset($attr['visiblity'])) {
31
            $self->setVisibility((string) $attr['visiblity']);
32
        }
33
34
        if (isset($attr['price'])) {
35
            $self->setPrice((int) $attr['price']);
36
        }
37
38
        if (isset($attr['name'])) {
39
            $self->setName((string) $attr['name']);
40
        }
41
42
        // characteristics (supporte l’ancienne faute "chracteristic")
43
        if (isset($children->characteristic)) {
44
            foreach ($children->characteristic as $charXml) {
45
                $self->addCharacteristic(Characteristic::createFromXML($charXml));
0 ignored issues
show
Bug introduced by
It seems like $charXml can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...ristic::createFromXML() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                $self->addCharacteristic(Characteristic::createFromXML(/** @scrutinizer ignore-type */ $charXml));
Loading history...
46
            }
47
        } elseif (isset($children->chracteristic)) {
48
            foreach ($children->chracteristic as $charXml) {
49
                $self->addCharacteristic(Characteristic::createFromXML($charXml));
50
            }
51
        }
52
53
        return $self;
54
    }
55
56
    public function getVisibility(): string
57
    {
58
        return $this->visibility;
59
    }
60
    public function setVisibility(string $visibility): void
61
    {
62
        $this->visibility = $visibility;
63
    }
64
65
    public function getPrice(): int
66
    {
67
        return $this->price;
68
    }
69
    public function setPrice(int $price): void
70
    {
71
        $this->price = $price;
72
    }
73
74
    public function getName(): string
75
    {
76
        return $this->name;
77
    }
78
    public function setName(string $name): void
79
    {
80
        $this->name = $name;
81
    }
82
83
    /** @return Characteristic[] */
84
    public function getCharacteristics(): array
85
    {
86
        return $this->characteristics;
87
    }
88
    public function addCharacteristic(Characteristic $characteristic): void
89
    {
90
        $this->characteristics[] = $characteristic;
91
    }
92
}