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

move-elevator / composer-translation-validator / 16273482048

14 Jul 2025 05:22PM UTC coverage: 95.945% (-0.2%) from 96.161%
16273482048

Pull #35

github

jackd248
refactor: enhance file handling and error reporting in validators and parsers
Pull Request #35: build: update phpstan level to 7

33 of 41 new or added lines in 10 files covered. (80.49%)

1680 of 1751 relevant lines covered (95.95%)

7.57 hits per line

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

97.06
/src/Parser/XliffParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MoveElevator\ComposerTranslationValidator\Parser;
6

7
class XliffParser extends AbstractParser implements ParserInterface
8
{
9
    private readonly \SimpleXMLElement|bool $xml;
10

11
    /**
12
     * @param string $filePath Path to the XLIFF file
13
     *
14
     * @throws \InvalidArgumentException If file cannot be parsed as
15
     *                                   valid XML
16
     */
17
    public function __construct(protected string $filePath)
14✔
18
    {
19
        parent::__construct($filePath);
14✔
20

21
        $xmlContent = file_get_contents($filePath);
11✔
22
        if (false === $xmlContent) {
11✔
NEW
23
            throw new \InvalidArgumentException("Failed to read file: {$filePath}");
×
24
        }
25

26
        libxml_use_internal_errors(true);
11✔
27
        $this->xml = simplexml_load_string($xmlContent);
11✔
28

29
        if (false === $this->xml) {
11✔
30
            throw new \InvalidArgumentException("Failed to parse XML content from file: {$filePath}");
1✔
31
        }
32
        libxml_clear_errors();
10✔
33
    }
34

35
    /**
36
     * @return array<int, string>|null
37
     */
38
    public function extractKeys(): ?array
1✔
39
    {
40
        $keys = [];
1✔
41
        foreach ($this->xml->file->body->{'trans-unit'} as $unit) {
1✔
42
            $keys[] = (string) $unit['id'];
1✔
43
        }
44

45
        return $keys;
1✔
46
    }
47

48
    public function getContentByKey(string $key, string $attribute = 'source'): ?string
3✔
49
    {
50
        foreach ($this->xml->file->body->{'trans-unit'} as $unit) {
3✔
51
            if ((string) $unit['id'] === $key) {
3✔
52
                if ('' !== (string) $unit->{$attribute}) {
3✔
53
                    return (string) $unit->{$attribute};
3✔
54
                }
55

56
                if ('source' === $attribute) {
2✔
57
                    return $this->getContentByKey($key, 'target');
1✔
58
                }
59
            }
60
        }
61

62
        return null;
2✔
63
    }
64

65
    /**
66
     * @return array<int, string>
67
     */
68
    public static function getSupportedFileExtensions(): array
23✔
69
    {
70
        return ['xliff', 'xlf'];
23✔
71
    }
72

73
    public function getLanguage(): string
3✔
74
    {
75
        if (preg_match(
3✔
76
            '/^([a-z]{2})\./i',
3✔
77
            $this->getFileName(),
3✔
78
            $matches
3✔
79
        )) {
3✔
80
            $language = $matches[1];
1✔
81
        } else {
82
            $language = (string) ($this->xml->file['source-language'] ?? '');
2✔
83
        }
84

85
        return $language;
3✔
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc