feat: add XLIFF 2.x support to XliffParser#107
Conversation
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughAdded XLIFF 2.0/2.2 support by detecting version in the parser, branching parsing behavior for v1 vs v2 XML structures, refactoring key/content extraction into new helpers, and extending language/target detection. Tests for XLIFF 2.0 and 2.2 were added. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.1.44)Composer install failed: this project depends on private packages that require authentication (e.g. GitLab/GitHub, Laravel Nova, etc.). Instead, run PHPStan in a CI/CD pipeline where you can use custom packages — our pipeline remediation tool can use the PHPStan output from your CI/CD pipeline. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
tests/src/Parser/XliffParserTest.php (1)
458-480: Consider adding edge case test for XLIFF 2.0 with no translation units.The existing tests cover the happy path well. For completeness, consider adding a test for an XLIFF 2.0 file with an empty
<file>element (no<unit>children) to verify thatextractKeys()returns an empty array.Example test case
public function testExtractKeysReturnsEmptyArrayWhenNoUnitsXliff2(): void { $emptyUnitsFile = $this->tempDir.'/empty_units_v2.xlf'; $emptyUnitsContent = <<<'EOT' <?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en"> <file id="messages"> </file> </xliff> EOT; file_put_contents($emptyUnitsFile, $emptyUnitsContent); $parser = new XliffParser($emptyUnitsFile); $this->assertSame([], $parser->extractKeys()); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/src/Parser/XliffParserTest.php` around lines 458 - 480, Add a new unit test method (e.g., testExtractKeysReturnsEmptyArrayWhenNoUnitsXliff2) that creates an XLIFF 2.0 file whose <file> element has no <unit> children, instantiates XliffParser with that file, and asserts that XliffParser::extractKeys() returns an empty array; ensure the test writes the file to the same tempDir pattern used in other tests and uses the parser methods (extractKeys and optionally getLanguage) to validate behavior.src/Parser/XliffParser.php (1)
59-72: Behavioral change:extractKeys()now returns[]instead ofnullwhen units are missing.Returning an empty array instead of
nullwhen translation units are unavailable is a valid approach within the interface contract (array<int, string>|null). However, this is a subtle semantic shift: previouslynullmay have indicated an error condition, while now[]indicates "no keys found."Based on the relevant code snippets, existing validators handle this correctly:
MismatchValidatorandDuplicateValuesValidatoruseif (!$keys)which handles both cases.PlaceholderConsistencyValidatorusesif (null === $keys)which becomes ineffective, but theforeachloop safely handles empty arrays.No immediate fix needed, but consider documenting this behavior in the interface or aligning the guard in
PlaceholderConsistencyValidatorfor consistency.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Parser/XliffParser.php` around lines 59 - 72, extractKeys() now returns an empty array instead of null when getTranslationUnits() yields no units; update the guard in PlaceholderConsistencyValidator (the method that currently does if (null === $keys)) to treat empty arrays the same as null (e.g., change the check to if (!$keys) or if (null === $keys || [] === $keys)) so it aligns with MismatchValidator and DuplicateValuesValidator and preserves the original semantic for "no keys available"; leave extractKeys() as-is.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Parser/XliffParser.php`:
- Around line 130-137: The getTranslationUnits() helper returns a
SimpleXMLElement even when no child nodes exist, so any downstream null checks
(e.g., the checks around retrieval of units in the parser) are ineffective;
update the code that expects null by replacing those null comparisons with an
explicit empty check using count() or casting to (array) to detect zero children
and return null when count($this->getTranslationUnits()) === 0 (or equivalent),
and update getTranslationUnits() callers to treat an empty SimpleXMLElement as
absent; specifically look at getTranslationUnits(), and the places that
previously did `null === $units` (the checks around retrieval of units) and
change them to use a count() === 0 test.
---
Nitpick comments:
In `@src/Parser/XliffParser.php`:
- Around line 59-72: extractKeys() now returns an empty array instead of null
when getTranslationUnits() yields no units; update the guard in
PlaceholderConsistencyValidator (the method that currently does if (null ===
$keys)) to treat empty arrays the same as null (e.g., change the check to if
(!$keys) or if (null === $keys || [] === $keys)) so it aligns with
MismatchValidator and DuplicateValuesValidator and preserves the original
semantic for "no keys available"; leave extractKeys() as-is.
In `@tests/src/Parser/XliffParserTest.php`:
- Around line 458-480: Add a new unit test method (e.g.,
testExtractKeysReturnsEmptyArrayWhenNoUnitsXliff2) that creates an XLIFF 2.0
file whose <file> element has no <unit> children, instantiates XliffParser with
that file, and asserts that XliffParser::extractKeys() returns an empty array;
ensure the test writes the file to the same tempDir pattern used in other tests
and uses the parser methods (extractKeys and optionally getLanguage) to validate
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 33c92138-2470-490d-a388-604ea756f3bc
📒 Files selected for processing (2)
src/Parser/XliffParser.phptests/src/Parser/XliffParserTest.php
Closes #105
Summary
XliffParserversion_compare()to cover all 2.x versionsChanges
src/Parser/XliffParser.php- Detect XLIFF version, delegate to version-specific XML paths for units, content, and language attributestests/src/Parser/XliffParserTest.php- Add 7 test cases for XLIFF 2.0 and 2.2 (key extraction, content retrieval, language detection, fallback behavior)Summary by CodeRabbit
New Features
Bug Fixes
Tests