| 1 | <?php |
||
| 9 | class Integer extends Real |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Returns a Integer object given a PHP native int as parameter. |
||
| 13 | * |
||
| 14 | * @param int $value |
||
| 15 | */ |
||
| 16 | 104 | public function __construct($value) |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Tells whether two Integer are equal by comparing their values |
||
| 29 | * |
||
| 30 | * @param ValueObjectInterface $integer |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | 46 | public function sameValueAs(ValueObjectInterface $integer) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the value of the integer number |
||
| 44 | * |
||
| 45 | * @return int |
||
| 46 | */ |
||
| 47 | 83 | public function toNative() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Returns a Real with the value of the Integer |
||
| 56 | * |
||
| 57 | * @return Real |
||
| 58 | */ |
||
| 59 | 1 | public function toReal() |
|
| 66 | } |
||
| 67 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: