Skip to content

Commit b292ccb

Browse files
authored
Merge pull request #27 from clue-labs/decoder-types
Check type of incoming `data` before trying to decode CSV
2 parents 5269154 + c7946a7 commit b292ccb

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Decoder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
8888
/** @internal */
8989
public function handleData($data)
9090
{
91+
if (!\is_string($data)) {
92+
$this->handleError(new \UnexpectedValueException('Expected stream to emit string, but got ' . \gettype($data)));
93+
return;
94+
}
95+
9196
$this->buffer .= $data;
9297

9398
// keep parsing while a newline has been found

tests/DecoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ public function testEmitDataWithBufferSizeOverflowWillForwardOverflowError()
160160
$this->input->emit('data', array("hello\n"));
161161
}
162162

163+
public function testEmitDataWithInvalidTypeWillForwardErrorWithUnexpectedValueException()
164+
{
165+
$this->decoder->on('data', $this->expectCallableNever());
166+
$this->decoder->on('error', $this->expectCallableOnceWith($this->isInstanceOf('UnexpectedValueException')));
167+
168+
$this->input->emit('data', array(false));
169+
}
170+
163171
public function testEmitEndWillForwardEnd()
164172
{
165173
$this->decoder->on('data', $this->expectCallableNever());

0 commit comments

Comments
 (0)