1 | <?php |
||
24 | final class UserId |
||
25 | { |
||
26 | /** |
||
27 | * The id in a primitive type. |
||
28 | * |
||
29 | * @var string|int |
||
30 | */ |
||
31 | private $id; |
||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | * |
||
36 | * @param string|int|null $anId The id in a primitive type |
||
37 | * |
||
38 | * @throws UserIdInvalidException when the id is not valid |
||
39 | */ |
||
40 | public function __construct($anId = null) |
||
47 | |||
48 | /** |
||
49 | * Gets the id. |
||
50 | * |
||
51 | * @return string|int |
||
52 | */ |
||
53 | public function id() |
||
57 | |||
58 | /** |
||
59 | * Method that checks if the id given is equal to the current. |
||
60 | * |
||
61 | * @param UserId $anId The id |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function equals(UserId $anId) |
||
69 | |||
70 | /** |
||
71 | * Magic method that represents the user id in string format. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function __toString() |
||
79 | } |
||
80 |
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.