-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] add exception when intl component not found #28513
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
[Validator] add exception when intl component not found #28513
Conversation
ronfroy
commented
Sep 19, 2018
Q | A |
---|---|
Branch? | master |
Bug fix? | no |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
License | MIT |
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.
Good idea. Here are some comments.
@@ -28,6 +29,10 @@ class CountryValidator extends ConstraintValidator | |||
*/ | |||
public function validate($value, Constraint $constraint) | |||
{ | |||
if (!class_exists('Intl')) { |
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.
should be Intl::class instead.
@@ -28,6 +29,10 @@ class CountryValidator extends ConstraintValidator | |||
*/ | |||
public function validate($value, Constraint $constraint) | |||
{ | |||
if (!class_exists('Intl')) { | |||
throw new RuntimeException('symfony/intl is required to use the Country constraint'); |
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.
missing dot at end of message, also I'd suggest to be a little bit more explicit (same in other messages below):
The "symfony/intl" component is required to use the Country constraint.
@nicolas-grekas fixed |
@@ -40,6 +41,10 @@ public function validate($value, Constraint $constraint) | |||
throw new UnexpectedTypeException($value, 'string'); | |||
} | |||
|
|||
if (!class_exists(Intl::class)) { | |||
throw new RuntimeException('The "symfony/intl" component is required to use the Country constraint.'); |
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.
hmm, shouldn't it be a LogicException
instead?
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.
i think not, this is not a logic issue.
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.
looking at the code, we use LogicException quite often in similar situations, so could be worth for consistency at least (there are a few similar cases using RuntimeException, but they're not the majority and could be worth fixing)
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.
I would have used RuntimeException
too ... but if we use LogicException
in similar situations, it's better to keep consistency. @ronfroy can you please update this? Thanks!
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.
how can we use LogicException
elsewhere if this PR introduces it as a new class?
For this case we use the RuntimeException
already 👍
symfony/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php
Lines 56 to 63 in 645b016
if (null === $this->expressionLanguage) { | |
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { | |
throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | |
} | |
$this->expressionLanguage = new ExpressionLanguage(); | |
} | |
return $this->expressionLanguage; |
@@ -40,6 +41,10 @@ public function validate($value, Constraint $constraint) | |||
throw new UnexpectedTypeException($value, 'string'); | |||
} | |||
|
|||
if (!class_exists(Intl::class)) { | |||
throw new RuntimeException('The "symfony/intl" component is required to use the Country constraint.'); |
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.
I would have used RuntimeException
too ... but if we use LogicException
in similar situations, it's better to keep consistency. @ronfroy can you please update this? Thanks!
done |
can you update src/Symfony/Component/Validator/Constraints/BicValidator.php and src/Symfony/Component/Validator/Mapping/Factory/BlackHoleMetadataFactory.php to use the new exception also please? |
@nicolas-grekas done |
and src/Symfony/Component/Validator/ValidatorBuilder.php |
for consitency we should change Runtime to Logic in |
@@ -17,7 +17,7 @@ | |||
class BlackHoleMetadataFactoryTest extends TestCase | |||
{ | |||
/** | |||
* @expectedException \LogicException | |||
* @expectedException \Symfony\Component\Validator\Exception\LogicException; |
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.
not sure about the trailing;
@@ -191,8 +192,8 @@ public function enableAnnotationMapping(Reader $annotationReader = null) | |||
} | |||
|
|||
if (null === $annotationReader) { | |||
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader') || !class_exists('Doctrine\Common\Cache\ArrayCache')) { | |||
throw new \RuntimeException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.'); | |||
if (!class_exists(Doctrine\Common\Annotations\AnnotationReader::class) || !class_exists(Doctrine\Common\Cache\ArrayCache::class)) { |
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.
should be moved as "use" statements.
in another PR :) |
Thank you @ronfroy. |
…(ronfroy) This PR was squashed before being merged into the 4.2-dev branch (closes #28513). Discussion ---------- [Validator] add exception when intl component not found | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT <!-- Add some exception when the Intl component is not found but required on some constraints --> Commits ------- b6f29f4 [Validator] add exception when intl component not found
…ro0NL) This PR was merged into the 4.2-dev branch. Discussion ---------- Favor LogicException for missing classes & functions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes-ish | BC breaks? | no-ish <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #28513 (comment) | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- c762735 Favor LogicException for missing classes & functions
This PR was merged into the 4.2-dev branch. Discussion ---------- [Form] Check for Intl availibility | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Same as #28513 for the form component Commits ------- 73c688c [Form] Check for Intl availibility