| 1 | <?php |
||
| 15 | class IteratorApplySalesman |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Callable |
||
| 20 | */ |
||
| 21 | public $salesman_factory; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Callable |
||
| 26 | */ |
||
| 27 | public $apply; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @param callable $salesman_factory Callable that takes a Salesman ID and returns a Salesman instance. |
||
| 32 | * @param SalesmanInterface $salesman_fallback Optional SalesmanInterface fallback instance |
||
| 33 | * @param callable $apply Optional Callable that takes the current item on iteration. |
||
| 34 | */ |
||
| 35 | public function __construct( callable $salesman_factory, SalesmanInterface $salesman_fallback = null, callable $apply = null ) |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Must return TRUE, according to PHP's iterator_applay. |
||
| 44 | * |
||
| 45 | * @see http://php.net/manual/de/function.iterator-apply.php |
||
| 46 | */ |
||
| 47 | public function __invoke( \Traversable $iterator ) |
||
| 61 | |||
| 62 | |||
| 63 | } |
||
| 64 |
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: