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

move-elevator / composer-translation-validator / 15991591801

01 Jul 2025 06:26AM UTC coverage: 85.714% (-3.9%) from 89.647%
15991591801

Pull #11

github

web-flow
Merge 770fcc207 into da6f9b819
Pull Request #11: feat: add json output format

77 of 102 new or added lines in 6 files covered. (75.49%)

3 existing lines in 2 files now uncovered.

402 of 469 relevant lines covered (85.71%)

2.26 hits per line

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

92.31
/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 valid XML
15
     */
16
    public function __construct(protected string $filePath)
12✔
17
    {
18
        parent::__construct($filePath);
12✔
19

20
        $xmlContent = file_get_contents($filePath);
9✔
21
        $this->xml = simplexml_load_string($xmlContent);
9✔
22

23
        if (false === $this->xml) {
9✔
24
            throw new \InvalidArgumentException("Failed to parse XML content from file: {$filePath}");
×
25
        }
26
    }
27

28
    /**
29
     * @return array<int, string>|null
30
     */
31
    public function extractKeys(): ?array
1✔
32
    {
33
        $keys = [];
1✔
34
        foreach ($this->xml->file->body->{'trans-unit'} as $unit) {
1✔
35
            $keys[] = (string) $unit['id'];
1✔
36
        }
37

38
        return $keys;
1✔
39
    }
40

41
    public function getContentByKey(string $key, string $attribute = 'source'): ?string
2✔
42
    {
43
        foreach ($this->xml->file->body->{'trans-unit'} as $unit) {
2✔
44
            if ((string) $unit['id'] === $key) {
2✔
45
                if ('' !== (string) $unit->{$attribute}) {
2✔
46
                    return (string) $unit->{$attribute};
2✔
47
                }
48

49
                if ('source' === $attribute) {
1✔
NEW
50
                    return $this->getContentByKey($key, 'target');
×
51
                }
52
            }
53
        }
54

55
        return null;
2✔
56
    }
57

58
    /**
59
     * @return array<int, string>
60
     */
61
    public static function getSupportedFileExtensions(): array
14✔
62
    {
63
        return ['xliff', 'xlf'];
14✔
64
    }
65

66
    public function getLanguage(): string
3✔
67
    {
68
        if (preg_match('/^([a-z]{2})\./i', $this->getFileName(), $matches)) {
3✔
69
            $language = $matches[1];
1✔
70
        } else {
71
            $language = (string) ($this->xml->file['source-language'] ?? '');
2✔
72
        }
73

74
        return $language;
3✔
75
    }
76
}
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