1 | <?php |
||
26 | class Reader |
||
27 | { |
||
28 | /** |
||
29 | * @var Readable |
||
30 | */ |
||
31 | private $file; |
||
32 | |||
33 | /** |
||
34 | * @var ParserInterface |
||
35 | */ |
||
36 | private $pp; |
||
37 | |||
38 | /** |
||
39 | * @var LexerInterface |
||
40 | */ |
||
41 | private $lexer; |
||
42 | |||
43 | /** |
||
44 | * @var GrammarInterface |
||
45 | */ |
||
46 | private $grammar; |
||
47 | |||
48 | /** |
||
49 | * @var Analyzer |
||
50 | */ |
||
51 | private $analyzer; |
||
52 | |||
53 | /** |
||
54 | * Reader constructor. |
||
55 | * @param Readable $file |
||
56 | */ |
||
57 | public function __construct(Readable $file) |
||
67 | |||
68 | /** |
||
69 | * @return void |
||
70 | */ |
||
71 | private function boot(): void |
||
77 | |||
78 | /** |
||
79 | * @return ParserInterface |
||
80 | * @throws \Railt\Io\Exception\ExternalFileException |
||
81 | * @throws \Railt\Io\Exception\NotReadableException |
||
82 | */ |
||
83 | public function getParser(): ParserInterface |
||
93 | |||
94 | /** |
||
95 | * @param Readable $file |
||
96 | * @throws \Railt\Io\Exception\ExternalFileException |
||
97 | * @throws \Railt\Io\Exception\NotReadableException |
||
98 | */ |
||
99 | private function addGrammar(Readable $file): void |
||
125 | } |
||
126 |
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: