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

move-elevator / composer-translation-validator / 16259259799

14 Jul 2025 06:04AM UTC coverage: 96.329% (+0.05%) from 96.279%
16259259799

push

github

web-flow
Merge pull request #31 from move-elevator/json-support

feat: add json support

52 of 53 new or added lines in 7 files covered. (98.11%)

1522 of 1580 relevant lines covered (96.33%)

7.44 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
namespace MoveElevator\ComposerTranslationValidator\Parser;
6

7
class JsonParser extends AbstractParser implements ParserInterface
8
{
9
    /** @var array<string, mixed> */
10
    private array $json = [];
11

12
    public function __construct(protected string $filePath)
13✔
13
    {
14
        parent::__construct($filePath);
13✔
15

16
        try {
17
            $content = file_get_contents($filePath);
10✔
18
            if (false === $content) {
10✔
NEW
19
                throw new \RuntimeException("Failed to read file: {$filePath}");
×
20
            }
21

22
            $decoded = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
10✔
23
            if (!is_array($decoded) || array_is_list($decoded)) {
8✔
24
                throw new \RuntimeException("JSON file does not contain an object: {$filePath}");
1✔
25
            }
26

27
            $this->json = $decoded;
7✔
28
        } catch (\JsonException $e) {
3✔
29
            throw new \RuntimeException(sprintf('Failed to parse JSON file "%s": %s', $filePath, $e->getMessage()), 0, $e);
2✔
30
        }
31
    }
32

33
    /**
34
     * @return array<int, string>|null
35
     */
36
    public function extractKeys(): ?array
2✔
37
    {
38
        $extract = static function (
2✔
39
            array $data,
2✔
40
            string $prefix = '',
2✔
41
        ) use (&$extract): array {
2✔
42
            $keys = [];
2✔
43
            foreach ($data as $key => $value) {
2✔
44
                $fullKey = '' === $prefix
2✔
45
                    ? $key
2✔
46
                    : $prefix.'.'.$key;
2✔
47
                if (is_array($value)) {
2✔
48
                    $extracted = $extract($value, $fullKey);
2✔
49
                    foreach ($extracted as $k) {
2✔
50
                        $keys[] = $k;
2✔
51
                    }
52
                } else {
53
                    $keys[] = $fullKey;
2✔
54
                }
55
            }
56

57
            return $keys;
2✔
58
        };
2✔
59

60
        return $extract($this->json);
2✔
61
    }
62

63
    public function getContentByKey(string $key, string $attribute = 'source'): ?string
3✔
64
    {
65
        // Note: the $attribute parameter is required by ParserInterface
66
        // but is not used for JSON, since JSON has no source/target concept.
67
        $parts = explode('.', $key);
3✔
68
        $value = $this->json;
3✔
69

70
        foreach ($parts as $part) {
3✔
71
            if (is_array($value) && array_key_exists($part, $value)) {
3✔
72
                $value = $value[$part];
3✔
73
            } else {
74
                return null;
2✔
75
            }
76
        }
77

78
        return is_string($value) ? $value : null;
3✔
79
    }
80

81
    /**
82
     * @return array<int, string>
83
     */
84
    public static function getSupportedFileExtensions(): array
22✔
85
    {
86
        return ['json'];
22✔
87
    }
88

89
    public function getLanguage(): string
2✔
90
    {
91
        if (preg_match(
2✔
92
            '/\.([a-z]{2})(?:[-_][A-Z]{2})?\./',
2✔
93
            $this->getFileName(),
2✔
94
            $matches
2✔
95
        )) {
2✔
96
            return $matches[1];
2✔
97
        }
98

99
        return '';
2✔
100
    }
101
}
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