Skip to content

Commit c2571b0

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 d683912 + 8baf183 commit c2571b0

38 files changed

+44
-44
lines changed

Constraints/AbstractComparisonValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(PropertyAccessorInterface $propertyAccessor = null)
4040
public function validate($value, Constraint $constraint)
4141
{
4242
if (!$constraint instanceof AbstractComparison) {
43-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
43+
throw new UnexpectedTypeException($constraint, AbstractComparison::class);
4444
}
4545

4646
if (null === $value) {

Constraints/AllValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AllValidator extends ConstraintValidator
2626
public function validate($value, Constraint $constraint)
2727
{
2828
if (!$constraint instanceof All) {
29-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
29+
throw new UnexpectedTypeException($constraint, All::class);
3030
}
3131

3232
if (null === $value) {

Constraints/BicValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BicValidator extends ConstraintValidator
2828
public function validate($value, Constraint $constraint)
2929
{
3030
if (!$constraint instanceof Bic) {
31-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Bic');
31+
throw new UnexpectedTypeException($constraint, Bic::class);
3232
}
3333

3434
if (null === $value || '' === $value) {

Constraints/BlankValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BlankValidator extends ConstraintValidator
2626
public function validate($value, Constraint $constraint)
2727
{
2828
if (!$constraint instanceof Blank) {
29-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Blank');
29+
throw new UnexpectedTypeException($constraint, Blank::class);
3030
}
3131

3232
if ('' !== $value && null !== $value) {

Constraints/CallbackValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CallbackValidator extends ConstraintValidator
2929
public function validate($object, Constraint $constraint)
3030
{
3131
if (!$constraint instanceof Callback) {
32-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
32+
throw new UnexpectedTypeException($constraint, Callback::class);
3333
}
3434

3535
$method = $constraint->callback;

Constraints/CardSchemeValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CardSchemeValidator extends ConstraintValidator
9191
public function validate($value, Constraint $constraint)
9292
{
9393
if (!$constraint instanceof CardScheme) {
94-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
94+
throw new UnexpectedTypeException($constraint, CardScheme::class);
9595
}
9696

9797
if (null === $value || '' === $value) {

Constraints/ChoiceValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ChoiceValidator extends ConstraintValidator
3131
public function validate($value, Constraint $constraint)
3232
{
3333
if (!$constraint instanceof Choice) {
34-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
34+
throw new UnexpectedTypeException($constraint, Choice::class);
3535
}
3636

3737
if (!\is_array($constraint->choices) && !$constraint->callback) {

Constraints/CollectionValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CollectionValidator extends ConstraintValidator
2626
public function validate($value, Constraint $constraint)
2727
{
2828
if (!$constraint instanceof Collection) {
29-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
29+
throw new UnexpectedTypeException($constraint, Collection::class);
3030
}
3131

3232
if (null === $value) {

Constraints/CountValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CountValidator extends ConstraintValidator
2626
public function validate($value, Constraint $constraint)
2727
{
2828
if (!$constraint instanceof Count) {
29-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Count');
29+
throw new UnexpectedTypeException($constraint, Count::class);
3030
}
3131

3232
if (null === $value) {

Constraints/CountryValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CountryValidator extends ConstraintValidator
2929
public function validate($value, Constraint $constraint)
3030
{
3131
if (!$constraint instanceof Country) {
32-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Country');
32+
throw new UnexpectedTypeException($constraint, Country::class);
3333
}
3434

3535
if (null === $value || '' === $value) {

0 commit comments

Comments
 (0)