forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeReflectionFromNodeRuleTest.php
More file actions
110 lines (94 loc) · 2.77 KB
/
AttributeReflectionFromNodeRuleTest.php
File metadata and controls
110 lines (94 loc) · 2.77 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection;
use PhpParser\Node;
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Node\InFunctionNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\VerbosityLevel;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function count;
use function implode;
use function sprintf;
/**
* @extends RuleTestCase<Rule<NodeAbstract>>
*/
class AttributeReflectionFromNodeRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new /** @implements Rule<NodeAbstract> */ class implements Rule {
public function getNodeType(): string
{
return NodeAbstract::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof InClassMethodNode) {
$reflection = $node->getMethodReflection();
} elseif ($node instanceof InFunctionNode) {
$reflection = $node->getFunctionReflection();
} else {
return [];
}
$parts = [];
foreach ($reflection->getAttributes() as $attribute) {
$args = [];
foreach ($attribute->getArgumentTypes() as $argName => $argType) {
$args[] = sprintf('%s: %s', $argName, $argType->describe(VerbosityLevel::precise()));
}
$parts[] = sprintf('#[%s(%s)]', $attribute->getName(), implode(', ', $args));
}
foreach ($reflection->getParameters() as $parameter) {
$parameterAttributes = [];
foreach ($parameter->getAttributes() as $parameterAttribute) {
$parameterArgs = [];
foreach ($parameterAttribute->getArgumentTypes() as $argName => $argType) {
$parameterArgs[] = sprintf('%s: %s', $argName, $argType->describe(VerbosityLevel::precise()));
}
$parameterAttributes[] = sprintf('#[%s(%s)]', $parameterAttribute->getName(), implode(', ', $parameterArgs));
}
if (count($parameterAttributes) === 0) {
continue;
}
$parts[] = sprintf('$%s: %s', $parameter->getName(), implode(', ', $parameterAttributes));
}
if (count($parts) === 0) {
return [];
}
return [
RuleErrorBuilder::message(implode(', ', $parts))->identifier('test.attributes')->build(),
];
}
};
}
#[RequiresPhp('>= 8.0')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/attribute-reflection.php'], [
[
'#[AttributeReflectionTest\MyAttr(one: 7, two: 8)], $test: #[AttributeReflectionTest\MyAttr(one: 9, two: 10)]',
29,
],
[
'#[AttributeReflectionTest\MyAttr()]',
39,
],
[
'#[AttributeReflectionTest\Nonexistent()]',
44,
],
[
'#[AttributeReflectionTest\MyAttr(one: 11, two: 12)]',
54,
],
[
'#[AttributeReflectionTest\MyAttr(one: 28, two: 29)]',
59,
],
]);
}
}