Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f09d36

Browse files
committed
minor #17357 [Validator] Remove calls to non-existing method (paradajozsef)
This PR was merged into the 3.0 branch. Discussion ---------- [Validator] Remove calls to non-existing method | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR removes some useless code after #16024. **1.** [ConstraintValidator::buildViolation()](https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Validator/ConstraintValidator.php#L64) marked as deprecated in [2.8](https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Validator/ConstraintValidator.php#L64), and has been removed in [3.0](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/Validator/ConstraintValidator.php#L51). But all the ```Symfony/Component/Validator/Constraints/*```validators still making calls to this parent method. **2.** Correct me if I'm wrong, but this condition: ```php $this->context instanceof ExecutionContextInterface ``` in the ```Symfony/Component/Validator/Constraints/*``` validators is useless since 3.0, because the ```$context``` can only be ```ExecutionContextInterface```. I guess the porpuse of this condition was, that in 2.8 there was a [legacy interface](https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php#L63) too. And AFAIK, the ```$context``` is [always initialized](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php#L842) before validation, so no need to check that ```$context``` is whether null or not. **3.** The return value of [ExecutionContextInterface::getViolations()](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php#L239), is in a different namespace, so it should be used. Commits ------- 37fb4e2 Remove calls to non-existing method
2 parents f5db539 + 37fb4e2 commit 9f09d36

33 files changed

+404
-1117
lines changed

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintValidator;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -55,21 +54,12 @@ public function validate($value, Constraint $constraint)
5554
}
5655

5756
if (!$this->compareValues($value, $comparedValue)) {
58-
if ($this->context instanceof ExecutionContextInterface) {
59-
$this->context->buildViolation($constraint->message)
60-
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
61-
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
62-
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
63-
->setCode($this->getErrorCode())
64-
->addViolation();
65-
} else {
66-
$this->buildViolation($constraint->message)
67-
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
68-
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
69-
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
70-
->setCode($this->getErrorCode())
71-
->addViolation();
72-
}
57+
$this->context->buildViolation($constraint->message)
58+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
59+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
60+
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
61+
->setCode($this->getErrorCode())
62+
->addViolation();
7363
}
7464
}
7565

src/Symfony/Component/Validator/Constraints/BlankValidator.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintValidator;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -31,17 +30,10 @@ public function validate($value, Constraint $constraint)
3130
}
3231

3332
if ('' !== $value && null !== $value) {
34-
if ($this->context instanceof ExecutionContextInterface) {
35-
$this->context->buildViolation($constraint->message)
36-
->setParameter('{{ value }}', $this->formatValue($value))
37-
->setCode(Blank::NOT_BLANK_ERROR)
38-
->addViolation();
39-
} else {
40-
$this->buildViolation($constraint->message)
41-
->setParameter('{{ value }}', $this->formatValue($value))
42-
->setCode(Blank::NOT_BLANK_ERROR)
43-
->addViolation();
44-
}
33+
$this->context->buildViolation($constraint->message)
34+
->setParameter('{{ value }}', $this->formatValue($value))
35+
->setCode(Blank::NOT_BLANK_ERROR)
36+
->addViolation();
4537
}
4638
}
4739
}

src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintValidator;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -100,17 +99,10 @@ public function validate($value, Constraint $constraint)
10099
}
101100

102101
if (!is_numeric($value)) {
103-
if ($this->context instanceof ExecutionContextInterface) {
104-
$this->context->buildViolation($constraint->message)
105-
->setParameter('{{ value }}', $this->formatValue($value))
106-
->setCode(CardScheme::NOT_NUMERIC_ERROR)
107-
->addViolation();
108-
} else {
109-
$this->buildViolation($constraint->message)
110-
->setParameter('{{ value }}', $this->formatValue($value))
111-
->setCode(CardScheme::NOT_NUMERIC_ERROR)
112-
->addViolation();
113-
}
102+
$this->context->buildViolation($constraint->message)
103+
->setParameter('{{ value }}', $this->formatValue($value))
104+
->setCode(CardScheme::NOT_NUMERIC_ERROR)
105+
->addViolation();
114106

115107
return;
116108
}
@@ -126,16 +118,9 @@ public function validate($value, Constraint $constraint)
126118
}
127119
}
128120

129-
if ($this->context instanceof ExecutionContextInterface) {
130-
$this->context->buildViolation($constraint->message)
131-
->setParameter('{{ value }}', $this->formatValue($value))
132-
->setCode(CardScheme::INVALID_FORMAT_ERROR)
133-
->addViolation();
134-
} else {
135-
$this->buildViolation($constraint->message)
136-
->setParameter('{{ value }}', $this->formatValue($value))
137-
->setCode(CardScheme::INVALID_FORMAT_ERROR)
138-
->addViolation();
139-
}
121+
$this->context->buildViolation($constraint->message)
122+
->setParameter('{{ value }}', $this->formatValue($value))
123+
->setCode(CardScheme::INVALID_FORMAT_ERROR)
124+
->addViolation();
140125
}
141126
}

src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintValidator;
1716
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -61,19 +60,11 @@ public function validate($value, Constraint $constraint)
6160
if ($constraint->multiple) {
6261
foreach ($value as $_value) {
6362
if (!in_array($_value, $choices, $constraint->strict)) {
64-
if ($this->context instanceof ExecutionContextInterface) {
65-
$this->context->buildViolation($constraint->multipleMessage)
66-
->setParameter('{{ value }}', $this->formatValue($_value))
67-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
68-
->setInvalidValue($_value)
69-
->addViolation();
70-
} else {
71-
$this->buildViolation($constraint->multipleMessage)
72-
->setParameter('{{ value }}', $this->formatValue($_value))
73-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
74-
->setInvalidValue($_value)
75-
->addViolation();
76-
}
63+
$this->context->buildViolation($constraint->multipleMessage)
64+
->setParameter('{{ value }}', $this->formatValue($_value))
65+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
66+
->setInvalidValue($_value)
67+
->addViolation();
7768

7869
return;
7970
}
@@ -82,52 +73,29 @@ public function validate($value, Constraint $constraint)
8273
$count = count($value);
8374

8475
if ($constraint->min !== null && $count < $constraint->min) {
85-
if ($this->context instanceof ExecutionContextInterface) {
86-
$this->context->buildViolation($constraint->minMessage)
87-
->setParameter('{{ limit }}', $constraint->min)
88-
->setPlural((int) $constraint->min)
89-
->setCode(Choice::TOO_FEW_ERROR)
90-
->addViolation();
91-
} else {
92-
$this->buildViolation($constraint->minMessage)
93-
->setParameter('{{ limit }}', $constraint->min)
94-
->setPlural((int) $constraint->min)
95-
->setCode(Choice::TOO_FEW_ERROR)
96-
->addViolation();
97-
}
76+
$this->context->buildViolation($constraint->minMessage)
77+
->setParameter('{{ limit }}', $constraint->min)
78+
->setPlural((int) $constraint->min)
79+
->setCode(Choice::TOO_FEW_ERROR)
80+
->addViolation();
9881

9982
return;
10083
}
10184

10285
if ($constraint->max !== null && $count > $constraint->max) {
103-
if ($this->context instanceof ExecutionContextInterface) {
104-
$this->context->buildViolation($constraint->maxMessage)
105-
->setParameter('{{ limit }}', $constraint->max)
106-
->setPlural((int) $constraint->max)
107-
->setCode(Choice::TOO_MANY_ERROR)
108-
->addViolation();
109-
} else {
110-
$this->buildViolation($constraint->maxMessage)
111-
->setParameter('{{ limit }}', $constraint->max)
112-
->setPlural((int) $constraint->max)
113-
->setCode(Choice::TOO_MANY_ERROR)
114-
->addViolation();
115-
}
86+
$this->context->buildViolation($constraint->maxMessage)
87+
->setParameter('{{ limit }}', $constraint->max)
88+
->setPlural((int) $constraint->max)
89+
->setCode(Choice::TOO_MANY_ERROR)
90+
->addViolation();
11691

11792
return;
11893
}
11994
} elseif (!in_array($value, $choices, $constraint->strict)) {
120-
if ($this->context instanceof ExecutionContextInterface) {
121-
$this->context->buildViolation($constraint->message)
122-
->setParameter('{{ value }}', $this->formatValue($value))
123-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
124-
->addViolation();
125-
} else {
126-
$this->buildViolation($constraint->message)
127-
->setParameter('{{ value }}', $this->formatValue($value))
128-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
129-
->addViolation();
130-
}
95+
$this->context->buildViolation($constraint->message)
96+
->setParameter('{{ value }}', $this->formatValue($value))
97+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
98+
->addViolation();
13199
}
132100
}
133101
}

src/Symfony/Component/Validator/Constraints/CountValidator.php

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintValidator;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -37,45 +36,25 @@ public function validate($value, Constraint $constraint)
3736
$count = count($value);
3837

3938
if (null !== $constraint->max && $count > $constraint->max) {
40-
if ($this->context instanceof ExecutionContextInterface) {
41-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
42-
->setParameter('{{ count }}', $count)
43-
->setParameter('{{ limit }}', $constraint->max)
44-
->setInvalidValue($value)
45-
->setPlural((int) $constraint->max)
46-
->setCode(Count::TOO_MANY_ERROR)
47-
->addViolation();
48-
} else {
49-
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
50-
->setParameter('{{ count }}', $count)
51-
->setParameter('{{ limit }}', $constraint->max)
52-
->setInvalidValue($value)
53-
->setPlural((int) $constraint->max)
54-
->setCode(Count::TOO_MANY_ERROR)
55-
->addViolation();
56-
}
39+
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
40+
->setParameter('{{ count }}', $count)
41+
->setParameter('{{ limit }}', $constraint->max)
42+
->setInvalidValue($value)
43+
->setPlural((int) $constraint->max)
44+
->setCode(Count::TOO_MANY_ERROR)
45+
->addViolation();
5746

5847
return;
5948
}
6049

6150
if (null !== $constraint->min && $count < $constraint->min) {
62-
if ($this->context instanceof ExecutionContextInterface) {
63-
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
64-
->setParameter('{{ count }}', $count)
65-
->setParameter('{{ limit }}', $constraint->min)
66-
->setInvalidValue($value)
67-
->setPlural((int) $constraint->min)
68-
->setCode(Count::TOO_FEW_ERROR)
69-
->addViolation();
70-
} else {
71-
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
72-
->setParameter('{{ count }}', $count)
73-
->setParameter('{{ limit }}', $constraint->min)
74-
->setInvalidValue($value)
75-
->setPlural((int) $constraint->min)
76-
->setCode(Count::TOO_FEW_ERROR)
77-
->addViolation();
78-
}
51+
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
52+
->setParameter('{{ count }}', $count)
53+
->setParameter('{{ limit }}', $constraint->min)
54+
->setInvalidValue($value)
55+
->setPlural((int) $constraint->min)
56+
->setCode(Count::TOO_FEW_ERROR)
57+
->addViolation();
7958
}
8059
}
8160
}

src/Symfony/Component/Validator/Constraints/CountryValidator.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Intl\Intl;
15-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1615
use Symfony\Component\Validator\Constraint;
1716
use Symfony\Component\Validator\ConstraintValidator;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -45,17 +44,10 @@ public function validate($value, Constraint $constraint)
4544
$countries = Intl::getRegionBundle()->getCountryNames();
4645

4746
if (!isset($countries[$value])) {
48-
if ($this->context instanceof ExecutionContextInterface) {
49-
$this->context->buildViolation($constraint->message)
50-
->setParameter('{{ value }}', $this->formatValue($value))
51-
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
52-
->addViolation();
53-
} else {
54-
$this->buildViolation($constraint->message)
55-
->setParameter('{{ value }}', $this->formatValue($value))
56-
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
57-
->addViolation();
58-
}
47+
$this->context->buildViolation($constraint->message)
48+
->setParameter('{{ value }}', $this->formatValue($value))
49+
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
50+
->addViolation();
5951
}
6052
}
6153
}

src/Symfony/Component/Validator/Constraints/CurrencyValidator.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Intl\Intl;
15-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1615
use Symfony\Component\Validator\Constraint;
1716
use Symfony\Component\Validator\ConstraintValidator;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -46,17 +45,10 @@ public function validate($value, Constraint $constraint)
4645
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
4746

4847
if (!isset($currencies[$value])) {
49-
if ($this->context instanceof ExecutionContextInterface) {
50-
$this->context->buildViolation($constraint->message)
51-
->setParameter('{{ value }}', $this->formatValue($value))
52-
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
53-
->addViolation();
54-
} else {
55-
$this->buildViolation($constraint->message)
56-
->setParameter('{{ value }}', $this->formatValue($value))
57-
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
58-
->addViolation();
59-
}
48+
$this->context->buildViolation($constraint->message)
49+
->setParameter('{{ value }}', $this->formatValue($value))
50+
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
51+
->addViolation();
6052
}
6153
}
6254
}

0 commit comments

Comments
 (0)