Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7a5a7f4

Browse files
committed
Minor CS/readability changes
- Spaces after `!` operators. - Return from conditional to remove else clause. - Return false from parseContentRange() on match failure to make return types explicit. - More detail on return types for parseContentRange(). - Use named groups in PCRE match.
1 parent c8ad5fc commit 7a5a7f4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Response/SapiStreamEmitter.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ public function emit(ResponseInterface $response, $maxBufferLength = 8192)
4141

4242
if (is_array($range)) {
4343
$this->emitBodyRange($range, $response, $maxBufferLength);
44-
} else {
45-
$this->emitBody($response, $maxBufferLength);
44+
return;
4645
}
46+
47+
$this->emitBody($response, $maxBufferLength);
4748
}
4849

4950
/**
@@ -57,7 +58,7 @@ private function emitBody(ResponseInterface $response, $maxBufferLength)
5758
$body = $response->getBody();
5859
$body->rewind();
5960

60-
while (!$body->eof()) {
61+
while (! $body->eof()) {
6162
echo $body->read($maxBufferLength);
6263
}
6364
}
@@ -78,7 +79,7 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
7879
$body->seek($first);
7980
$pos = $first;
8081

81-
while (!$body->eof() && $pos < $last) {
82+
while (! $body->eof() && $pos < $last) {
8283
if (($pos + $maxBufferLength) > $last) {
8384
echo $body->read($last - $pos);
8485
break;
@@ -94,17 +95,19 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
9495
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
9596
*
9697
* @param string $header
97-
* @return array [unit, first, last, lenght]
98+
* @return false|array [unit, first, last, length]; returns false if no
99+
* content range or an invalid content range is provided
98100
*/
99101
private function parseContentRange($header)
100102
{
101-
if (preg_match('/([\w]+)\s+(\d+)-(\d+)\/(\d+|\*)/', $header, $matches)) {
103+
if (preg_match('/(?P<unit>[\w]+)\s+(?P<first>\d+)-(?P<last>\d+)\/(?P<length>\d+|\*)/', $header, $matches)) {
102104
return [
103-
$matches[1],
104-
(int) $matches[2],
105-
(int) $matches[3],
106-
$matches[4] === '*' ? '*' : (int) $matches[4],
105+
$matches['unit'],
106+
(int) $matches['first'],
107+
(int) $matches['last'],
108+
$matches['length'] === '*' ? '*' : (int) $matches['length'],
107109
];
108110
}
111+
return false;
109112
}
110113
}

0 commit comments

Comments
 (0)