AbstractModel::getId()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 model ID.
18
     *
19
     * @var int
20
     */
21
    protected $id;
22
23
    /**
24
     * The model name.
25
     *
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * The date on which the model was created.
32
     *
33
     * @var DateTime
34
     */
35
    protected $createdAt;
36
37
    /**
38
     * The date on which the model was updated.
39
     *
40
     * @var DateTime
41
     */
42
    protected $updatedAt;
43
44
    public function __construct(string $xml)
45
    {
46
        $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...
47
        $this->id = (int) (string) $this->xml->id;
48
        $this->name = (string) $this->xml->xpath('//name')[0];
49
        $this->createdAt = new DateTime((string) $this->xml->xpath('//created-at')[0]);
50
        $this->updatedAt = new DateTime((string) $this->xml->xpath('//updated-at')[0]);
51
    }
52
53
    final public function getXml(): SimpleXMLElement
54
    {
55
        return $this->xml;
56
    }
57
58
    final public function getId(): int
59
    {
60
        return $this->id;
61
    }
62
63
    final public function getName(): string
64
    {
65
        return $this->name;
66
    }
67
68
    final public function getCreatedAt(): DateTime
69
    {
70
        return $this->createdAt;
71
    }
72
73
    final public function getUpdatedAt(): DateTime
74
    {
75
        return $this->updatedAt;
76
    }
77
78
    public function toArray(): array
79
    {
80
        return [];
81
    }
82
}
83