• 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

87.5
/src/Config/SchemaValidator.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
use RuntimeException;
18

19
use function sprintf;
20

21
/**
22
 * SchemaValidator.
23
 *
24
 * @author Konrad Michalik <km@move-elevator.de>
25
 * @license GPL-3.0-or-later
26
 */
27
class SchemaValidator
28
{
29
    private const SCHEMA_PATH = __DIR__.'/../../schema/translation-validator.schema.json';
30

31
    /**
32
     * @param array<string, mixed> $data
33
     *
34
     * @throws JsonException
35
     */
36
    public function validate(array $data): void
38✔
37
    {
38
        if (!class_exists(\JsonSchema\Validator::class)) {
38✔
UNCOV
39
            return;
×
40
        }
41

42
        $schema = $this->loadSchema();
38✔
43
        $validator = new \JsonSchema\Validator();
35✔
44

45
        $dataObject = json_decode(json_encode($data, \JSON_THROW_ON_ERROR), false, 512, \JSON_THROW_ON_ERROR);
35✔
46
        $validator->validate($dataObject, $schema);
35✔
47

48
        if (!$validator->isValid()) {
35✔
49
            $errors = [];
6✔
50
            foreach ($validator->getErrors() as $error) {
6✔
51
                $errors[] = sprintf('[%s] %s', $error['property'], $error['message']);
6✔
52
            }
53

54
            throw new RuntimeException('Configuration validation failed:'.\PHP_EOL.implode(\PHP_EOL, $errors));
6✔
55
        }
56
    }
57

58
    public function isAvailable(): bool
30✔
59
    {
60
        return class_exists(\JsonSchema\Validator::class);
30✔
61
    }
62

63
    /**
64
     * @throws JsonException
65
     */
66
    private function loadSchema(): object
38✔
67
    {
68
        if (!file_exists(self::SCHEMA_PATH) || !is_readable(self::SCHEMA_PATH) || !is_file(self::SCHEMA_PATH)) {
38✔
69
            throw new RuntimeException('JSON Schema file not found: '.self::SCHEMA_PATH);
2✔
70
        }
71

72
        $schemaContent = file_get_contents(self::SCHEMA_PATH);
36✔
73
        if (false === $schemaContent) {
36✔
UNCOV
74
            throw new RuntimeException('Failed to read JSON Schema file: '.self::SCHEMA_PATH);
×
75
        }
76

77
        $schema = json_decode($schemaContent, false, 512, \JSON_THROW_ON_ERROR);
36✔
78
        if (null === $schema) {
35✔
UNCOV
79
            throw new RuntimeException('Invalid JSON Schema file: '.self::SCHEMA_PATH);
×
80
        }
81

82
        return $schema;
35✔
83
    }
84
}
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