forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassAttributesRuleTest.php
More file actions
218 lines (204 loc) · 5.6 KB
/
ClassAttributesRuleTest.php
File metadata and controls
218 lines (204 loc) · 5.6 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
/**
* @extends RuleTestCase<ClassAttributesRule>
*/
class ClassAttributesRuleTest extends RuleTestCase
{
private bool $checkExplicitMixed = false;
private bool $checkImplicitMixed = false;
protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
$container = self::getContainer();
return new ClassAttributesRule(
new AttributesCheck(
$reflectionProvider,
new FunctionCallParametersCheck(
new RuleLevelHelper(
$reflectionProvider,
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: $this->checkExplicitMixed,
checkImplicitMixed: $this->checkImplicitMixed,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
),
new NullsafeCheck(),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
$reflectionProvider,
checkArgumentTypes: true,
checkArgumentsPassedByReference: true,
checkExtraArguments: true,
checkMissingTypehints: true,
),
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, checkInternalClassCaseSensitivity: false),
new ClassForbiddenNameCheck($container),
$reflectionProvider,
$container,
),
deprecationRulesInstalled: true,
),
);
}
#[RequiresPhp('>= 8.0')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/class-attributes.php'], [
[
'Attribute class ClassAttributes\Nonexistent does not exist.',
22,
],
[
'Class ClassAttributes\Foo is not an Attribute class.',
28,
],
[
'Class ClassAttributes\Bar referenced with incorrect case: ClassAttributes\baR.',
34,
],
[
'Attribute class ClassAttributes\Baz does not have the class target.',
46,
],
[
'Attribute class ClassAttributes\Bar is not repeatable but is already present above the class.',
59,
],
[
'Attribute class self does not exist.',
65,
],
[
'Attribute class ClassAttributes\AbstractAttribute is abstract.',
77,
],
[
'Attribute class ClassAttributes\Bar does not have a constructor and must be instantiated without any parameters.',
83,
],
[
'Constructor of attribute class ClassAttributes\NonPublicConstructor is not public.',
100,
],
[
'Attribute class ClassAttributes\AttributeWithConstructor constructor invoked with 0 parameters, 2 required.',
118,
],
[
'Attribute class ClassAttributes\AttributeWithConstructor constructor invoked with 1 parameter, 2 required.',
119,
],
[
'Unknown parameter $r in call to ClassAttributes\AttributeWithConstructor constructor.',
120,
],
[
'Interface ClassAttributes\InterfaceAsAttribute is not an Attribute class.',
132,
],
[
'Trait ClassAttributes\TraitAsAttribute is not an Attribute class.',
142,
],
[
'Attribute class ClassAttributes\FlagsAttributeWithPropertyTarget does not have the class target.',
164,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testRuleForEnums(): void
{
$this->analyse([__DIR__ . '/data/enum-attributes.php'], [
[
'Attribute class EnumAttributes\AttributeWithPropertyTarget does not have the class target.',
23,
],
[
'Enum EnumAttributes\EnumAsAttribute is not an Attribute class.',
35,
],
]);
}
#[RequiresPhp('>= 8.0')]
public function testBug7171(): void
{
$this->analyse([__DIR__ . '/data/bug-7171.php'], [
[
'Parameter $repositoryClass of attribute class Bug7171\Entity constructor expects class-string<Bug7171\EntityRepository<T of object>>|null, class-string<stdClass> given.',
66,
],
]);
}
#[RequiresPhp('>= 8.0')]
public function testAllowDynamicPropertiesAttribute(): void
{
$this->analyse([__DIR__ . '/data/allow-dynamic-properties-attribute.php'], []);
}
#[RequiresPhp('>= 8.3')]
public function testBug12011(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12011.php'], [
[
'Parameter #1 $name of attribute class Bug12011\Table constructor expects string|null, int given.',
23,
],
]);
}
#[RequiresPhp('>= 8.2')]
public function testBug12281(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12281.php'], [
[
'Attribute class AllowDynamicProperties cannot be used with readonly class.',
05,
],
[
'Attribute class AllowDynamicProperties cannot be used with enum.',
12,
],
[
'Attribute class AllowDynamicProperties cannot be used with interface.',
15,
],
]);
}
#[RequiresPhp('>= 8.0')]
public function testDeprecatedAttribute(): void
{
$this->analyse([__DIR__ . '/data/deprecated-attr-on-class.php'], [
[
'Attribute class Deprecated cannot be used with class DeprecatedAttrOnClass\Foo.',
7,
],
[
'Attribute class Deprecated cannot be used with interface DeprecatedAttrOnClass\Bar.',
13,
],
[
'Attribute class Deprecated cannot be used with enum DeprecatedAttrOnClass\Baz.',
19,
],
]);
}
}