Line::getNumberOfItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Bpost\BpostApiClient\Bpost\Order;
3
4
/**
5
 * bPost Line class
6
 *
7
 * @author Tijs Verkoyen <[email protected]>
8
 */
9
class Line
10
{
11
    /**
12
     * @var string
13
     */
14
    private $text;
15
16
    /**
17
     * @var int
18
     */
19
    private $numberOfItems;
20
21
    /**
22
     * @param int $nbOfItems
23
     */
24 3
    public function setNumberOfItems($nbOfItems)
25
    {
26 3
        $this->numberOfItems = $nbOfItems;
27 3
    }
28
29
    /**
30
     * @return int
31
     */
32 2
    public function getNumberOfItems()
33
    {
34 2
        return $this->numberOfItems;
35
    }
36
37
    /**
38
     * @param string $text
39
     */
40 3
    public function setText($text)
41
    {
42 3
        $this->text = $text;
43 3
    }
44
45
    /**
46
     * @return string
47
     */
48 2
    public function getText()
49
    {
50 2
        return $this->text;
51
    }
52
53
    /**
54
     * @param string $text
0 ignored issues
show
Documentation introduced by
Should the type for parameter $text not be string|null?

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...
55
     * @param int    $numberOfItems
0 ignored issues
show
Documentation introduced by
Should the type for parameter $numberOfItems not be integer|null?

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...
56
     */
57 3
    public function __construct($text = null, $numberOfItems = null)
58
    {
59 3
        if ($text != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $text of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
60 2
            $this->setText($text);
61 2
        }
62 3
        if ($numberOfItems != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $numberOfItems of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
63 2
            $this->setNumberOfItems($numberOfItems);
64 2
        }
65 3
    }
66
67
    /**
68
     * Return the object as an array for usage in the XML
69
     *
70
     * @param  \DomDocument $document
71
     * @param  string       $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

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...
72
     * @return \DOMElement
73
     */
74 2
    public function toXML(\DomDocument $document, $prefix = null)
75
    {
76 2
        $tagName = 'orderLine';
77 2
        if ($prefix !== null) {
78 1
            $tagName = $prefix . ':' . $tagName;
79 1
        }
80
81 2
        $line = $document->createElement($tagName);
82
83 2 View Code Duplication
        if ($this->getText() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 2
            $tagName = 'text';
85 2
            if ($prefix !== null) {
86 1
                $tagName = $prefix . ':' . $tagName;
87 1
            }
88 2
            $line->appendChild(
89 2
                $document->createElement(
90 2
                    $tagName,
91 2
                    $this->getText()
92 2
                )
93 2
            );
94 2
        }
95 2
        if ($this->getNumberOfItems() !== null) {
96 2
            $tagName = 'nbOfItems';
97 2
            if ($prefix !== null) {
98 1
                $tagName = $prefix . ':' . $tagName;
99 1
            }
100 2
            $line->appendChild(
101 2
                $document->createElement(
102 2
                    $tagName,
103 2
                    $this->getNumberOfItems()
104 2
                )
105 2
            );
106 2
        }
107
108 2
        return $line;
109
    }
110
111
    /**
112
     * @param  \SimpleXMLElement $xml
113
     * @return Line
114
     */
115 1
    public static function createFromXML(\SimpleXMLElement $xml)
116
    {
117 1
        $line = new Line();
118 1
        if (isset($xml->text) && $xml->text != '') {
119 1
            $line->setText((string) $xml->text);
120 1
        }
121 1
        if (isset($xml->nbOfItems) && $xml->nbOfItems != '') {
122 1
            $line->setNumberOfItems((int) $xml->nbOfItems);
123 1
        }
124
125 1
        return $line;
126
    }
127
}
128