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
|
|||
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 |
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 theid
property of an instance of theAccount
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.