feat: introduce ResultType enum for improved validation result handling#13
feat: introduce ResultType enum for improved validation result handling#13konradmichalik merged 3 commits intomainfrom
Conversation
WalkthroughA new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant ValidateTranslationCommand
participant Validator(s)
participant ResultType
User->>CLI: Run validation command
CLI->>ValidateTranslationCommand: execute()
loop For each Validator
ValidateTranslationCommand->>Validator(s): validate()
Validator(s)-->>ValidateTranslationCommand: issues, resultTypeOnValidationFailure()
ValidateTranslationCommand->>ResultType: max(current, validatorResultType)
ResultType-->>ValidateTranslationCommand: updated resultType
end
ValidateTranslationCommand->>CLI: renderCliResult() using aggregated ResultType
CLI-->>User: Output message and exit code based on ResultType
Possibly related PRs
Poem
✨ Finishing Touches
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: 1
🧹 Nitpick comments (3)
src/Validator/ResultType.php (1)
25-36: Consider documenting the strict mode behavior.The method handles
strictmode only forWARNINGcases but not forERROR. This is likely intentional (errors always fail regardless of strict mode), but it would be helpful to add a PHPDoc comment explaining this behavior for clarity.+ /** + * Resolves the result type to a command exit code. + * + * @param bool $dryRun If true, always returns SUCCESS + * @param bool $strict If true, treats WARNINGs as failures + * @return int Command exit code (SUCCESS or FAILURE) + */ public function resolveErrorToCommandExitCode(bool $dryRun, bool $strict): intsrc/Command/ValidateTranslationCommand.php (2)
111-117: Consider performance impact of instantiating validators in the loop.Moving validator instantiation inside the nested loops (line 111) means the same validator class could be instantiated multiple times. This might impact performance for large file sets.
Consider caching validator instances:
$validators = $input->getOption('validator') ? [$input->getOption('validator')] : ValidatorRegistry::getAvailableValidators(); $issues = []; +$validatorInstances = []; // ToDo: Simplify this nested loop structure foreach ($allFiles as $parser => $paths) { foreach ($paths as $path => $translationSets) { foreach ($translationSets as $setKey => $files) { foreach ($validators as $validator) { - $validatorInstance = new $validator($this->logger); + if (!isset($validatorInstances[$validator])) { + $validatorInstances[$validator] = new $validator($this->logger); + } + $validatorInstance = $validatorInstances[$validator]; $result = $validatorInstance->validate($files, $parser);
194-199: Consider simplifying the else clause (optional).The static analysis tool suggests removing the else clause for cleaner code. While the current code is readable, you could consider early return pattern.
if ($this->resultType->notFullySuccessful()) { $this->io->newLine(); $this->io->{$this->dryRun ? 'warning' : 'error'}( $this->dryRun ? 'Language validation failed and completed in dry-run mode.' : 'Language validation failed.' ); -} else { - $message = 'Language validation succeeded.'; - $this->output->isVerbose() - ? $this->io->success($message) - : $this->output->writeln('<fg=green>'.$message.'</>'); + return $this->resultType->resolveErrorToCommandExitCode($this->dryRun, false); } -return $this->resultType->resolveErrorToCommandExitCode($this->dryRun, false); +$message = 'Language validation succeeded.'; +$this->output->isVerbose() + ? $this->io->success($message) + : $this->output->writeln('<fg=green>'.$message.'</>'); + +return Command::SUCCESS;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/Command/ValidateTranslationCommand.php(4 hunks)src/Validator/AbstractValidator.php(1 hunks)src/Validator/ResultType.php(1 hunks)src/Validator/ValidatorInterface.php(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/Validator/ValidatorInterface.php (1)
src/Validator/AbstractValidator.php (1)
resultTypeOnValidationFailure(87-90)
src/Validator/AbstractValidator.php (1)
src/Validator/ValidatorInterface.php (1)
resultTypeOnValidationFailure(38-38)
🪛 PHPMD (2.15.0)
src/Command/ValidateTranslationCommand.php
194-199: The method renderCliResult uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. (Clean Code Rules)
(ElseExpression)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Code Coverage Report
🔇 Additional comments (5)
src/Validator/AbstractValidator.php (1)
87-90: LGTM! Good default implementation.The method correctly implements the interface requirement with a sensible default of
ResultType::ERRORfor validation failures.src/Validator/ValidatorInterface.php (1)
38-38: ResultType import verified; no changes required
ResultTypeis declared in the same namespace (MoveElevator\ComposerTranslationValidator\Validator) and no otherResultTypedefinitions were found. The method signature inValidatorInterface.phpis correct as-is—no explicit import is needed.src/Validator/ResultType.php (1)
9-37: Well-designed enum with clear severity levels.The enum provides a clean abstraction for validation results with appropriate comparison and exit code resolution methods.
src/Command/ValidateTranslationCommand.php (2)
32-32: Good initialization of ResultType property.Starting with
SUCCESSand updating based on validation results is the correct approach.
187-201: Clean refactoring of result handling logic.The use of
ResultTypefor determining output and exit codes is much cleaner than the previous approach. The logic is now centralized and easier to maintain.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/src/Validator/ResultTypeTest.php (1)
29-66: Excellent comprehensive testing of resolveErrorToCommandExitCode() method.The test covers all 12 possible combinations (3 enum values × 2 dryRun flags × 2 strict flags) with clear comments explaining each scenario. The expected results align perfectly with the implementation logic:
- ERROR fails unless it's a dry run
- WARNING fails only in strict mode
- SUCCESS always succeeds
The explicit approach makes the test cases very clear and maintainable.
For future maintenance, you could consider using a data provider to make the test more concise:
/** * @dataProvider resolveErrorToCommandExitCodeProvider */ public function testResolveErrorToCommandExitCode(ResultType $resultType, bool $dryRun, bool $strict, int $expected): void { $this->assertSame($expected, $resultType->resolveErrorToCommandExitCode($dryRun, $strict)); } public function resolveErrorToCommandExitCodeProvider(): array { return [ 'ERROR, not dryRun, not strict' => [ResultType::ERROR, false, false, Command::FAILURE], 'ERROR, dryRun, not strict' => [ResultType::ERROR, true, false, Command::SUCCESS], // ... other test cases ]; }However, the current explicit approach is also perfectly valid and may be more readable for this specific case.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/Command/ValidateTranslationCommand.php(5 hunks)tests/src/Validator/ResultTypeTest.php(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/Command/ValidateTranslationCommand.php
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/src/Validator/ResultTypeTest.php (1)
src/Validator/ResultType.php (3)
max(15-18)notFullySuccessful(20-23)resolveErrorToCommandExitCode(25-36)
🔇 Additional comments (3)
tests/src/Validator/ResultTypeTest.php (3)
1-10: Well-structured test file with proper imports and conventions.The file follows PHP and PHPUnit best practices with proper strict types declaration, namespace organization, and necessary imports.
13-20: Comprehensive test coverage for the max() method.The test cases cover all possible severity comparisons between ResultType enum values, ensuring the method correctly identifies the more severe result type in all scenarios.
22-27: Complete coverage of notFullySuccessful() method.All three enum values are tested with correct expected outcomes - SUCCESS returns false while WARNING and ERROR return true.
Summary by CodeRabbit
New Features
Refactor