forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeprecatedAttributePhpFunctionFromParserReflectionRuleTest.php
More file actions
142 lines (128 loc) · 2.7 KB
/
DeprecatedAttributePhpFunctionFromParserReflectionRuleTest.php
File metadata and controls
142 lines (128 loc) · 2.7 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Annotations;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Node\InFunctionNode;
use PHPStan\Node\InPropertyHookNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function sprintf;
/**
* @extends RuleTestCase<Rule>
*/
class DeprecatedAttributePhpFunctionFromParserReflectionRuleTest extends RuleTestCase
{
/**
* @return Rule<Node>
*/
protected function getRule(): Rule
{
return new /** @implements Rule<Node> */ class implements Rule {
public function getNodeType(): string
{
return Node::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof InFunctionNode) {
$reflection = $node->getFunctionReflection();
} elseif ($node instanceof InClassMethodNode) {
$reflection = $node->getMethodReflection();
} elseif ($node instanceof InPropertyHookNode) {
$reflection = $node->getHookReflection();
} else {
return [];
}
if (!$reflection->isDeprecated()->yes()) {
return [
RuleErrorBuilder::message('Not deprecated')->identifier('tests.notDeprecated')->build(),
];
}
$description = $reflection->getDeprecatedDescription();
if ($description === null) {
return [
RuleErrorBuilder::message('Deprecated')->identifier('tests.deprecated')->build(),
];
}
return [
RuleErrorBuilder::message(sprintf('Deprecated: %s', $description))->identifier('tests.deprecated')->build(),
];
}
};
}
public function testFunctionRule(): void
{
$this->analyse([__DIR__ . '/data/deprecated-attribute-functions.php'], [
[
'Not deprecated',
7,
],
[
'Deprecated',
12,
],
[
'Deprecated: msg',
18,
],
[
'Deprecated: msg2',
24,
],
[
'Deprecated: DeprecatedAttributeFunctions\\fooWithConstantMessage',
30,
],
]);
}
public function testMethodRule(): void
{
$this->analyse([__DIR__ . '/data/deprecated-attribute-methods.php'], [
[
'Not deprecated',
10,
],
[
'Deprecated',
16,
],
[
'Deprecated: msg',
22,
],
[
'Deprecated: msg2',
28,
],
]);
}
#[RequiresPhp('>= 8.4')]
public function testPropertyHookRule(): void
{
$this->analyse([__DIR__ . '/data/deprecated-attribute-property-hooks.php'], [
[
'Not deprecated',
11,
],
[
'Deprecated',
17,
],
[
'Deprecated: msg',
24,
],
[
'Deprecated: msg2',
31,
],
[
'Deprecated: $m::get+DeprecatedAttributePropertyHooks\Foo::$m::get+m',
38,
],
]);
}
}