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

move-elevator / composer-translation-validator / 16088856561

05 Jul 2025 02:03PM UTC coverage: 93.207% (-0.7%) from 93.93%
16088856561

push

github

web-flow
Merge pull request #17 from move-elevator/symfony-read-file

fix: replace XmlUtils::loadFile with file_get_contents to avoid autoloading issues

6 of 8 new or added lines in 1 file covered. (75.0%)

3 existing lines in 1 file now uncovered.

590 of 633 relevant lines covered (93.21%)

4.4 hits per line

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

90.0
/src/Validator/SchemaValidator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MoveElevator\ComposerTranslationValidator\Validator;
6

7
use MoveElevator\ComposerTranslationValidator\Parser\ParserInterface;
8
use MoveElevator\ComposerTranslationValidator\Parser\XliffParser;
9
use Symfony\Component\Config\Util\XmlUtils;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Helper\TableSeparator;
12
use Symfony\Component\Console\Helper\TableStyle;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Translation\Util\XliffUtils;
16

17
class SchemaValidator extends AbstractValidator implements ValidatorInterface
18
{
19
    public function processFile(ParserInterface $file): array
3✔
20
    {
21
        try {
22
            /*
23
             * With XmlUtils::loadFile() we always get a strange symfony error related to global composer autoloading issue.
24
             *      Call to undefined method Symfony\Component\Filesystem\Filesystem::readFile()
25
             */
26
            if (!file_exists($file->getFilePath())) {
3✔
27
                $this->logger?->error('File does not exist: '.$file->getFileName());
1✔
28

29
                return [];
1✔
30
            }
31

32
            $fileContent = file_get_contents($file->getFilePath());
2✔
33
            if (false === $fileContent) {
2✔
NEW
34
                $this->logger?->error('Failed to read file: '.$file->getFileName());
×
35

NEW
36
                return [];
×
37
            }
38
            $dom = XmlUtils::parse($fileContent);
2✔
39
            $errors = XliffUtils::validateSchema($dom);
2✔
UNCOV
40
        } catch (\Exception $e) {
×
UNCOV
41
            $this->logger?->error('Failed to validate XML schema: '.$e->getMessage());
×
42

UNCOV
43
            return [];
×
44
        }
45

46
        if (!empty($errors)) {
2✔
47
            return $errors;
1✔
48
        }
49

50
        return [];
1✔
51
    }
52

53
    /**
54
     * @param array<string, array<int, array{
55
     *     file: string,
56
     *     issues: array<int, array{
57
     *         level: string,
58
     *         code: int,
59
     *         message: string,
60
     *         file: string,
61
     *         line: int,
62
     *         column: int
63
     *     }>,
64
     *     parser: string,
65
     *     type: string
66
     * }>> $issueSets
67
     */
68
    public function renderIssueSets(InputInterface $input, OutputInterface $output, array $issueSets): void
1✔
69
    {
70
        $currentFile = null;
1✔
71
        $table = new Table($output);
1✔
72
        $table
1✔
73
            ->setHeaders(['File', 'Level', 'Code', 'Message', 'Line'])
1✔
74
            ->setStyle(
1✔
75
                (new TableStyle())
1✔
76
                    ->setCellHeaderFormat('%s')
1✔
77
            );
1✔
78

79
        foreach ($issueSets as $issues) {
1✔
80
            foreach ($issues as $errors) {
1✔
81
                if ($currentFile !== $errors['file'] && null !== $currentFile) {
1✔
82
                    $table->addRow(new TableSeparator());
1✔
83
                }
84
                $currentFile = $errors['file'];
1✔
85

86
                foreach ($errors['issues'] as $error) {
1✔
87
                    $message = preg_replace(
1✔
88
                        "/^Element ('(?:\{[^}]+\})?[^']+'):?\s*/",
1✔
89
                        '',
1✔
90
                        (string) $error['message']
1✔
91
                    );
1✔
92

93
                    $table->addRow([
1✔
94
                        '<fg=red>'.$errors['file'].'</>',
1✔
95
                        LIBXML_ERR_WARNING === (int) $error['level'] ? 'WARNING' : 'ERROR',
1✔
96
                        $error['code'],
1✔
97
                        trim((string) $message),
1✔
98
                        $error['line'],
1✔
99
                    ]);
1✔
100
                    $errors['file'] = ''; // Reset file for subsequent rows
1✔
101
                }
102
            }
103
        }
104
        $table->render();
1✔
105
    }
106

107
    public function explain(): string
1✔
108
    {
109
        return 'Validates the XML schema of translation files against the XLIFF standard. '.
1✔
110
            'This ensures that the files are well-formed and adhere to the expected structure.';
1✔
111
    }
112

113
    /**
114
     * @return class-string<ParserInterface>[]
115
     */
116
    public function supportsParser(): array
1✔
117
    {
118
        return [XliffParser::class];
1✔
119
    }
120
}
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