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

TYPO3-Headless / headless / 26439917307

26 May 2026 07:58AM UTC coverage: 70.455% (-5.0%) from 75.459%
26439917307

Pull #893

github

web-flow
Merge 00997f766 into 79b7c5472
Pull Request #893: [TASK] Reintroduce missing features, extension cleanup

360 of 523 new or added lines in 32 files covered. (68.83%)

167 existing lines in 7 files now uncovered.

1364 of 1936 relevant lines covered (70.45%)

7.36 hits per line

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

92.31
/Classes/Json/JsonDecoder.php
1
<?php
2

3
/*
4
 * This file is part of the "headless" Extension for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.md file that was distributed with this source code.
8
 */
9

10
declare(strict_types=1);
11

12
namespace FriendsOfTYPO3\Headless\Json;
13

14
use function is_array;
15
use function is_numeric;
16
use function is_object;
17
use function is_string;
18
use function json_decode;
19
use function trim;
20

21
use const PHP_VERSION_ID;
22

23
class JsonDecoder implements JsonDecoderInterface
24
{
25
    /**
26
     * @param array<mixed> $data
27
     * @return array<mixed>
28
     */
29
    public function decode(array $data): array
30
    {
31
        foreach ($data as $key => $singleData) {
59✔
32
            if (is_string($singleData)) {
54✔
33
                $decoded = $this->tryDecodeJsonString($singleData);
41✔
34
                if ($decoded !== null) {
41✔
35
                    $data[$key] = $decoded;
28✔
36
                }
37
            } elseif (is_array($singleData)) {
45✔
38
                $data[$key] = $this->decode($singleData);
34✔
39
            }
40
        }
41
        return $data;
59✔
42
    }
43

44
    public function isJson(mixed $possibleJson): bool
45
    {
46
        if (!is_string($possibleJson)) {
16✔
47
            return false;
8✔
48
        }
49

50
        return $this->tryDecodeJsonString($possibleJson) !== null;
8✔
51
    }
52

53
    /**
54
     * @return array<mixed>|object|null
55
     */
56
    private function tryDecodeJsonString(string $value): array|object|null
57
    {
58
        if (is_numeric($value)) {
49✔
59
            return null;
26✔
60
        }
61

62
        $trimmed = trim($value);
45✔
63
        if ($trimmed === '') {
45✔
64
            return null;
28✔
65
        }
66

67
        $first = $trimmed[0];
42✔
68
        $last = $trimmed[-1];
42✔
69
        if (!(($first === '{' && $last === '}') || ($first === '[' && $last === ']'))) {
42✔
70
            return null;
39✔
71
        }
72

73
        if (PHP_VERSION_ID >= 80300 && !json_validate($trimmed)) {
29✔
NEW
74
            return null;
×
75
        }
76

77
        $decoded = json_decode($trimmed);
29✔
78

79
        if (!is_object($decoded) && !is_array($decoded)) {
29✔
NEW
80
            return null;
×
81
        }
82

83
        return $decoded;
29✔
84
    }
85
}
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