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

move-elevator / composer-translation-validator / 18559956393

16 Oct 2025 11:36AM UTC coverage: 95.519%. Remained the same
18559956393

Pull #73

github

jackd248
Merge remote-tracking branch 'origin/main' into php-cs-fixer-preset
Pull Request #73: build: add php-cs-fixer-preset

206 of 210 new or added lines in 16 files covered. (98.1%)

91 existing lines in 20 files now uncovered.

2345 of 2455 relevant lines covered (95.52%)

7.73 hits per line

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

97.87
/src/Parser/JsonParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the "composer-translation-validator" Composer plugin.
7
 *
8
 * (c) 2025 Konrad Michalik <km@move-elevator.de>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace MoveElevator\ComposerTranslationValidator\Parser;
15

16
use JsonException;
17
use RuntimeException;
18

19
use function array_key_exists;
20
use function is_array;
21
use function is_string;
22
use function sprintf;
23

24
/**
25
 * JsonParser.
26
 *
27
 * @author Konrad Michalik <km@move-elevator.de>
28
 * @license GPL-3.0-or-later
29
 */
30
class JsonParser extends AbstractParser implements ParserInterface
31
{
32
    /** @var array<string, mixed> */
33
    private array $json = [];
34

35
    public function __construct(string $filePath)
15✔
36
    {
37
        parent::__construct($filePath);
15✔
38

39
        try {
40
            $content = file_get_contents($filePath);
12✔
41
            if (false === $content) {
12✔
UNCOV
42
                throw new RuntimeException("Failed to read file: {$filePath}");
×
43
            }
44

45
            $decoded = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
12✔
46
            if (!is_array($decoded) || array_is_list($decoded)) {
10✔
47
                throw new RuntimeException("JSON file does not contain an object: {$filePath}");
1✔
48
            }
49

50
            $this->json = $decoded;
9✔
51
        } catch (JsonException $e) {
3✔
52
            throw new RuntimeException(sprintf('Failed to parse JSON file "%s": %s', $filePath, $e->getMessage()), 0, $e);
2✔
53
        }
54
    }
55

56
    /**
57
     * @return array<int, string>|null
58
     */
59
    public function extractKeys(): ?array
2✔
60
    {
61
        $extract = static function (
2✔
62
            array $data,
2✔
63
            string $prefix = '',
2✔
64
        ) use (&$extract): array {
2✔
65
            $keys = [];
2✔
66
            foreach ($data as $key => $value) {
2✔
67
                $fullKey = '' === $prefix
2✔
68
                    ? $key
2✔
69
                    : $prefix.'.'.$key;
2✔
70
                if (is_array($value)) {
2✔
71
                    $extracted = $extract($value, $fullKey);
2✔
72
                    foreach ($extracted as $k) {
2✔
73
                        $keys[] = $k;
2✔
74
                    }
75
                } else {
76
                    $keys[] = $fullKey;
2✔
77
                }
78
            }
79

80
            return $keys;
2✔
81
        };
2✔
82

83
        return $extract($this->json);
2✔
84
    }
85

86
    public function getContentByKey(string $key): ?string
3✔
87
    {
88
        // Note: the $attribute parameter is required by ParserInterface
89
        // but is not used for JSON, since JSON has no source/target concept.
90
        $parts = explode('.', $key);
3✔
91
        $value = $this->json;
3✔
92

93
        foreach ($parts as $part) {
3✔
94
            if (is_array($value) && array_key_exists($part, $value)) {
3✔
95
                $value = $value[$part];
3✔
96
            } else {
97
                return null;
2✔
98
            }
99
        }
100

101
        return is_string($value) ? $value : null;
3✔
102
    }
103

104
    /**
105
     * @return array<int, string>
106
     */
107
    public static function getSupportedFileExtensions(): array
26✔
108
    {
109
        return ['json'];
26✔
110
    }
111

112
    public function getLanguage(): string
2✔
113
    {
114
        if (preg_match(
2✔
115
            '/\.([a-z]{2})(?:[-_][A-Z]{2})?\./',
2✔
116
            $this->getFileName(),
2✔
117
            $matches,
2✔
118
        )) {
2✔
119
            return $matches[1];
2✔
120
        }
121

122
        return '';
2✔
123
    }
124
}
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