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

azjezz / psl / 13067850156

31 Jan 2025 06:47AM UTC coverage: 98.465% (-0.06%) from 98.52%
13067850156

Pull #506

github

web-flow
Merge 4fe58876d into 14ad277e8
Pull Request #506: Update psalm, infection, mago, ...

13 of 14 new or added lines in 7 files covered. (92.86%)

2 existing lines in 1 file now uncovered.

5324 of 5407 relevant lines covered (98.46%)

50.86 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

© 2025 Coveralls, Inc