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

move-elevator / composer-translation-validator / 16252968349

13 Jul 2025 08:07PM UTC coverage: 96.329% (+0.05%) from 96.279%
16252968349

Pull #31

github

jackd248
fix: remove duplicate JsonParser import in ParserRegistryTest
Pull Request #31: 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
        $parts = explode('.', $key);
3✔
66
        $value = $this->json;
3✔
67

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

76
        return is_string($value) ? $value : null;
3✔
77
    }
78

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

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

97
        return '';
2✔
98
    }
99
}
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