1 | <?php |
||
18 | class MemoryStream implements InputOutputStream, Resettable |
||
19 | { |
||
20 | use Interpolator { |
||
21 | interpolate as tinterpolate; |
||
22 | } |
||
23 | /** |
||
24 | * The local memory buffer |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $memory; |
||
29 | |||
30 | /** |
||
31 | * Current position in memory buffer |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | private $current; |
||
36 | |||
37 | /** |
||
38 | * Whether it is possible to perform reading action |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $ready; |
||
43 | |||
44 | /** |
||
45 | * Whether stream is closed |
||
46 | * |
||
47 | * @var boolean |
||
48 | */ |
||
49 | private $closed; |
||
50 | |||
51 | /** |
||
52 | * Create a new MemoryStream |
||
53 | * |
||
54 | * @param InputStream $in |
||
55 | * optional existing input stream - will be copied |
||
56 | */ |
||
57 | 31 | public function __construct(InputStream $in = null) |
|
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | * @see \Generics\Streams\Stream::close() |
||
76 | */ |
||
77 | public function close() |
||
84 | 5 | ||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | * @see \Generics\Streams\Stream::ready() |
||
88 | */ |
||
89 | public function ready() |
||
93 | 21 | ||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | * @see \Generics\Streams\OutputStream::write() |
||
97 | */ |
||
98 | public function write($buffer) |
||
106 | 28 | ||
107 | 28 | /** |
|
108 | 28 | * {@inheritDoc} |
|
109 | * @see \Generics\Streams\InputStream::read() |
||
110 | */ |
||
111 | public function read($length = 1, $offset = null) |
||
139 | 23 | ||
140 | /** |
||
141 | 23 | * {@inheritDoc} |
|
142 | * @see \Countable::count() |
||
143 | */ |
||
144 | public function count() |
||
154 | 9 | ||
155 | 1 | /** |
|
156 | * {@inheritDoc} |
||
157 | 8 | * @see \Generics\Resettable::reset() |
|
158 | */ |
||
159 | public function reset() |
||
167 | 10 | ||
168 | 1 | /** |
|
169 | * Write to stream by interpolation of context vars into a string |
||
170 | 9 | * |
|
171 | 9 | * @param string $string |
|
172 | 9 | * The string to interpolate, may contains placeholders in format {placeholder}. |
|
173 | * @param array $context |
||
174 | * The context array containing the associative replacers and its values. |
||
175 | */ |
||
176 | public function interpolate($string, array $context) |
||
180 | |||
181 | /** |
||
182 | 20 | * {@inheritDoc} |
|
183 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
184 | 20 | */ |
|
185 | 20 | public function isWriteable() |
|
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | 1 | * @see \Generics\Streams\OutputStream::flush() |
|
193 | */ |
||
194 | 1 | public function flush() |
|
203 | |||
204 | 2 | /** |
|
205 | 1 | * {@inheritDoc} |
|
206 | * @see \Generics\Streams\Stream::isOpen() |
||
207 | */ |
||
208 | 1 | public function isOpen() |
|
212 | } |
||
213 |
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: