1 | <?php |
||
5 | abstract class AbstractTypedCollection extends AbstractCollection |
||
6 | { |
||
7 | /** |
||
8 | * @var \Closure The function used to check if an object is belongs to the type of this collection |
||
9 | * This function must take one argument and return true when args is in valid type (false otherwise) |
||
10 | */ |
||
11 | private $validateTypeFct; |
||
12 | |||
13 | /** |
||
14 | * @var \Closure The function used to check if two object are equals |
||
15 | * This function must take two arguments and return true when arguments are equals (false otherwise) |
||
16 | */ |
||
17 | private $equalsFct; |
||
18 | |||
19 | /** |
||
20 | * AbstractTypedCollection constructor. |
||
21 | * |
||
22 | * @param string|\Closure $type Two value are possible for this field. |
||
23 | * 1. The class name of object you want to store in this collection |
||
24 | * 2. A function which take one arguments and return true when it in the good type (false otherwise) |
||
25 | * @param null|\Closure $equalsFct When you pass null, the object will use the function \Equatable::equals if exist, else use the === |
||
26 | * When you pass a Closure, the function must take two arguments and return true when 2 arguments are equals |
||
27 | */ |
||
28 | 10 | public function __construct($type, $equalsFct = null) |
|
52 | |||
53 | /** |
||
54 | * Check if an object can be manage by the current collection |
||
55 | * @param $object The object you want to test |
||
56 | * @return bool True if can be added false otherwise |
||
57 | */ |
||
58 | 10 | public function validateType($object) : bool |
|
62 | |||
63 | /** |
||
64 | * Check if two object are equals |
||
65 | * @param $object1 First object you want to compare to |
||
66 | * @param $object2 Second object you want to compare |
||
67 | * @return bool True if objects are equals false otherwise |
||
68 | */ |
||
69 | 8 | private function checkEquality($object1, $object2) : bool |
|
73 | |||
74 | /** |
||
75 | * Check if an object is present in the collection. |
||
76 | * The check will be done by `Equatable::equals()` method if exist else use `===` |
||
77 | * |
||
78 | * @param $objectToFind The object you want to check if it's already present. |
||
79 | * |
||
80 | * @return bool True if present else false |
||
81 | * @throws \TypeError When object in parameter is not good |
||
82 | */ |
||
83 | 8 | public function contains($objectToFind) : bool |
|
96 | } |
||
97 |
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.