/
UnreachableIfBranchesRuleTest.php
131 lines (119 loc) · 3.1 KB
/
UnreachableIfBranchesRuleTest.php
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
/**
* @extends RuleTestCase<UnreachableIfBranchesRule>
*/
class UnreachableIfBranchesRuleTest extends RuleTestCase
{
private bool $treatPhpDocTypesAsCertain;
protected function getRule(): Rule
{
return new UnreachableIfBranchesRule(
new ConstantConditionRuleHelper(
new ImpossibleCheckTypeHelper(
$this->createReflectionProvider(),
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
$this->treatPhpDocTypesAsCertain,
);
}
protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return $this->treatPhpDocTypesAsCertain;
}
public function testRule(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/unreachable-if-branches.php'], [
[
'Else branch is unreachable because previous condition is always true.',
15,
],
[
'Elseif branch is unreachable because previous condition is always true.',
25,
],
[
'Else branch is unreachable because previous condition is always true.',
27,
],
[
'Elseif branch is unreachable because previous condition is always true.',
39,
],
[
'Else branch is unreachable because previous condition is always true.',
41,
],
]);
}
public function testDoNotReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/unreachable-if-branches-not-phpdoc.php'], [
[
'Elseif branch is unreachable because previous condition is always true.',
18,
],
[
'Else branch is unreachable because previous condition is always true.',
28,
],
[
'Elseif branch is unreachable because previous condition is always true.',
38,
],
]);
}
public function testReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = true;
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/data/unreachable-if-branches-not-phpdoc.php'], [
[
'Elseif branch is unreachable because previous condition is always true.',
18,
],
[
'Else branch is unreachable because previous condition is always true.',
28,
],
[
'Elseif branch is unreachable because previous condition is always true.',
38,
],
[
'Elseif branch is unreachable because previous condition is always true.',
44,
$tipText,
],
[
'Else branch is unreachable because previous condition is always true.',
54,
//$tipText,
],
[
'Elseif branch is unreachable because previous condition is always true.',
64,
//$tipText,
],
]);
}
public function testBug8076(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8076.php'], []);
}
public function testBug8562(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8562.php'], []);
}
}