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:
Nicolas Grekas 2019-12-10 09:19:19 +01:00
commit 2eacbc5b54
2 changed files with 10 additions and 1 deletions

View File

@ -147,8 +147,16 @@ class BinaryNode extends Node
case '*':
return $left * $right;
case '/':
if (0 == $right) {
throw new \DivisionByZeroError('Division by zero');
}
return $left / $right;
case '%':
if (0 == $right) {
throw new \DivisionByZeroError('Modulo by zero');
}
return $left % $right;
case 'matches':
return preg_match($right, $left);

View File

@ -17,7 +17,8 @@
],
"require": {
"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": {
"psr-4": { "Symfony\\Component\\ExpressionLanguage\\": "" },