| 1 | <?php |
||
| 7 | class StaticGraphicRenderer extends AbstractRenderer |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var callable |
||
| 11 | */ |
||
| 12 | protected $graphicResolver; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param callable $graphicResolver |
||
| 16 | */ |
||
| 17 | public function setGraphicResolver(callable $graphicResolver) |
||
| 18 | { |
||
| 19 | $this->graphicResolver = $graphicResolver; |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return callable |
||
| 24 | */ |
||
| 25 | protected function getGraphicResolver() |
||
| 26 | { |
||
| 27 | return $this->graphicResolver; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | protected function getGraphic() |
||
| 34 | { |
||
| 35 | return $this->parser->getGraphic(); |
||
|
|
|||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return Intervention/Image/Image |
||
| 40 | */ |
||
| 41 | 1 | public function render() |
|
| 49 | } |
||
| 50 |
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: