forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantConditionRuleHelper.php
More file actions
91 lines (77 loc) · 2.18 KB
/
ConstantConditionRuleHelper.php
File metadata and controls
91 lines (77 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\BooleanType;
#[AutowiredService]
final class ConstantConditionRuleHelper
{
public function __construct(
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
)
{
}
private function shouldSkip(Scope $scope, Expr $expr): bool
{
if (
$expr instanceof Expr\BinaryOp\Equal
|| $expr instanceof Expr\BinaryOp\NotEqual
) {
return true;
}
if (
$expr instanceof Expr\Instanceof_
|| $expr instanceof Expr\BinaryOp\Identical
|| $expr instanceof Expr\BinaryOp\NotIdentical
|| $expr instanceof Expr\BooleanNot
|| $expr instanceof Expr\BinaryOp\BooleanOr
|| $expr instanceof Expr\BinaryOp\BooleanAnd
|| $expr instanceof Expr\Ternary
|| $expr instanceof Expr\Isset_
|| $expr instanceof Expr\Empty_
|| $expr instanceof Expr\BinaryOp\Greater
|| $expr instanceof Expr\BinaryOp\GreaterOrEqual
|| $expr instanceof Expr\BinaryOp\Smaller
|| $expr instanceof Expr\BinaryOp\SmallerOrEqual
) {
// already checked by different rules
return true;
}
if (
(
$expr instanceof FuncCall
|| $expr instanceof MethodCall
|| $expr instanceof Expr\StaticCall
) && !$expr->isFirstClassCallable()
) {
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $expr, false);
if ($isAlways !== null) {
return true;
}
}
return false;
}
public function getBooleanType(Scope $scope, Expr $expr): BooleanType
{
if ($this->shouldSkip($scope, $expr)) {
return new BooleanType();
}
if ($this->treatPhpDocTypesAsCertain) {
return $scope->getType($expr)->toBoolean();
}
return $scope->getNativeType($expr)->toBoolean();
}
public function getNativeBooleanType(Scope $scope, Expr $expr): BooleanType
{
if ($this->shouldSkip($scope, $expr)) {
return new BooleanType();
}
return $scope->getNativeType($expr)->toBoolean();
}
}