8000 [Validator] code review changes · symfony/symfony@5a92e1b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a92e1b

Browse files
committed
[Validator] code review changes
- changed message for UnexpectedTypeException - inverted type check - add tests for invalid zero values
1 parent 77ba93a commit 5a92e1b

7 files changed

+38
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function validate($value, Constraint $constraint)
3131
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NegativeOrZero');
3232
}
3333

34-
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35-
throw new UnexpectedTypeException($value, 'int|float|string');
34+
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
35+
throw new UnexpectedTypeException($value, 'int, float or string');
3636
}
3737

3838
$value = (float) $value;

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

Lines changed: 3 additions & 3 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function validate($value, Constraint $constraint)
3131
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Negative');
3232
}
3333

34-
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35-
throw new UnexpectedTypeException($value, 'int|float|string');
34+
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
35+
throw new UnexpectedTypeException($value, 'int, float or string');
3636
}
3737

3838
$value = (float) $value;
3939

40-
if (empty($value)) {
40+
if (.0 === $value) {
4141
$this->context->buildViolation($constraint->message)
4242
->setParameter('{{ value }}', $this->formatValue($value))
4343
->setCode(Negative::IS_ZERO_ERROR)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function validate($value, Constraint $constraint)
3131
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\PositiveOrZero');
3232
}
3333

34-
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35-
throw new UnexpectedTypeException($value, 'int|float|string');
34+
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
35+
throw new UnexpectedTypeException($value, 'int, float or string');
3636
}
3737

3838
$value = (float) $value;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function validate($value, Constraint $constraint)
3131
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Positive');
3232
}
3333

34-
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35-
throw new UnexpectedTypeException($value, 'int|float|string');
34+
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
35+
throw new UnexpectedTypeException($value, 'int, float or string');
3636
}
3737

3838
$value = (float) $value;
3939

40-
if (empty($value)) {
40+
if (.0 === $value) {
4141
$this->context->buildViolation($constraint->message)
4242
->setParameter('{{ value }}', $this->formatValue($value))
4343
->setCode(Positive::IS_ZERO_ERROR)

src/Symfony/Component/Validator/Tests/Constraints/NegativeOrZeroValidatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function getValidValues()
3939
{
4040
return array(
4141
array(0),
42+
array(0.0),
43+
array(.0),
4244
array(-1),
4345
array(-1.1),
4446
array('-1.1'),

src/Symfony/Component/Validator/Tests/Constraints/NegativeValidatorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public function getInvalidValues()
7070
);
7171
}
7272

73-
public function testZeroIsInvalid()
73+
/**
74+
* @dataProvider getZeroValues
75+
*/
76+
public function testZeroIsInvalid($value)
7477
{
75-
$value = 0;
7678
$constraint = new Negative(array(
7779
'message' => 'myMessage',
7880
));
@@ -85,6 +87,15 @@ public function testZeroIsInvalid()
8587
->assertRaised();
8688
}
8789

90+
public function getZeroValues()
91+
{
92+
return array(
93+
array(0),
94+
array(0.0),
95+
array(.0),
96+
);
97+
}
98+
8899
/**
89100
* @dataProvider getWrongTypeValues
90101
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException

src/Symfony/Component/Validator/Tests/Constraints/PositiveValidatorTest.php

Lines changed: 13 additions & 2 deletions
< 924F td data-grid-cell-id="diff-d254a6234f7b5535f0fc0bb65d50da2b2b0e7e60f0dfcefe163c4ff022e721ed-78-80-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">80
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public function getInvalidValues()
7070
);
7171
}
7272

73-
public function testZeroIsInvalid()
73+
/**
74+
* @dataProvider getZeroValues
75+
*/
76+
public function testZeroIsInvalid($value)
7477
{
75-
$value = 0;
7678
$constraint = new Positive(array(
7779
'message' => 'myMessage',
78
));
@@ -85,6 +87,15 @@ public function testZeroIsInvalid()
8587
->assertRaised();
8688
}
8789

90+
public function getZeroValues()
91+
{
92+
return array(
93+
array(0),
94+
array(0.0),
95+
array(.0),
96+
);
97+
}
98+
8899
/**
89100
* @dataProvider getWrongTypeValues
90101
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException

0 commit comments

Comments
 (0)
0