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

move-elevator / composer-translation-validator / 16213481303

11 Jul 2025 06:33AM UTC coverage: 95.336% (+1.5%) from 93.814%
16213481303

Pull #26

github

jackd248
feat: implement configuration factory and file reader for translation validator
Pull Request #26: feat: implement configuration factory and file reader for translation validator

213 of 218 new or added lines in 7 files covered. (97.71%)

1206 of 1265 relevant lines covered (95.34%)

7.27 hits per line

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

92.5
/src/Config/ConfigFileReader.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MoveElevator\ComposerTranslationValidator\Config;
6

7
use Symfony\Component\Yaml\Yaml;
8

9
class ConfigFileReader
10
{
11
    public function __construct(
11✔
12
        private readonly ConfigFactory $factory = new ConfigFactory(),
13
    ) {
14
    }
11✔
15

16
    /**
17
     * @return array<string, mixed>
18
     *
19
     * @throws \JsonException
20
     */
21
    public function readFile(string $configPath): array
9✔
22
    {
23
        if (!file_exists($configPath)) {
9✔
24
            throw new \InvalidArgumentException("Configuration file not found: {$configPath}");
1✔
25
        }
26

27
        if (!is_readable($configPath)) {
8✔
28
            throw new \RuntimeException("Configuration file is not readable: {$configPath}");
1✔
29
        }
30

31
        $extension = pathinfo($configPath, PATHINFO_EXTENSION);
7✔
32

33
        return match ($extension) {
7✔
34
            'php' => $this->readPhpConfigAsArray($configPath),
1✔
35
            'json' => $this->readJsonConfig($configPath),
3✔
36
            'yaml', 'yml' => $this->readYamlConfig($configPath),
2✔
37
            default => throw new \InvalidArgumentException("Unsupported configuration file format: {$extension}"),
5✔
38
        };
7✔
39
    }
40

41
    /**
42
     * @throws \JsonException
43
     */
44
    public function readAsConfig(string $configPath): TranslationValidatorConfig
3✔
45
    {
46
        $extension = pathinfo($configPath, PATHINFO_EXTENSION);
3✔
47

48
        if ('php' === $extension) {
3✔
49
            return $this->readPhpConfig($configPath);
2✔
50
        }
51

52
        $data = $this->readFile($configPath);
1✔
53

54
        return $this->factory->createFromArray($data);
1✔
55
    }
56

57
    /**
58
     * @return array<string, mixed>
59
     */
60
    private function readPhpConfigAsArray(string $configPath): array
1✔
61
    {
62
        return $this->readPhpConfig($configPath)->toArray();
1✔
63
    }
64

65
    private function readPhpConfig(string $configPath): TranslationValidatorConfig
3✔
66
    {
67
        $config = require $configPath;
3✔
68

69
        if (!$config instanceof TranslationValidatorConfig) {
3✔
70
            throw new \RuntimeException('PHP configuration file must return an instance of TranslationValidatorConfig');
1✔
71
        }
72

73
        return $config;
2✔
74
    }
75

76
    /**
77
     * @return array<string, mixed>
78
     *
79
     * @throws \JsonException
80
     */
81
    private function readJsonConfig(string $configPath): array
3✔
82
    {
83
        $content = file_get_contents($configPath);
3✔
84
        if (false === $content) {
3✔
NEW
85
            throw new \RuntimeException("Failed to read configuration file: {$configPath}");
×
86
        }
87

88
        $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
3✔
89
        if (!is_array($data)) {
2✔
NEW
90
            throw new \RuntimeException("Invalid JSON configuration file: {$configPath}");
×
91
        }
92

93
        return $data;
2✔
94
    }
95

96
    /**
97
     * @return array<string, mixed>
98
     */
99
    private function readYamlConfig(string $configPath): array
2✔
100
    {
101
        $data = Yaml::parseFile($configPath);
2✔
102
        if (!is_array($data)) {
1✔
NEW
103
            throw new \RuntimeException("Invalid YAML configuration file: {$configPath}");
×
104
        }
105

106
        return $data;
1✔
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc