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

azjezz / psl / 17432557268

03 Sep 2025 11:52AM UTC coverage: 98.446% (-0.07%) from 98.513%
17432557268

push

github

web-flow
chore: migrate from `psalm` to `mago` (#527)

232 of 241 new or added lines in 81 files covered. (96.27%)

14 existing lines in 12 files now uncovered.

5510 of 5597 relevant lines covered (98.45%)

52.23 hits per line

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

88.89
/src/Psl/Json/encode.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Json;
6

7
use JsonException;
8
use Psl\Str;
9

10
use function json_encode;
11

12
use const JSON_PRESERVE_ZERO_FRACTION;
13
use const JSON_PRETTY_PRINT;
14
use const JSON_THROW_ON_ERROR;
15
use const JSON_UNESCAPED_SLASHES;
16
use const JSON_UNESCAPED_UNICODE;
17

18
/**
19
 * Returns a string containing the JSON representation of the supplied value.
20
 *
21
 * @pure
22
 *
23
 * @throws Exception\EncodeException If an error occurred.
24
 *
25
 * @return non-empty-string
26
 */
27
function encode(mixed $value, bool $pretty = false, int $flags = 0): string
28
{
29
    $flags |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_THROW_ON_ERROR;
10✔
30

31
    if ($pretty) {
10✔
32
        $flags |= JSON_PRETTY_PRINT;
1✔
33
    }
34

35
    try {
36
        $json = json_encode($value, $flags);
10✔
37
    } catch (JsonException $e) {
3✔
38
        throw new Exception\EncodeException(Str\format('%s.', $e->getMessage()), (int) $e->getCode(), $e);
3✔
39
    }
40

41
    if ($json === false || $json === '') {
7✔
NEW
42
        throw new Exception\EncodeException('Unexpected error occurred while encoding JSON.');
×
43
    }
44

45
    return $json;
7✔
46
}
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