|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Smart Analysis project 2015-2016 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PHPSA\Compiler\Expression\Operators\Arithmetical; |
|
9
|
|
|
|
|
10
|
|
|
use PHPSA\CompiledExpression; |
|
11
|
|
|
use PHPSA\Context; |
|
12
|
|
|
use PHPSA\Compiler\Expression; |
|
13
|
|
|
use PHPSA\Compiler\Expression\AbstractExpressionCompiler; |
|
14
|
|
|
|
|
15
|
|
|
class Div extends AbstractExpressionCompiler |
|
16
|
|
|
{ |
|
17
|
|
|
protected $name = 'PhpParser\Node\Expr\BinaryOp\Div'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {expr} / {expr} |
|
21
|
|
|
* |
|
22
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\Div $expr |
|
23
|
|
|
* @param Context $context |
|
24
|
|
|
* @return CompiledExpression |
|
25
|
|
|
*/ |
|
26
|
37 |
|
protected function compile($expr, Context $context) |
|
27
|
|
|
{ |
|
28
|
37 |
|
$left = $context->getExpressionCompiler()->compile($expr->left); |
|
29
|
37 |
|
$right = $context->getExpressionCompiler()->compile($expr->right); |
|
30
|
|
|
|
|
31
|
37 |
|
if ($left->isEquals(0)) { |
|
32
|
5 |
|
$context->notice( |
|
33
|
5 |
|
'division-zero', |
|
34
|
5 |
|
sprintf('You are trying to divide from 0: %s/{expr}', $left->getValue()), |
|
35
|
|
|
$expr |
|
36
|
5 |
|
); |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
37 |
|
if ($right->isEquals(0)) { |
|
40
|
1 |
|
$context->notice( |
|
41
|
1 |
|
'division-zero', |
|
42
|
1 |
|
sprintf('You are trying to divide by 0: {expr}/%s', $right->getValue()), |
|
43
|
|
|
$expr |
|
44
|
1 |
|
); |
|
45
|
|
|
|
|
46
|
1 |
|
return new CompiledExpression(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
36 |
|
switch ($left->getType()) { |
|
50
|
36 |
|
case CompiledExpression::INTEGER: |
|
51
|
36 |
|
case CompiledExpression::DOUBLE: |
|
52
|
36 |
|
case CompiledExpression::BOOLEAN: |
|
53
|
35 |
|
switch ($right->getType()) { |
|
54
|
35 |
|
case CompiledExpression::BOOLEAN: |
|
55
|
|
|
/** |
|
56
|
|
|
* Boolean is true since isEquals(0) check did not pass before |
|
57
|
|
|
* {expr}/1 = {expr} |
|
58
|
|
|
*/ |
|
59
|
|
|
|
|
60
|
3 |
|
$context->notice( |
|
61
|
3 |
|
'division-by-true', |
|
62
|
3 |
|
'You are trying to divide by true: {expr}/true ~ {expr}/1 = {expr}', |
|
63
|
|
|
$expr |
|
64
|
3 |
|
); |
|
65
|
|
|
//no break |
|
66
|
35 |
|
case CompiledExpression::INTEGER: |
|
67
|
35 |
|
case CompiledExpression::DOUBLE: |
|
68
|
35 |
|
case CompiledExpression::BOOLEAN: |
|
69
|
35 |
|
return CompiledExpression::fromZvalValue($left->getValue() / $right->getValue()); |
|
70
|
|
|
} |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return new CompiledExpression(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|