Skip to content

Commit beffe9e

Browse files
committed
Improved ValidationError translation extraction
1 parent 2f4870d commit beffe9e

18 files changed

Lines changed: 438 additions & 162 deletions

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9156,18 +9156,6 @@ parameters:
91569156
count: 1
91579157
path: src/lib/MVC/Symfony/Translation/TranslatableExceptionsFileVisitor.php
91589158

9159-
-
9160-
message: '#^Access to an undefined property PhpParser\\Node\\Arg\|PhpParser\\Node\\VariadicPlaceholder\:\:\$value\.$#'
9161-
identifier: property.notFound
9162-
count: 2
9163-
path: src/lib/MVC/Symfony/Translation/ValidationErrorFileVisitor.php
9164-
9165-
-
9166-
message: '#^Method Ibexa\\Core\\MVC\\Symfony\\Translation\\ValidationErrorFileVisitor\:\:setLogger\(\) has no return type specified\.$#'
9167-
identifier: missingType.return
9168-
count: 1
9169-
path: src/lib/MVC/Symfony/Translation/ValidationErrorFileVisitor.php
9170-
91719159
-
91729160
message: '#^Parameter \#1 \$desc of method JMS\\TranslationBundle\\Model\\Message\:\:setDesc\(\) expects string, string\|null given\.$#'
91739161
identifier: argument.type

src/bundle/Core/Resources/config/services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,10 @@ services:
371371
decoration_priority: 500
372372
arguments:
373373
$inner: '@.inner'
374+
375+
376+
jms_translation.doc_parser:
377+
class: Doctrine\Common\Annotations\DocParser
378+
calls:
379+
- [setImports, [{ desc: 'JMS\TranslationBundle\Annotation\Desc', domain: 'Ibexa\Core\MVC\Symfony\Translation\Annotation\Domain', ignore: 'JMS\TranslationBundle\Annotation\Ignore', meaning: 'JMS\TranslationBundle\Annotation\Meaning' }]]
380+
- [setIgnoreNotImportedAnnotations, [true]]

src/bundle/Core/Resources/translations/ibexa_repository_exceptions.en.xlf

Lines changed: 98 additions & 73 deletions
Large diffs are not rendered by default.

src/bundle/Core/Resources/translations/validators.en.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<trans-unit id="cb73b740405234713ab9927dd526edf48a250f60" resname="ibexa.identifier_already_exists">
1010
<source>Identifier already exists.</source>
11-
<target state="new">Identifier already exists.</target>
11+
<target>Identifier already exists.</target>
1212
<note>key: ibexa.identifier_already_exists</note>
1313
</trans-unit>
1414
</body>

src/lib/FieldType/BaseTextType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ protected function checkValueStructure(BaseValue $value): void
6363
protected function buildUnknownValidatorError(string $parameterName, string $validatorIdentifier): ValidationError
6464
{
6565
return new ValidationError(
66-
"Validator '$parameterName' is unknown",
66+
/** @Desc("Validator '%parameter%' is unknown") */
67+
"Validator '%parameter%' is unknown",
6768
null,
68-
[
69-
$parameterName => $validatorIdentifier,
70-
]
69+
['%parameter%' => $parameterName]
7170
);
7271
}
7372

src/lib/FieldType/ISBN/Type.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Ibexa\Core\FieldType\FieldType;
1414
use Ibexa\Core\FieldType\ValidationError;
1515
use Ibexa\Core\FieldType\Value as BaseValue;
16+
use JMS\TranslationBundle\Annotation\Ignore;
1617
use JMS\TranslationBundle\Model\Message;
1718
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
1819

@@ -157,7 +158,10 @@ public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
157158
} else {
158159
// ISBN-13 check
159160
if (!$this->validateISBN13Checksum($isbnTestNumber, $error)) {
161+
// TODO: Replace the out-parameter error flow with stable translation keys/templates.
162+
// JMS extraction cannot read a ValidationError ID passed through a variable.
160163
$validationErrors[] = new ValidationError(
164+
/** @Ignore */
161165
$error,
162166
null,
163167
[],

src/lib/FieldType/Keyword/Type.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ibexa\Core\FieldType\FieldType;
1515
use Ibexa\Core\FieldType\ValidationError;
1616
use Ibexa\Core\FieldType\Value as BaseValue;
17+
use JMS\TranslationBundle\Annotation\Desc;
1718
use JMS\TranslationBundle\Model\Message;
1819
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
1920

@@ -120,9 +121,10 @@ public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
120121
);
121122
} elseif (mb_strlen($keyword) > self::MAX_KEYWORD_LENGTH) {
122123
$validationErrors[] = new ValidationError(
123-
'Keyword value must be less than or equal to ' . self::MAX_KEYWORD_LENGTH . ' characters.',
124+
/** @Desc("Keyword value must be less than or equal to %max_keyword_length% characters.") */
125+
'ibexa.keyword.value.max_length',
124126
null,
125-
[],
127+
['%max_keyword_length%' => self::MAX_KEYWORD_LENGTH],
126128
'values'
127129
);
128130
}

src/lib/FieldType/Validator/BaseNumericValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Ibexa\Core\FieldType\ValidationError;
1212
use Ibexa\Core\FieldType\Validator;
13+
use JMS\TranslationBundle\Annotation\Ignore;
1314

1415
abstract class BaseNumericValidator extends Validator
1516
{
@@ -24,7 +25,10 @@ public function validateConstraints($constraints): array
2425
foreach ($constraints as $name => $value) {
2526
$validationErrorMessage = $this->getConstraintsValidationErrorMessage($name, $value);
2627
if (null !== $validationErrorMessage) {
28+
// TODO: Return stable translation keys/templates instead of a computed message string.
29+
// JMS extraction cannot read a ValidationError ID passed through a variable.
2730
$validationErrors[] = new ValidationError(
31+
/** @Ignore */
2832
$validationErrorMessage,
2933
null,
3034
[

src/lib/FieldType/Validator/StringLengthValidator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ibexa\Core\FieldType\ValidationError;
1212
use Ibexa\Core\FieldType\Validator;
1313
use Ibexa\Core\FieldType\Value as BaseValue;
14+
use JMS\TranslationBundle\Annotation\Desc;
1415

1516
/**
1617
* Validator for checking min. and max. length of strings.
@@ -47,11 +48,10 @@ public function validateConstraints($constraints)
4748
case 'maxStringLength':
4849
if ($value !== false && !is_int($value) && !(null === $value)) {
4950
$validationErrors[] = new ValidationError(
50-
sprintf('Validator parameter \'%s\' value must be of integer type', self::PARAMETER_NAME),
51+
/** @Desc("Validator parameter '%parameter%' value must be of integer type") */
52+
"Validator parameter '%parameter%' value must be of integer type",
5153
null,
52-
[
53-
self::PARAMETER_NAME => $name,
54-
]
54+
[self::PARAMETER_NAME => $name]
5555
);
5656
} elseif ($value < 0) {
5757
$validationErrors[] = new ValidationError(

src/lib/Limitation/SiteAccessLimitationType.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ public function validate(APILimitationValue $limitationValue): array
8383
foreach ($limitationValue->limitationValues as $key => $value) {
8484
if (!isset($siteAccessList[$value])) {
8585
$validationErrors[] = new ValidationError(
86-
"\$limitationValue->limitationValues[%key%] => Invalid SiteAccess value \"$value\"",
86+
/** @Desc("$limitationValue->limitationValues[%key%] => Invalid SiteAccess value '%value%'") */
87+
'$limitationValue->limitationValues[%key%] => Invalid SiteAccess value "%value%"',
8788
null,
88-
[
89-
'value' => $value,
90-
'key' => $key,
91-
]
89+
['%key%' => $key, '%value%' => $value]
9290
);
9391
}
9492
}

0 commit comments

Comments
 (0)