Skip to content

Commit 2c1f057

Browse files
minor #34990 Use ::class constants instead of __NAMESPACE__ when possible (fre5h)
This PR was merged into the 3.4 branch. Discussion ---------- Use `::class` constants instead of `__NAMESPACE__` when possible | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Related to #34987 | License | MIT | Doc PR | no Form component has a lot of built-in form types. Some of them were implemented from the very beginning. In most of them there is a such method ```php /** * {@inheritdoc} */ public function getParent() { return __NAMESPACE__.'\TextType'; } ``` This `getParent()` method was refactored in Symfony 2.8. The upgrade instructions are given here https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#form I think the `__NAMESPACE__.'\TextType';` expression was used because Symfony 2.8 was using `"php": ">=5.3.9"`, and the constant `::class` was added only in PHP 5.5 Now this line can be refactored into ```php /** * {@inheritdoc} */ public function getParent() { return TextType::class; } ``` For example new form types, that were added later, already using the `::class` constant. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ColorType.php#L23 https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/TelType.php#L23 So, in this pull request I propose to refactor all old form types to use `::class` constant. It will give a benefit during the future refactoring, because IDE or static analysers will find all usages of parent class. Unlike the `__NAMESPACE__.'\TextType';` line, which doesn't show the real link to the class for IDE or static analysers, and it could complicate finding all usages of parent class. Commits ------- 32bf50abca Use `::class` constants instead of `__NAMESPACE__` when possible
2 parents 846ff3e + f01da51 commit 2c1f057

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Validator/Constraints/UserPasswordValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryI
3636
public function validate($password, Constraint $constraint)
3737
{
3838
if (!$constraint instanceof UserPassword) {
39-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
39+
throw new UnexpectedTypeException($constraint, UserPassword::class);
4040
}
4141

4242
if (null === $password || '' === $password) {

0 commit comments

Comments
 (0)