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

move-elevator / composer-translation-validator / 16214450780

11 Jul 2025 07:31AM UTC coverage: 95.347% (+1.5%) from 93.814%
16214450780

Pull #26

github

jackd248
Merge remote-tracking branch 'origin/main' into code-improvement
Pull Request #26: feat: implement configuration factory and file reader for translation validator

216 of 221 new or added lines in 7 files covered. (97.74%)

1209 of 1268 relevant lines covered (95.35%)

7.97 hits per line

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

90.7
/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
        $realPath = realpath($configPath);
3✔
68
        if (false === $realPath) {
3✔
NEW
69
            throw new \RuntimeException("Invalid configuration file path: {$configPath}");
×
70
        }
71
        $config = require $realPath;
3✔
72

73
        if (!$config instanceof TranslationValidatorConfig) {
3✔
74
            throw new \RuntimeException('PHP configuration file must return an instance of TranslationValidatorConfig');
1✔
75
        }
76

77
        return $config;
2✔
78
    }
79

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

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

97
        return $data;
2✔
98
    }
99

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

110
        return $data;
1✔
111
    }
112
}
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