feat: add KeyCountValidator to warn about excessive translation keys#64
feat: add KeyCountValidator to warn about excessive translation keys#64konradmichalik merged 1 commit intomainfrom
Conversation
WalkthroughA new translation file validator, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ValidatorRegistry
participant KeyCountValidator
participant Parser
participant Logger
User->>ValidatorRegistry: getAvailableValidators()
ValidatorRegistry-->>User: [ ..., KeyCountValidator, ... ]
User->>KeyCountValidator: processFile(Parser)
KeyCountValidator->>Parser: extractTranslationKeys()
Parser-->>KeyCountValidator: [key1, key2, ..., keyN]
KeyCountValidator->>KeyCountValidator: Count keys
alt Key count > threshold
KeyCountValidator->>Logger: log warning
KeyCountValidator-->>User: [Warning: Key count exceeded]
else Key count <= threshold
KeyCountValidator-->>User: []
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)
338-345: Specify language for fenced code block.The console output example should specify the language for better syntax highlighting and accessibility.
-``` +```console
📜 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/KeyCountValidator.php(1 hunks)src/Validator/ValidatorRegistry.php(1 hunks)tests/Fixtures/examples/README.md(1 hunks)tests/Fixtures/examples/key-count/large-file.en.yaml(1 hunks)tests/Fixtures/examples/key-count/translation-validator.yaml(1 hunks)tests/src/Validator/KeyCountValidatorTest.php(1 hunks)tests/src/Validator/ValidatorRegistryTest.php(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Validator/KeyCountValidator.php (11)
src/Config/TranslationValidatorConfig.php (4)
TranslationValidatorConfig(26-341)hasValidatorSettings(316-319)getValidatorSettings(298-301)TranslationValidatorConfig(26-285)src/Parser/JsonParser.php (1)
JsonParser(29-123)src/Parser/PhpParser.php (1)
PhpParser(29-144)src/Parser/XliffParser.php (1)
XliffParser(29-126)src/Parser/YamlParser.php (1)
YamlParser(30-114)src/Validator/AbstractValidator.php (2)
AbstractValidator(34-238)AbstractValidator(34-223)src/Validator/DuplicateKeysValidator.php (2)
DuplicateKeysValidator(33-87)processFile(38-59)src/Validator/MismatchValidator.php (2)
MismatchValidator(37-256)processFile(42-57)src/Validator/DuplicateValuesValidator.php (2)
DuplicateValuesValidator(33-120)processFile(41-60)src/Validator/EmptyValuesValidator.php (1)
EmptyValuesValidator(33-95)src/Validator/ValidatorInterface.php (1)
ValidatorInterface(31-90)
🪛 markdownlint-cli2 (0.17.2)
docs/validators.md
338-338: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🔇 Additional comments (23)
tests/Fixtures/examples/README.md (1)
14-14: LGTM! Documentation update is consistent.The addition of the
key-count/directory entry follows the established format and naming conventions used for other validator examples.README.md (1)
71-71: LGTM! Validator documentation entry is well-formatted.The new KeyCountValidator entry follows the established table format and provides an accurate, concise description of its functionality.
tests/Fixtures/examples/key-count/translation-validator.yaml (1)
1-10: LGTM! Configuration example is well-structured.The YAML configuration properly demonstrates the KeyCountValidator settings with:
- Clear comments explaining the demonstration purpose
- Appropriate lower threshold (100) for testing
- Correct YAML syntax and structure
src/Validator/ValidatorRegistry.php (1)
41-41: Validator Registration VerifiedKeyCountValidator exists (src/Validator/KeyCountValidator.php) and correctly implements ValidatorInterface. Its inclusion in ValidatorRegistry follows the established pattern and requires no further changes.
tests/src/Validator/ValidatorRegistryTest.php (3)
30-30: LGTM! Import statement follows the established pattern.The KeyCountValidator import is consistent with other validator imports in the test file.
49-49: LGTM! Test assertion properly covers the new validator.The assertion for KeyCountValidator follows the same pattern as other validator assertions, ensuring comprehensive test coverage.
53-53: LGTM! Validator count updated correctly.The count assertion is properly updated from 9 to 10 validators to reflect the addition of KeyCountValidator.
tests/Fixtures/examples/key-count/large-file.en.yaml (3)
1-3: Well-documented test fixture with clear purpose.The comments clearly explain this file's role as a test fixture for the KeyCountValidator, making it easy for developers to understand its purpose.
4-334: Comprehensive and realistic translation structure.The nested structure covers a wide range of realistic UI translation scenarios (user profiles, navigation, forms, buttons, messages, tables, etc.), making this an excellent test fixture that represents real-world usage patterns.
335-387: Effective threshold testing mechanism.The
additional_keyssection with numbered keys (key_301 through key_350) provides a clean way to ensure the file exceeds the 300-key threshold for testing purposes.docs/validators.md (2)
10-10: Proper integration into table of contents.The KeyCountValidator is correctly positioned alphabetically in the table of contents between HtmlTagValidator and KeyNamingConventionValidator.
296-377: Comprehensive validator documentation following established patterns.The documentation thoroughly covers the validator's purpose, configuration options, example usage, and includes helpful tips. The structure matches other validator documentation sections perfectly.
src/Validator/KeyCountValidator.php (5)
34-41: Clean class structure following established patterns.The class properly extends AbstractValidator and implements ValidatorInterface, maintaining consistency with other validators in the codebase.
43-53: Robust configuration handling with proper validation.The configuration method includes proper type checking and fallback to defaults, ensuring the validator works reliably even with invalid configuration values.
58-85: Well-implemented processFile method with proper error handling.The method correctly handles both valid and invalid files, logs appropriate errors, and returns structured data that includes both human-readable messages and metadata for programmatic use.
87-90: Appropriate result type for non-critical issues.Using ResultType::WARNING is correct for this validator since having many keys is a maintainability concern rather than a critical error.
95-98: Comprehensive parser support.Supporting all four major parser types (Xliff, Yaml, Json, Php) ensures the validator works across different translation file formats commonly used in projects.
tests/src/Validator/KeyCountValidatorTest.php (6)
35-56: Excellent test coverage for the core functionality.The test properly verifies that files exceeding the threshold trigger warnings with correct metadata (key count, threshold, and descriptive message).
58-90: Comprehensive boundary condition testing.Testing both files within the threshold and files exactly at the threshold ensures the validator behaves correctly at edge cases.
92-122: Proper error handling verification.The tests verify that invalid files are handled gracefully with appropriate logging and empty result arrays, ensuring the validator doesn't crash on malformed input.
124-230: Thorough configuration testing covering all scenarios.The test suite covers custom thresholds, invalid configuration values, missing configuration, and empty settings, ensuring robust configuration handling in all circumstances.
232-260: Complete interface compliance verification.Testing the validator interface methods (supportsParser, getShortName, resultTypeOnValidationFailure) ensures the validator properly integrates with the framework.
262-308: Comprehensive message formatting tests.Testing both basic and prefixed message formatting ensures the validator outputs are properly styled and contain the expected information for CLI display.
A validator to increase clarity in translation files by splitting them into multiple files when individual files are too large.
Summary by CodeRabbit
New Features
Documentation
Tests