Skip to content

Improve ValidationError translation extraction#735

Draft
mikadamczyk wants to merge 1 commit intomainfrom
validation-error
Draft

Improve ValidationError translation extraction#735
mikadamczyk wants to merge 1 commit intomainfrom
validation-error

Conversation

@mikadamczyk
Copy link
Copy Markdown
Contributor

🎫 Issue N/A

Description:

This PR improves the way translation messages are extracted from ValidationError usages.
Until now, the custom ValidationErrorFileVisitor only accepted scalar string literals as translation IDs. In practice, this meant that a very common and perfectly valid PHP pattern, such as new ValidationError(self::SOME_MESSAGE), could not be extracted, even though the value was statically known. As a result, developers had to either inline translation keys as raw strings, duplicate messages elsewhere for extraction purposes, or silence the extractor with @Ignore.

This change makes the extractor more practical and consistent with real-world usage. ValidationErrorFileVisitor now supports simple class constant references, including self::CONST, static::CONST, and equivalent references to the current class, as long as they can be resolved statically to a string value. The implementation intentionally stays conservative: it does not try to evaluate arbitrary PHP expressions, and it only resolves cases that are safe and predictable during AST-based analysis.

In addition, this PR introduces support for a new @Domain(...) annotation for ValidationError extraction. Previously, messages handled by ValidationErrorFileVisitor always fell back to the visitor’s default domain, which made it impossible to cleanly route validation messages into domains such as validators without relying on separate translation container declarations. With @Domain(...), the extraction domain can now be controlled directly at the ValidationError call site, alongside existing metadata such as @Desc and @Meaning.

The runtime JMS doc parser configuration has also been updated so that the new annotation is recognized during actual translation extraction, not only in isolated unit tests. This was necessary to make the feature work end-to-end in real extraction runs.

The test suite for ValidationErrorFileVisitor has been expanded accordingly. It now covers extraction from string literals, extraction from class constants, imported @Desc usage, and explicit domain override with @Domain(...). The visitor implementation was also slightly tightened to satisfy static analysis more cleanly.

While working on extraction issues, a few remaining ValidationError usages were identified where the translation ID is still passed through a variable. Those cases are not meaningfully extractable with the current static model and were already effectively unsupported. For now, they have been marked with @Ignore and documented with TODO comments explaining that they should
eventually be refactored to stable translation keys or templates. This keeps extraction noise down without pretending those messages are currently extractable.

For QA:

Documentation:

@mikadamczyk mikadamczyk self-assigned this Mar 23, 2026
@mikadamczyk mikadamczyk force-pushed the validation-error branch 3 times, most recently from 720516c to beffe9e Compare March 23, 2026 12:01
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants