-
Notifications
You must be signed in to change notification settings - Fork 17
IBX-9727: Fixed strict types of translatable Exceptions and Values #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2c28150
to
f954065
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below you can find some comments to the changes:
src/lib/MVC/Symfony/Templating/Exception/InvalidResponseException.php
Outdated
Show resolved
Hide resolved
null, | ||
[ | ||
'value' => $limitationValue->limitationValues, | ||
'identifier' => $limitationValue->getIdentifier(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
This is my best guess of what the Author meant to place here. At least it makes the most sense.
$this->setParameters(['%module%' => $module, '%function%' => $function]); | ||
|
||
if ($properties) { | ||
if (!empty($properties)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
Actually needs to behave the same for []
and null
(discovered by test coverage).
if (!is_int($parameters['maxFileSize']) && $parameters['maxFileSize'] !== null) { | ||
$validationErrors[] = new ValidationError( | ||
'Validator %validator% expects parameter %parameter% to be of %type%.', | ||
'Validator %validator% expects parameter %parameter% to be of %type% type.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
The message actually is not grammatically correct without the type
at the end (see the unit test coverage). Hopefully 🤞 it's not covered literally by behat anywhere. Are those in xliff files somewhere though?
|
||
if (count($groups) !== 1) { | ||
throw new Exception\TypeGroupNotFound($groupId); | ||
throw new Exception\TypeGroupNotFound((string)$groupId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
We're using that one with both numeric IDs and string identifiers. I prefer this way instead of doing int|string
on the exception class argument type.
final class ContentFieldValidationExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @see \Ibexa\Core\Base\Exceptions\ContentFieldValidationException::MAX_MESSAGES_NUMBER | ||
*/ | ||
private const int MAX_MESSAGES_NUMBER = 32; | ||
|
||
public function testTranslatableMessageValidationErrorLimit(): void | ||
{ | ||
$errors = []; | ||
for ($fieldId = 1; $fieldId <= self::MAX_MESSAGES_NUMBER + 1; ++$fieldId) { | ||
$errors[$fieldId] = [ | ||
'eng-GB' => [new ValidationError("Field $fieldId error message")], | ||
]; | ||
} | ||
$exception = ContentFieldValidationException::createNewWithMultiline($errors); | ||
self::assertStringEndsWith( | ||
sprintf('- Limit of %d validation errors has been exceeded.', self::MAX_MESSAGES_NUMBER), | ||
$exception->getMessage() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
Needed to fix ContentFieldValidationException
so validation messages overflow error is an instance of ValidationError
too (previously was a string), for the type consistency in the array. Hence the test double checking the logic.
], | ||
'Expected an instance of "Ibexa\Contracts\Core\Repository\Values\Filter\FilteringSortClause", ' . | ||
'got "integer" at position 1', | ||
'got "int" at position 1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
The mentioned side-effect of using get_debug_type
578764c
to
ee563bf
Compare
src/contracts/FieldType/Generic/ValidationError/ConstraintViolationAdapter.php
Outdated
Show resolved
Hide resolved
src/contracts/FieldType/Generic/ValidationError/ConstraintViolationAdapter.php
Outdated
Show resolved
Hide resolved
src/lib/MVC/Symfony/Templating/Exception/InvalidResponseException.php
Outdated
Show resolved
Hide resolved
9377fb4
to
b3f3870
Compare
@alongosz Do we need to adapt code base in other repositories, before the merge ? |
…ructor with the changes
Co-Authored-By: Konrad Oboza <konradoboza@users.noreply.github.com>
b3f3870
to
19ab54a
Compare
|
* [Tests] Fixed strict types after ibexa/core#590 changes * [PHPStan] Removed resolved issues from the baseline
Related PRs:
Description:
TL;DR;
There are two interfaces with translatable contracts:
\Ibexa\Core\Base\Translatable
\Ibexa\Contracts\Core\Repository\Translatable
fixed strict types there and aligned code extending them. Mostly: exceptions and validation errors.
Interesting parts
After adding strict types I've stumbled upon two "gems" spotted by PHPStan (see: 37833d8):
BinaryBase::validateValidatorConfiguration
method (binary field types), passing target as the part of$values
argument instead of$target
argument. It's used bystrtr
so the bug was harmless and invisible - superfluous value with index 0 was ignored. However if you'd placed literal '0' anywhere in the message, it would get replaced :-)$values
's value, which accepts only scalars.BlockingLimitationType
would crash withArray to string conversion
error in such case. This Limitation is not used anymore in the system, so it was also invisible.The second thing spotted by PHPStan was the fact that we allow for
UnauthorizedException
properties which values are non-strings, but also: other scalars, Stringables (hypothetical case tbh), and nulls. This was my mistake, which was fixed and squashed into ac9304c.While investigating those issues, I've created 2 unit test cases for easier debugging of "things" -
ValidationErrorTest
andUnauthorizedExceptionTest
. Added them here for the future reference of how these "things" work.Minor out of scope change: it's recommended in PHP 8 to use
instead of e.g.:
That looks more compact, however it's not 1:1, as e.g.: "integer" becomes "int". Aligned the existing test case with that change.
For QA:
Vast scope. Regression run needs to be enough.