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

azjezz / psl / 23095146612

14 Mar 2026 07:51PM UTC coverage: 98.421% (-0.06%) from 98.483%
23095146612

push

github

web-flow
feat(encoding): introduce `Psl\Encoding\EncodedWord` (#628)

111 of 119 new or added lines in 12 files covered. (93.28%)

2 existing lines in 1 file now uncovered.

9976 of 10136 relevant lines covered (98.42%)

34.65 hits per line

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

96.3
/src/Psl/Encoding/EncodedWord/decode.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Encoding\EncodedWord;
6

7
use Psl\Encoding\Exception;
8

9
use function preg_match_all;
10
use function strlen;
11
use function strpos;
12
use function substr;
13
use function trim;
14

15
use const PREG_SET_ORDER;
16

17
/**
18
 * Decode RFC 2047 encoded-words in a header value.
19
 *
20
 * Handles both B-encoding (base64) and Q-encoding. Per RFC 2047 ยง6.2,
21
 * whitespace between adjacent encoded-words is removed.
22
 *
23
 * @throws Exception\ParsingException If an encoded-word is malformed.
24
 *
25
 * @link https://datatracker.ietf.org/doc/html/rfc2047
26
 */
27
function decode(string $input): string
28
{
29
    if ($input === '') {
20✔
30
        return '';
1✔
31
    }
32

33
    $pattern = '/=\?([^?]+)\?([BbQq])\?([^?]*)\?=/';
19✔
34

35
    /** @var list<array{0: string, 1: string, 2: string, 3: string}> $matches */
36
    $matches = [];
19✔
37
    if (preg_match_all($pattern, $input, $matches, PREG_SET_ORDER) === 0) {
19✔
38
        return $input;
3✔
39
    }
40

41
    $result = '';
16✔
42
    $lastEnd = 0;
16✔
43
    $lastWasEncoded = false;
16✔
44

45
    foreach ($matches as $match) {
16✔
46
        $full = $match[0];
16✔
47
        $charset = $match[1];
16✔
48
        $encoding = $match[2];
16✔
49
        $encodedText = $match[3];
16✔
50

51
        $matchStart = strpos($input, $full, $lastEnd);
16✔
52
        if ($matchStart === false) {
16✔
NEW
53
            continue;
×
54
        }
55

56
        $matchEnd = $matchStart + strlen($full);
16✔
57

58
        $between = substr($input, $lastEnd, $matchStart - $lastEnd);
16✔
59

60
        if (!$lastWasEncoded || trim($between) !== '') {
16✔
61
            $result .= $between;
16✔
62
        }
63

64
        $decoded = Internal\decode_payload($encoding, $encodedText);
16✔
65
        $result .= Internal\convert_charset($charset, $decoded);
15✔
66

67
        $lastEnd = $matchEnd;
15✔
68
        $lastWasEncoded = true;
15✔
69
    }
70

71
    $result .= substr($input, $lastEnd);
15✔
72

73
    return $result;
15✔
74
}
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