• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 13230608391

09 Feb 2025 11:27PM UTC coverage: 98.466% (+0.001%) from 98.465%
13230608391

Pull #504

github

web-flow
Merge 903a56fa6 into 96a3e7279
Pull Request #504: chore: benchmarks for `Collection` component

17 of 18 new or added lines in 9 files covered. (94.44%)

1 existing line in 1 file now uncovered.

5326 of 5409 relevant lines covered (98.47%)

50.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/Psl/Shell/stream_unpack.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Shell;
6

7
use Generator;
8
use Psl\Str;
9

10
use function unpack as byte_unpack;
11

12
/**
13
 * Stream unpack the result of `Shell\execute()` when using `ErrorOutputBehavior::Packed` error output behavior,
14
 * maintaining the outputting order, chunk by chunk.
15
 *
16
 * @param string $content
17
 *
18
 * @throws Exception\InvalidArgumentException If $content is invalid.
19
 *
20
 * @return Generator<1|2, string, null, void> Generator where the key is either 1 ( representing the standard output ),
21
 *                                            or 2 ( representing the standard error output ), and the value is the output chunk.
22
 *
23
 * Example:
24
 *
25
 *      Shell\stream_unpack(
26
 *          Shell\execute('php', ['-r', 'fwrite(STDOUT, "a"); fwrite(STDERR, "b"); fwrite(STDOUT, "c");'], null, [], ErrorOutputBehavior::Packed),
27
 *      );
28
 *      => Generator(1 => "a", 2 => "b", 1 => "c")
29
 */
30
function stream_unpack(string $content): Generator
31
{
32
    while ($content !== '') {
20✔
33
        if (Str\Byte\length($content) < 5) {
19✔
34
            throw new Exception\InvalidArgumentException('$content contains an invalid header value.');
1✔
35
        }
36

37
        $headers = byte_unpack('C1type/N1size', Str\Byte\slice($content, 0, 5));
19✔
38
        if ($headers === false) {
19✔
NEW
39
            throw new Exception\InvalidArgumentException('$content contains an invalid header value.');
×
40
        }
41

42
        /** @var int<0, max> $type */
43
        $type = (int) $headers['type'];
19✔
44
        /** @var int<0, max> $size */
45
        $size = (int) $headers['size'];
19✔
46

47
        if ($size > (Str\Byte\length($content) - 5)) {
19✔
48
            throw new Exception\InvalidArgumentException('$content contains an invalid header value.');
2✔
49
        }
50

51
        $chunk = Str\Byte\slice($content, 5, $size);
19✔
52
        $content = Str\Byte\slice($content, $size + 5);
19✔
53

54
        if ($type === 1 || $type === 2) {
19✔
55
            yield $type => $chunk;
19✔
56
        } else {
57
            throw new Exception\InvalidArgumentException('$content contains an invalid header value.');
2✔
58
        }
59
    }
60
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc