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

move-elevator / composer-translation-validator / 18559927341

16 Oct 2025 11:35AM UTC coverage: 95.519%. Remained the same
18559927341

Pull #73

github

jackd248
build: add 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.67
/src/Config/ConfigFileReader.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\Config;
15

16
use InvalidArgumentException;
17
use JsonException;
18
use RuntimeException;
19
use Symfony\Component\Yaml\Yaml;
20

21
use function is_array;
22

23
/**
24
 * ConfigFileReader.
25
 *
26
 * @author Konrad Michalik <km@move-elevator.de>
27
 * @license GPL-3.0-or-later
28
 */
29
class ConfigFileReader
30
{
31
    public function __construct(
15✔
32
        private readonly ConfigFactory $factory = new ConfigFactory(),
33
    ) {}
15✔
34

35
    /**
36
     * @return array<string, mixed>
37
     *
38
     * @throws JsonException
39
     */
40
    public function readFile(string $configPath): array
11✔
41
    {
42
        if (!file_exists($configPath)) {
11✔
43
            throw new InvalidArgumentException("Configuration file not found: {$configPath}");
1✔
44
        }
45

46
        if (!is_readable($configPath)) {
10✔
47
            throw new RuntimeException("Configuration file is not readable: {$configPath}");
1✔
48
        }
49

50
        $extension = pathinfo($configPath, \PATHINFO_EXTENSION);
9✔
51

52
        return match ($extension) {
9✔
53
            'php' => $this->readPhpConfigAsArray($configPath),
1✔
54
            'json' => $this->readJsonConfig($configPath),
4✔
55
            'yaml', 'yml' => $this->readYamlConfig($configPath),
3✔
56
            default => throw new InvalidArgumentException("Unsupported configuration file format: {$extension}"),
5✔
57
        };
9✔
58
    }
59

60
    /**
61
     * @throws JsonException
62
     */
63
    public function readAsConfig(string $configPath): TranslationValidatorConfig
5✔
64
    {
65
        $extension = pathinfo($configPath, \PATHINFO_EXTENSION);
5✔
66

67
        if ('php' === $extension) {
5✔
68
            return $this->readPhpConfig($configPath);
4✔
69
        }
70

71
        $data = $this->readFile($configPath);
1✔
72

73
        return $this->factory->createFromArray($data);
1✔
74
    }
75

76
    /**
77
     * @return array<string, mixed>
78
     */
79
    private function readPhpConfigAsArray(string $configPath): array
1✔
80
    {
81
        return $this->readPhpConfig($configPath)->toArray();
1✔
82
    }
83

84
    private function readPhpConfig(string $configPath): TranslationValidatorConfig
5✔
85
    {
86
        $realPath = realpath($configPath);
5✔
87
        if (false === $realPath) {
5✔
88
            throw new RuntimeException("Invalid configuration file path: {$configPath}");
1✔
89
        }
90
        $config = require $realPath;
4✔
91

92
        if (!$config instanceof TranslationValidatorConfig) {
4✔
93
            throw new RuntimeException('PHP configuration file must return an instance of TranslationValidatorConfig');
1✔
94
        }
95

96
        return $config;
3✔
97
    }
98

99
    /**
100
     * @return array<string, mixed>
101
     *
102
     * @throws JsonException
103
     */
104
    private function readJsonConfig(string $configPath): array
4✔
105
    {
106
        $content = file_get_contents($configPath);
4✔
107
        if (false === $content) {
4✔
UNCOV
108
            throw new RuntimeException("Failed to read configuration file: {$configPath}");
×
109
        }
110

111
        $data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
4✔
112
        if (!is_array($data)) {
3✔
113
            throw new RuntimeException("Invalid JSON configuration file: {$configPath}");
1✔
114
        }
115

116
        return $data;
2✔
117
    }
118

119
    /**
120
     * @return array<string, mixed>
121
     */
122
    private function readYamlConfig(string $configPath): array
3✔
123
    {
124
        $data = Yaml::parseFile($configPath);
3✔
125
        if (!is_array($data)) {
2✔
126
            throw new RuntimeException("Invalid YAML configuration file: {$configPath}");
1✔
127
        }
128

129
        return $data;
1✔
130
    }
131
}
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