Repository navigation
Expand file tree
/
Copy pathConstantToFunctionParameterMapTest.php
More file actions
162 lines (137 loc) · 6.01 KB
/
Copy pathConstantToFunctionParameterMapTest.php
File metadata and controls
162 lines (137 loc) · 6.01 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection;
use PhpParser\Node\Name;
use PHPStan\Testing\PHPStanTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function array_keys;
use function array_merge;
use function count;
use function explode;
use function implode;
use function sprintf;
use function str_contains;
#[RequiresPhp('>= 8.0.0')]
class ConstantToFunctionParameterMapTest extends PHPStanTestCase
{
public function testMapIsValid(): void
{
$map = require __DIR__ . '/../../../resources/constantToFunctionParameterMap.php';
$this->assertIsArray($map);
$reflectionProvider = self::createReflectionProvider();
foreach ($map as $entry => $parameters) {
$this->assertIsString($entry, 'Entry key must be a string.');
$this->assertIsArray($parameters, sprintf('Parameters for %s must be an array.', $entry));
if (str_contains($entry, '::')) {
// Method entry: Class::method
[$className, $methodName] = explode('::', $entry, 2);
$this->assertTrue(
$reflectionProvider->hasClass($className),
sprintf('Class %s not found in reflection (from %s).', $className, $entry),
);
$classReflection = $reflectionProvider->getClass($className);
$this->assertTrue(
$classReflection->hasMethod($methodName),
sprintf('Method %s not found in reflection.', $entry),
);
$methodReflection = $classReflection->getNativeMethod($methodName);
$variants = $methodReflection->getVariants();
$this->assertNotCount(0, $variants, sprintf('Method %s has no variants.', $entry));
$reflectionParameters = $variants[0]->getParameters();
} else {
$this->assertNotSame('', $entry);
// Function entry
$nameNode = new Name($entry);
$this->assertTrue(
$reflectionProvider->hasFunction($nameNode, null),
sprintf('Function %s() not found in reflection.', $entry),
);
$functionReflection = $reflectionProvider->getFunction($nameNode, null);
$variants = $functionReflection->getVariants();
$this->assertNotCount(0, $variants, sprintf('Function %s() has no variants.', $entry));
$reflectionParameters = $variants[0]->getParameters();
}
$reflectionParameterNames = [];
foreach ($reflectionParameters as $reflectionParameter) {
$reflectionParameterNames[] = $reflectionParameter->getName();
}
foreach ($parameters as $parameterName => $config) {
$this->assertIsString($parameterName, sprintf('Parameter name for %s must be a string.', $entry));
$this->assertContains(
$parameterName,
$reflectionParameterNames,
sprintf(
'Parameter $%s not found in %s. Available parameters: $%s',
$parameterName,
$entry,
implode(', $', $reflectionParameterNames),
),
);
$this->assertIsArray($config, sprintf('Config for %s($%s) must be an array.', $entry, $parameterName));
$this->assertArrayHasKey('type', $config, sprintf('Missing "type" key for %s($%s).', $entry, $parameterName));
$this->assertContains($config['type'], ['single', 'bitmask'], sprintf('Invalid type "%s" for %s($%s).', $config['type'], $entry, $parameterName));
$this->assertArrayHasKey('constants', $config, sprintf('Missing "constants" key for %s($%s).', $entry, $parameterName));
$this->assertIsArray($config['constants'], sprintf('Constants for %s($%s) must be an array.', $entry, $parameterName));
$this->assertNotCount(0, $config['constants'], sprintf('Constants for %s($%s) must not be empty.', $entry, $parameterName));
foreach ($config['constants'] as $constantName) {
$this->assertIsString($constantName, sprintf('Constant name for %s($%s) must be a string.', $entry, $parameterName));
if (str_contains($constantName, '::')) {
// Class constant: Class::CONSTANT
[$constClassName, $constName] = explode('::', $constantName, 2);
$this->assertTrue(
$reflectionProvider->hasClass($constClassName),
sprintf('Class %s not found in reflection (constant %s used in %s($%s)).', $constClassName, $constantName, $entry, $parameterName),
);
$constClassReflection = $reflectionProvider->getClass($constClassName);
$this->assertTrue(
$constClassReflection->hasConstant($constName),
sprintf('Constant %s not found in reflection (used in %s($%s)).', $constantName, $entry, $parameterName),
);
} else {
$this->assertNotSame('', $constantName);
// Global constant
$constantNameNode = new Name($constantName);
$this->assertTrue(
$reflectionProvider->hasConstant($constantNameNode, null),
sprintf('Constant %s (used in %s($%s)) not found in reflection.', $constantName, $entry, $parameterName),
);
}
}
$allowedKeys = ['type', 'constants', 'exclusiveGroups'];
foreach (array_keys($config) as $key) {
$this->assertContains($key, $allowedKeys, sprintf('Unknown key "%s" in config for %s($%s).', $key, $entry, $parameterName));
}
if (!isset($config['exclusiveGroups'])) {
continue;
}
$this->assertSame('bitmask', $config['type'], sprintf('exclusiveGroups only makes sense for bitmask type in %s($%s).', $entry, $parameterName));
$this->assertIsArray($config['exclusiveGroups']);
foreach ($config['exclusiveGroups'] as $groupIndex => $group) {
$this->assertIsArray($group, sprintf('Exclusive group #%d for %s($%s) must be an array.', $groupIndex, $entry, $parameterName));
$this->assertGreaterThanOrEqual(2, count($group), sprintf('Exclusive group #%d for %s($%s) must have at least 2 constants.', $groupIndex, $entry, $parameterName));
foreach ($group as $constantName) {
$this->assertContains(
$constantName,
$config['constants'],
sprintf(
'Constant %s in exclusive group #%d for %s($%s) is not in the constants list.',
$constantName,
$groupIndex,
$entry,
$parameterName,
),
);
}
}
}
}
}
public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/constantToFunctionParameterMap.neon',
],
);
}
}