• 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

90.63
/src/Config/ConfigReader.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 JsonException;
17

18
use function dirname;
19
use function is_array;
20

21
/**
22
 * ConfigReader.
23
 *
24
 * @author Konrad Michalik <km@move-elevator.de>
25
 * @license GPL-3.0-or-later
26
 */
27
class ConfigReader
28
{
29
    private const AUTO_DETECTION_FILES = [
30
        'translation-validator.php',
31
        'translation-validator.json',
32
        'translation-validator.yaml',
33
        'translation-validator.yml',
34
    ];
35

36
    public function __construct(
31✔
37
        private readonly ConfigFileReader $fileReader = new ConfigFileReader(),
38
    ) {}
31✔
39

40
    /**
41
     * @throws JsonException
42
     */
43
    public function read(string $configPath): TranslationValidatorConfig
26✔
44
    {
45
        return $this->fileReader->readAsConfig($configPath);
26✔
46
    }
47

48
    /**
49
     * @throws JsonException
50
     */
51
    public function autoDetect(?string $workingDirectory = null): ?TranslationValidatorConfig
5✔
52
    {
53
        $workingDirectory ??= getcwd();
5✔
54

55
        if (!$workingDirectory) {
5✔
UNCOV
56
            return null;
×
57
        }
58

59
        foreach (self::AUTO_DETECTION_FILES as $filename) {
5✔
60
            $configPath = $workingDirectory.\DIRECTORY_SEPARATOR.$filename;
5✔
61
            if (file_exists($configPath)) {
5✔
62
                return $this->read($configPath);
3✔
63
            }
64
        }
65

66
        return null;
2✔
67
    }
68

69
    /**
70
     * @throws JsonException
71
     */
72
    public function readFromComposerJson(string $composerJsonPath): ?TranslationValidatorConfig
6✔
73
    {
74
        if (!file_exists($composerJsonPath)) {
6✔
75
            return null;
1✔
76
        }
77

78
        $content = file_get_contents($composerJsonPath);
5✔
79
        if (false === $content) {
5✔
UNCOV
80
            return null;
×
81
        }
82

83
        $composerData = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
5✔
84
        if (!is_array($composerData)) {
4✔
UNCOV
85
            return null;
×
86
        }
87

88
        $configFilePath = $composerData['extra']['translation-validator']['config-file'] ?? null;
4✔
89
        if (!$configFilePath) {
4✔
90
            return null;
1✔
91
        }
92

93
        $composerDir = dirname($composerJsonPath);
3✔
94
        $absoluteConfigPath = $this->resolveConfigPath($configFilePath, $composerDir);
3✔
95

96
        return $this->read($absoluteConfigPath);
3✔
97
    }
98

99
    private function resolveConfigPath(string $configPath, string $basePath): string
3✔
100
    {
101
        if (str_starts_with($configPath, '/') || preg_match('/^[a-zA-Z]:[\\/]/', $configPath)) {
3✔
102
            return $configPath;
1✔
103
        }
104

105
        return $basePath.\DIRECTORY_SEPARATOR.$configPath;
2✔
106
    }
107
}
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