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

azjezz / psl / 23100454484

15 Mar 2026 01:16AM UTC coverage: 95.628% (-2.8%) from 98.421%
23100454484

Pull #629

github

azjezz
feat(encoding): introduce streaming IO handles for Base64, QuotedPrintable, and Hex

Signed-off-by: azjezz <azjezz@protonmail.com>
Pull Request #629: feat(encoding): introduce streaming IO handles for Base64, QuotedPrintable, and Hex

479 of 797 new or added lines in 13 files covered. (60.1%)

2 existing lines in 1 file now uncovered.

10455 of 10933 relevant lines covered (95.63%)

32.65 hits per line

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

81.25
/src/Psl/Encoding/Hex/DecodingWriteHandle.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Encoding\Hex;
6

7
use Psl\Async\CancellationTokenInterface;
8
use Psl\Async\NullCancellationToken;
9
use Psl\Encoding\Exception;
10
use Psl\IO;
11

12
use function strlen;
13
use function substr;
14

15
/**
16
 * A write handle that accepts hex-encoded bytes, buffers until complete 2-byte pairs
17
 * are available, decodes and writes to the inner handle.
18
 */
19
final class DecodingWriteHandle implements IO\WriteHandleInterface
20
{
21
    use IO\WriteHandleConvenienceMethodsTrait;
22

23
    private string $remainder = '';
24

25
    public function __construct(
26
        private readonly IO\WriteHandleInterface $handle,
27
    ) {}
4✔
28

29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function tryWrite(string $bytes): int
33
    {
34
        $length = strlen($bytes);
4✔
35

36
        $data = $this->remainder . $bytes;
4✔
37

38
        $data_length = strlen($data);
4✔
39
        $usable = $data_length - ($data_length % 2);
4✔
40

41
        if ($usable > 0) {
4✔
42
            $decoded = decode(substr($data, 0, $usable));
4✔
43
            $this->handle->writeAll($decoded);
4✔
44
            $this->remainder = substr($data, $usable);
4✔
45
        } else {
NEW
46
            $this->remainder = $data;
×
47
        }
48

49
        return $length;
4✔
50
    }
51

52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function write(string $bytes, CancellationTokenInterface $cancellation = new NullCancellationToken()): int
56
    {
57
        return $this->tryWrite($bytes);
4✔
58
    }
59

60
    /**
61
     * Flush any remaining buffered hex data through the decoder to the inner handle.
62
     *
63
     * @throws Exception\RangeException If there is an odd number of hex characters remaining.
64
     */
65
    public function flush(): void
66
    {
67
        if ($this->remainder !== '') {
4✔
68
            $decoded = decode($this->remainder);
1✔
NEW
69
            $this->remainder = '';
×
NEW
70
            $this->handle->writeAll($decoded);
×
71
        }
72
    }
73
}
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