Repository navigation
Expand file tree
/
Copy pathUnusedPrivateConstantRuleTest.php
More file actions
155 lines (134 loc) · 3.88 KB
/
Copy pathUnusedPrivateConstantRuleTest.php
File metadata and controls
155 lines (134 loc) · 3.88 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PHPStan\DependencyInjection\DirectExtensionsCollection;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Rules\Constants\AlwaysUsedClassConstantsExtension;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use UnusedPrivateConstant\TestExtension;
/**
* @extends RuleTestCase<UnusedPrivateConstantRule>
*/
class UnusedPrivateConstantRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new UnusedPrivateConstantRule(
new DirectExtensionsCollection([
new class() implements AlwaysUsedClassConstantsExtension {
public function isAlwaysUsed(ClassConstantReflection $constant): bool
{
return $constant->getDeclaringClass()->getName() === TestExtension::class
&& $constant->getName() === 'USED';
}
},
]),
);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/unused-private-constant.php'], [
[
'Constant UnusedPrivateConstant\Foo::BAR_CONST is unused.',
10,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
[
'Constant UnusedPrivateConstant\TestExtension::UNUSED is unused.',
23,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
]);
}
public function testBug5651(): void
{
$this->analyse([__DIR__ . '/data/bug-5651.php'], []);
}
#[RequiresPhp('>= 8.1.0')]
public function testEnums(): void
{
$this->analyse([__DIR__ . '/data/unused-private-constant-enum.php'], [
[
'Constant UnusedPrivateConstantEnum\Foo::TEST_2 is unused.',
9,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
]);
}
public function testBug6758(): void
{
$this->analyse([__DIR__ . '/data/bug-6758.php'], []);
}
#[RequiresPhp('>= 8.0.0')]
public function testBug8204(): void
{
$this->analyse([__DIR__ . '/data/bug-8204.php'], []);
}
#[RequiresPhp('>= 8.1.0')]
public function testBug9005(): void
{
$this->analyse([__DIR__ . '/data/bug-9005.php'], []);
}
public function testBug9765(): void
{
$this->analyse([__DIR__ . '/data/bug-9765.php'], []);
}
#[RequiresPhp('>= 8.3.0')]
public function testDynamicConstantFetch(): void
{
$this->analyse([__DIR__ . '/data/unused-private-constant-dynamic-fetch.php'], [
[
'Constant UnusedPrivateConstantDynamicFetch\Baz::FOO is unused.',
32,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
]);
}
public function testBug14880(): void
{
$this->analyse([__DIR__ . '/data/bug-14880-constant.php'], []);
}
#[RequiresPhp('>= 8.2.0')]
public function testBug12201(): void
{
$this->analyse([__DIR__ . '/data/bug-12201-constant.php'], [
[
'Constant Bug12201Constant\\AnotherKernel::UNUSED is unused.',
23,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
[
'Constant Bug12201Constant\\UsesNeverFetchedTrait::NEVER_FETCHED is unused.',
30,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
[
'Constant Bug12201Constant\\ChildKernel::ALLOWED_ENVS is unused.',
52,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
[
'Constant Bug12201Constant\\RedeclaresAnalysedTrait::REDECLARED is unused.',
69,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
]);
}
public function testBug6415(): void
{
$this->analyse([__DIR__ . '/data/bug-6415.php'], []);
}
public function testBug7184(): void
{
$this->analyse([__DIR__ . '/data/bug-7184.php'], []);
}
public function testBug9385(): void
{
$this->analyse([__DIR__ . '/data/bug-9385.php'], []);
}
public function testPhpdocConstWildcard(): void
{
$this->analyse([__DIR__ . '/data/phpdoc-const-wildcard.php'], []);
}
}