Completed
Push — master ( 686ecf...037cc5 )
by Guillermo A.
09:28
created

AbstractModel::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Guillermoandrae\Highrise\Models;
4
5
use DateTime;
6
use SimpleXMLElement;
7
use Guillermoandrae\Models\AbstractModel as BaseAbstractModel;
8
9
abstract class AbstractModel extends BaseAbstractModel implements ModelInterface
10
{
11
    /**
12
     * @var SimpleXMLElement
13
     */
14
    protected $xml;
15
16
    /**
17
     * The entity name.
18
     *
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * The date on which the entity was created.
25
     *
26
     * @var DateTime
27
     */
28
    protected $createdAt;
29
30
    /**
31
     * The date on which the entity was updated.
32
     *
33
     * @var DateTime
34
     */
35
    protected $updatedAt;
36
37
    public function __construct(string $xml)
38
    {
39
        $this->xml = simplexml_load_string($xml);
0 ignored issues
show
Documentation Bug introduced by
It seems like simplexml_load_string($xml) can also be of type false. However, the property $xml is declared as type SimpleXMLElement. 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...
40
        $this->id = (int) $this->xml->xpath('//id');
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
        $this->name = (string) $this->xml->xpath('//name')[0];
42
        $this->createdAt = new DateTime((string) $this->xml->xpath('//created-at')[0]);
43
        $this->updatedAt = new DateTime((string) $this->xml->xpath('//updated-at')[0]);
44
    }
45
46
    /**
47
     * Returns the SimpleXMLElement.
48
     *
49
     * @return SimpleXMLElement
50
     */
51
    final public function getXml(): SimpleXMLElement
52
    {
53
        return $this->xml;
54
    }
55
56
    /**
57
     * Returns the entity name;
58
     *
59
     * @return string
60
     */
61
    public function getName(): string
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * Returns the date on which the entity was created.
68
     *
69
     * @return DateTime
70
     */
71
    public function getCreatedAt(): DateTime
72
    {
73
        return $this->createdAt;
74
    }
75
76
    /**
77
     * Returns the date on which the entity was updated.
78
     *
79
     * @return DateTime
80
     */
81
    public function getUpdatedAt(): DateTime
82
    {
83
        return $this->updatedAt;
84
    }
85
86
    public function toArray(): array
87
    {
88
        return [];
89
    }
90
}
91