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

move-elevator / composer-translation-validator / 16517534501

25 Jul 2025 08:29AM UTC coverage: 96.5% (-0.002%) from 96.502%
16517534501

Pull #46

github

jackd248
fix: add newline at end of unsupported.txt
Pull Request #46: feat: add KeyNamingConventionValidator with configurable naming conventions

139 of 144 new or added lines in 6 files covered. (96.53%)

22 existing lines in 6 files now uncovered.

1875 of 1943 relevant lines covered (96.5%)

8.95 hits per line

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

96.55
/src/Result/ValidationRun.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer plugin "composer-translation-validator".
7
 *
8
 * Copyright (C) 2025 Konrad Michalik <km@move-elevator.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace MoveElevator\ComposerTranslationValidator\Result;
25

26
use MoveElevator\ComposerTranslationValidator\Config\TranslationValidatorConfig;
27
use MoveElevator\ComposerTranslationValidator\FileDetector\FileSet;
28
use MoveElevator\ComposerTranslationValidator\Parser\ParserCache;
29
use MoveElevator\ComposerTranslationValidator\Validator\ResultType;
30
use MoveElevator\ComposerTranslationValidator\Validator\ValidatorInterface;
31
use Psr\Log\LoggerInterface;
32
use Throwable;
33

34
class ValidationRun
35
{
36
    public function __construct(
11✔
37
        private readonly LoggerInterface $logger,
38
    ) {}
11✔
39

40
    /**
41
     * @param array<FileSet>                          $fileSets
42
     * @param array<class-string<ValidatorInterface>> $validatorClasses
43
     */
44
    public function executeFor(array $fileSets, array $validatorClasses, ?TranslationValidatorConfig $config = null): ValidationResult
11✔
45
    {
46
        $startTime = microtime(true);
11✔
47
        $validatorInstances = [];
11✔
48
        $validatorFileSetPairs = [];
11✔
49
        $overallResult = ResultType::SUCCESS;
11✔
50
        $filesChecked = 0;
11✔
51

52
        foreach ($fileSets as $fileSet) {
11✔
53
            $filesChecked += count($fileSet->getFiles());
8✔
54
            foreach ($validatorClasses as $validatorClass) {
8✔
55
                $validatorInstance = new $validatorClass($this->logger);
7✔
56

57
                // Pass config to validator if it supports it
58
                if (null !== $config && method_exists($validatorInstance, 'setConfig')) {
7✔
NEW
59
                    $validatorInstance->setConfig($config);
×
60
                }
61

62
                /** @var class-string<\MoveElevator\ComposerTranslationValidator\Parser\ParserInterface> $parserClass */
63
                $parserClass = $fileSet->getParser();
7✔
64
                $result = $validatorInstance->validate($fileSet->getFiles(), $parserClass);
7✔
65
                if (!empty($result)) {
7✔
66
                    $overallResult = $overallResult->max($validatorInstance->resultTypeOnValidationFailure());
6✔
67
                    $validatorInstances[] = $validatorInstance;
6✔
68
                    $validatorFileSetPairs[] = [
6✔
69
                        'validator' => $validatorInstance,
6✔
70
                        'fileSet' => $fileSet,
6✔
71
                    ];
6✔
72
                }
73
            }
74
        }
75

76
        $keysChecked = $this->countKeysChecked($fileSets);
11✔
77

78
        $validatorsRun = count($validatorClasses);
11✔
79

80
        // Get cache statistics before clearing cache
81
        $cacheStats = ParserCache::getCacheStats();
11✔
82
        $parsersCached = $cacheStats['cached_parsers'];
11✔
83

84
        $executionTime = microtime(true) - $startTime;
11✔
85
        $statistics = new ValidationStatistics(
11✔
86
            $executionTime,
11✔
87
            $filesChecked,
11✔
88
            $keysChecked,
11✔
89
            $validatorsRun,
11✔
90
            $parsersCached,
11✔
91
        );
11✔
92

93
        $validationResult = new ValidationResult($validatorInstances, $overallResult, $validatorFileSetPairs, $statistics);
11✔
94
        ParserCache::clear();
11✔
95

96
        return $validationResult;
11✔
97
    }
98

99
    /**
100
     * @param array<string, array<string, array<string, array<string>>>> $allFiles
101
     *
102
     * @return array<FileSet>
103
     */
104
    public static function createFileSetsFromArray(array $allFiles): array
4✔
105
    {
106
        $fileSets = [];
4✔
107

108
        foreach ($allFiles as $parser => $paths) {
4✔
109
            foreach ($paths as $path => $translationSets) {
3✔
110
                foreach ($translationSets as $setKey => $files) {
3✔
111
                    $fileSets[] = new FileSet($parser, $path, $setKey, $files);
3✔
112
                }
113
            }
114
        }
115

116
        return $fileSets;
4✔
117
    }
118

119
    /**
120
     * @param array<FileSet> $fileSets
121
     */
122
    private function countKeysChecked(array $fileSets): int
11✔
123
    {
124
        $keysChecked = 0;
11✔
125

126
        foreach ($fileSets as $fileSet) {
11✔
127
            $parserClass = $fileSet->getParser();
8✔
128

129
            foreach ($fileSet->getFiles() as $file) {
8✔
130
                try {
131
                    $parser = ParserCache::get($file, $parserClass);
8✔
132
                    if (false === $parser) {
4✔
UNCOV
133
                        continue;
×
134
                    }
135
                    $keys = $parser->extractKeys();
4✔
136
                    if (is_array($keys)) {
3✔
137
                        $keysChecked += count($keys);
3✔
138
                    }
139
                } catch (Throwable) {
5✔
140
                    // Skip files that can't be parsed
141
                }
142
            }
143
        }
144

145
        return $keysChecked;
11✔
146
    }
147
}
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

© 2026 Coveralls, Inc