feat: add KeyDepthValidator to warn about excessive nesting depth in translation keys#65
Conversation
WalkthroughA new translation key nesting depth validator, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Validator as KeyDepthValidator
participant Parser
participant Logger
User->>Validator: processFile(Parser)
Validator->>Parser: getKeys()
Parser-->>Validator: List of translation keys
Validator->>Validator: Calculate depth for each key
alt Any key exceeds threshold
Validator->>Logger: Log warning (if applicable)
Validator-->>User: Return warning with violating keys
else All keys within threshold
Validator-->>User: Return empty result (no violations)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~18 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
docs/validators.md (1)
382-463: Comprehensive validator documentation with minor formatting issues.The documentation provides excellent coverage of the KeyDepthValidator including:
- Clear purpose and configuration options
- Realistic examples and console output
- Configuration instructions and best practices
However, address these minor formatting issues:
-> [!NOTE] -> New in version **1.2.0** (https://github.com/move-elevator/composer-translation-validator/pull/65) +> [!NOTE] +> New in version **1.2.0** ([PR #65](https://github.com/move-elevator/composer-translation-validator/pull/65)) -``` +```bashConsider using "challenging" instead of "hard" for more professional tone:
-Warns when translation keys have excessive nesting depth, helping identify overly complex key structures that may be hard to maintain. +Warns when translation keys have excessive nesting depth, helping identify overly complex key structures that may be challenging to maintain.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
README.md(1 hunks)docs/validators.md(2 hunks)src/Validator/KeyDepthValidator.php(1 hunks)src/Validator/ValidatorRegistry.php(1 hunks)tests/Fixtures/examples/README.md(1 hunks)tests/Fixtures/examples/key-depth/deeply-nested.en.yaml(1 hunks)tests/Fixtures/examples/key-depth/translation-validator.yaml(1 hunks)tests/src/Validator/KeyDepthValidatorTest.php(1 hunks)tests/src/Validator/ValidatorRegistryTest.php(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
tests/Fixtures/examples/key-depth/translation-validator.yaml (2)
tests/src/Config/TranslationValidatorConfigTest.php (1)
TranslationValidatorConfigTest(30-302)src/Config/TranslationValidatorConfig.php (1)
TranslationValidatorConfig(26-285)
src/Validator/ValidatorRegistry.php (1)
src/Validator/KeyDepthValidator.php (1)
KeyDepthValidator(34-140)
🪛 LanguageTool
docs/validators.md
[style] ~387-~387: To elevate your writing, try using a synonym here.
Context: ...erly complex key structures that may be hard to maintain. Result: 
🪛 markdownlint-cli2 (0.17.2)
docs/validators.md
385-385: Bare URL used
(MD034, no-bare-urls)
425-425: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🔇 Additional comments (19)
tests/Fixtures/examples/README.md (1)
15-15: LGTM!The addition of the
key-depth/directory entry correctly documents the new KeyDepthValidator example fixtures and maintains alphabetical ordering.README.md (1)
72-72: LGTM!The KeyDepthValidator entry is properly documented with a clear description and follows the existing table format. The documentation link aligns with the validator documentation pattern.
tests/Fixtures/examples/key-depth/translation-validator.yaml (1)
1-9: LGTM!The configuration file is well-structured for demonstration purposes. The lowered threshold of 5 (compared to the default of 8) makes it easier to trigger the validator with example files, and the comments clearly explain the intent.
src/Validator/ValidatorRegistry.php (1)
42-42: LGTM!The KeyDepthValidator is correctly added to the available validators registry, enabling its discovery and use by the validation framework. The placement maintains logical grouping with other key-related validators.
tests/src/Validator/ValidatorRegistryTest.php (3)
31-31: LGTM!The import for KeyDepthValidator is correctly added to support the test assertions.
51-51: LGTM!The assertion correctly verifies that KeyDepthValidator is included in the available validators list.
55-55: LGTM!The validator count is correctly updated from 10 to 11 to reflect the addition of KeyDepthValidator.
tests/Fixtures/examples/key-depth/deeply-nested.en.yaml (1)
1-69: Excellent test fixture design!This YAML file provides comprehensive test cases for the KeyDepthValidator with:
- Clear examples of acceptable nesting (≤8 levels)
- Realistic examples that exceed the threshold (9-11 levels)
- Various separator patterns (dots, underscores, hyphens)
- Helpful comments indicating depth levels for validation
The examples are realistic and will effectively test the validator's behavior across different scenarios.
docs/validators.md (1)
11-11: Good documentation structure maintained.The new KeyDepthValidator entry is properly integrated into the table of contents and follows the established documentation format.
src/Validator/KeyDepthValidator.php (4)
1-53: Well-structured validator class with good configuration handling.The class follows established patterns and properly handles:
- Constructor injection of logger
- Configuration validation with fallback to defaults
- Type safety with strict typing
58-96: Solid file processing logic with proper error handling.The
processFilemethod correctly:
- Handles invalid files with appropriate logging
- Processes keys and calculates depths
- Returns structured violation data
- Includes helpful pluralization in messages
118-140: Clever depth calculation algorithm with good separator support.The
calculateKeyDepthmethod effectively:
- Handles edge cases (empty keys)
- Supports multiple common separators
- Uses max depth for mixed separator scenarios
- Returns intuitive depth values (1-based counting)
The logic correctly identifies the maximum nesting depth when multiple separator types are used in the same key.
106-109: Comprehensive parser support.Good that the validator supports all major translation file formats (XLIFF, YAML, JSON, PHP), making it widely applicable.
tests/src/Validator/KeyDepthValidatorTest.php (6)
36-70: Excellent core functionality testing.This test effectively validates the main use case with:
- Realistic deep nesting examples (9-10 levels)
- Proper assertion of violation structure
- Verification of threshold enforcement
- Clear test data with meaningful key names
97-117: Thorough separator testing with good edge case coverage.The test validates different separator types and mixed separators, ensuring the algorithm correctly handles real-world key patterns.
119-134: Proper error handling verification.Good testing of the error path with appropriate logger mock expectations, ensuring invalid files are handled gracefully.
151-179: Comprehensive configuration testing.Excellent validation of custom threshold configuration with proper boundary testing (exactly at threshold vs exceeding it).
354-385: Thorough private method testing with excellent test cases.The reflection-based testing of
calculateKeyDepthprovides comprehensive coverage of:
- Edge cases (empty strings, single characters)
- Various separator patterns
- Mixed separator scenarios
- Boundary conditions
The test cases are well-designed and cover all realistic scenarios.
273-301: Complete validator interface testing.Good coverage of all validator interface methods including parser support, result types, and naming.
Summary by CodeRabbit
New Features
Documentation
Tests