1 | <?php |
||
23 | class RateFactory implements FactoryInterface |
||
24 | { |
||
25 | /** |
||
26 | * Make a new rate instance. |
||
27 | * |
||
28 | * TODO: abstract the return value via contract |
||
29 | * |
||
30 | * @param \Projectmentor\Quota\Contracts\PayloadInterface $data |
||
31 | * @return \bandwidthThrottle\tokenBucket\Rate |
||
32 | */ |
||
33 | 1 | public function make(PayloadInterface $data) |
|
37 | |||
38 | /** |
||
39 | * Retrieve value for constant defined in Rate::class. |
||
40 | * Convienience method. |
||
41 | * |
||
42 | * @param string $key constant name |
||
43 | * @return string constant value |
||
44 | * @throws InvalidArgumentException |
||
45 | */ |
||
46 | 1 | public function getConstant($key) |
|
59 | |||
60 | /** |
||
61 | * Retrieve an array of constants defined in Rate::class. |
||
62 | * Convienience method. |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | 2 | public function getConstants() |
|
71 | } |
||
72 |
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: