Skip to content

Commit ef81edf

Browse files
committed
Refactor to remove outdated code and achieve 100% test coverage
1 parent 3c04e99 commit ef81edf

File tree

5 files changed

+27
-134
lines changed

5 files changed

+27
-134
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"require": {
1414
"php": ">=7.0",
1515
"ext-zlib": "*",
16-
"clue/stream-filter": "~1.3",
1716
"react/stream": "^1.0 || ^0.7 || ^0.6"
1817
},
1918
"require-dev": {

src/Compressor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function transformData($chunk)
5555
$ret = deflate_add($this->context, $chunk, ZLIB_NO_FLUSH);
5656

5757
if ($ret !== '') {
58-
$this->forwardData($ret);
58+
$this->emit('data', [$ret]);
5959
}
6060
}
6161

@@ -65,9 +65,10 @@ protected function transformEnd($chunk)
6565
$this->context = null;
6666

6767
if ($ret !== '') {
68-
$this->forwardData($ret);
68+
$this->emit('data', [$ret]);
6969
}
7070

71-
$this->forwardEnd();
71+
$this->emit('end');
72+
$this->close();
7273
}
7374
}

src/Decompressor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function transformData($chunk)
5757
}
5858

5959
if ($ret !== '') {
60-
$this->forwardData($ret);
60+
$this->emit('data', [$ret]);
6161
}
6262
}
6363

@@ -71,9 +71,10 @@ protected function transformEnd($chunk)
7171
}
7272

7373
if ($ret !== '') {
74-
$this->forwardData($ret);
74+
$this->emit('data', [$ret]);
7575
}
7676

77-
$this->forwardEnd();
77+
$this->emit('end');
78+
$this->close();
7879
}
7980
}

src/TransformStream.php

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function write($data)
3535

3636
return true;
3737
} catch (Exception $e) {
38-
$this->forwardError($e);
38+
$this->emit('error', [$e]);
39+
$this->close();
3940
return false;
4041
}
4142
}
@@ -53,7 +54,8 @@ public function end($data = null)
5354
}
5455
$this->transformEnd($data);
5556
} catch (Exception $e) {
56-
$this->forwardError($e);
57+
$this->emit('error', [$e]);
58+
$this->close();
5759
}
5860
}
5961

@@ -107,109 +109,49 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
107109
return $dest;
108110
}
109111

110-
/**
111-
* Forwards a single "data" event to the reading side of the stream
112-
*
113-
* This will emit an "data" event.
114-
*
115-
* If the stream is not readable, then this is a NO-OP.
116-
*
117-
* @param string $data
118-
*/
119-
protected function forwardData($data)
120-
{
121-
if (!$this->readable) {
122-
return;
123-
}
124-
$this->emit('data', array($data));
125-
}
126-
127-
/**
128-
* Forwards an "end" event to the reading side of the stream
129-
*
130-
* This will emit an "end" event and will then close this stream.
131-
*
132-
* If the stream is not readable, then this is a NO-OP.
133-
*
134-
* @uses self::close()
135-
*/
136-
protected function forwardEnd()
137-
{
138-
if (!$this->readable) {
139-
return;
140-
}
141-
$this->readable = false;
142-
$this->writable = false;
143-
144-
$this->emit('end');
145-
$this->close();
146-
}
147-
148-
/**
149-
* Forwards the given $error message to the reading side of the stream
150-
*
151-
* This will emit an "error" event and will then close this stream.
152-
*
153-
* If the stream is not readable, then this is a NO-OP.
154-
*
155-
* @param Exception $error
156-
* @uses self::close()
157-
*/
158-
protected function forwardError(Exception $error)
159-
{
160-
if (!$this->readable) {
161-
return;
162-
}
163-
$this->readable = false;
164-
$this->writable = false;
165-
166-
$this->emit('error', array($error));
167-
$this->close();
168-
}
169-
170112
/**
171113
* can be overwritten in order to implement custom transformation behavior
172114
*
173-
* This gets passed a single chunk of $data and should invoke `forwardData()`
115+
* This gets passed a single chunk of $data and should emit a `data` event
174116
* with the filtered result.
175117
*
176-
* If the given data chunk is not valid, then you should invoke `forwardError()`
177-
* or throw an Exception.
118+
* If the given data chunk is not valid, then you should throw an Exception
119+
* which will automatically be turned into an `error` event.
178120
*
179-
* If you do not overwrite this method, then its default implementation simply
180-
* invokes `forwardData()` on the unmodified input data chunk.
121+
* If you do not overwrite this method, then its default implementation
122+
* simply emits a `data` event with the unmodified input data chunk.
181123
*
182124
* @param string $data
183-
* @see self::forwardData()
184125
*/
185126
protected function transformData($data)
186127
{
187-
$this->forwardData($data);
128+
$this->emit('data', [$data]);
188129
}
189130

190131
/**
191132
* can be overwritten in order to implement custom stream ending behavior
192133
*
193-
* This may get passed a single final chunk of $data and should invoke `forwardEnd()`.
134+
* This may get passed a single final chunk of $data and should emit an
135+
* `end` event and close the stream.
194136
*
195-
* If the given data chunk is not valid, then you should invoke `forwardError()`
196-
* or throw an Exception.
137+
* If the given data chunk is not valid, then you should throw an Exception
138+
* which will automatically be turned into an `error` event.
197139
*
198140
* If you do not overwrite this method, then its default implementation simply
199141
* invokes `transformData()` on the unmodified input data chunk (if any),
200-
* which in turn defaults to invoking `forwardData()` and then finally
201-
* invokes `forwardEnd()`.
142+
* which in turn defaults to emitting a `data` event and then finally
143+
* emits an `end` event and closes the stream.
202144
*
203145
* @param string $data
204146
* @see self::transformData()
205-
* @see self::forwardData()
206-
* @see self::forwardEnd()
207147
*/
208148
protected function transformEnd($data)
209149
{
210150
if ($data !== '') {
211151
$this->transformData($data);
212152
}
213-
$this->forwardEnd();
153+
154+
$this->emit('end');
155+
$this->close();
214156
}
215157
}

src/ZlibFilterStream.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)