bug #34842 [ExpressionLanguage] Process division by zero (tigr1991)
This PR was merged into the 3.4 branch.
Discussion
----------
[ExpressionLanguage] Process division by zero
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR |
To be able to catch the error in expression like ` 1 / 0`
**Before PR:**
```
try {
1 / 0;
} catch (\Throwable $e) {
// It won't be caught anyway
// PHP Warning: Division by zero in...
}
try {
1 % 0;
} catch (\Throwable $e) {
// It will be caught since PHP7
// \DivisionByZeroError with message `Modulo by zero`
}
```
**After PR:**
```
try {
1 / 0;
} catch (\Throwable $e) {
// It will be caught
// \DivisionByZeroError with message `Division by zero`
}
try {
1 % 0;
} catch (\Throwable $e) {
// It will be caught
// \DivisionByZeroError with message `Modulo by zero`
}
```
Commits
-------
02ab72ab30
[ExpressionLanguage][Node][BinaryNode] Process division by zero
This commit is contained in:
commit
2eacbc5b54
@ -147,8 +147,16 @@ class BinaryNode extends Node
|
|||||||
case '*':
|
case '*':
|
||||||
return $left * $right;
|
return $left * $right;
|
||||||
case '/':
|
case '/':
|
||||||
|
if (0 == $right) {
|
||||||
|
throw new \DivisionByZeroError('Division by zero');
|
||||||
|
}
|
||||||
|
|
||||||
return $left / $right;
|
return $left / $right;
|
||||||
case '%':
|
case '%':
|
||||||
|
if (0 == $right) {
|
||||||
|
throw new \DivisionByZeroError('Modulo by zero');
|
||||||
|
}
|
||||||
|
|
||||||
return $left % $right;
|
return $left % $right;
|
||||||
case 'matches':
|
case 'matches':
|
||||||
return preg_match($right, $left);
|
return preg_match($right, $left);
|
||||||
|
@ -17,7 +17,8 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^5.5.9|>=7.0.8",
|
"php": "^5.5.9|>=7.0.8",
|
||||||
"symfony/cache": "~3.1|~4.0"
|
"symfony/cache": "~3.1|~4.0",
|
||||||
|
"symfony/polyfill-php70": "~1.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": { "Symfony\\Component\\ExpressionLanguage\\": "" },
|
"psr-4": { "Symfony\\Component\\ExpressionLanguage\\": "" },
|
||||||
|
Reference in New Issue
Block a user