-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathPHPUnitAssertionPlugin.php
More file actions
241 lines (229 loc) · 10.8 KB
/
PHPUnitAssertionPlugin.php
File metadata and controls
241 lines (229 loc) · 10.8 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
use ast\Node;
use Phan\AST\UnionTypeVisitor;
use Phan\CodeBase;
use Phan\Language\Context;
use Phan\Language\Element\Comment\Assertion;
use Phan\Language\Element\FunctionInterface;
use Phan\Language\Element\Method;
use Phan\Language\FQSEN\FullyQualifiedClassName;
use Phan\Language\UnionType;
use Phan\PluginV3;
use Phan\PluginV3\AnalyzeFunctionCallCapability;
/**
* Mark PHPUnit helper assertions as having side effects.
*
* - assertTrue
* - assertNull
* - assertNotNull
* - assertFalse
* - assertSame($expected, $actual)
* - assertInstanceof
*
* NOTE: This will probably be rewritten
*/
class PHPUnitAssertionPlugin extends PluginV3 implements AnalyzeFunctionCallCapability
{
/**
* @override
*/
public function getAnalyzeFunctionCallClosures(CodeBase $code_base): array
{
// @phan-suppress-next-line PhanThrowTypeAbsentForCall
$assert_class_fqsen = FullyQualifiedClassName::fromFullyQualifiedString('PHPUnit\Framework\Assert');
if (!$code_base->hasClassWithFQSEN($assert_class_fqsen)) {
if (!getenv('PHAN_PHPUNIT_ASSERTION_PLUGIN_QUIET')) {
// @phan-suppress-next-line PhanPluginRemoveDebugCall
fwrite(STDERR, "PHPUnitAssertionPlugin failed to find class PHPUnit\Framework\Assert, giving up (set environment variable PHAN_PHPUNIT_ASSERTION_PLUGIN_QUIET=1 to ignore this)\n");
}
return [];
}
$result = [];
foreach ($code_base->getClassByFQSEN($assert_class_fqsen)->getMethodMap($code_base) as $method) {
$closure = $this->createClosureForMethod($code_base, $method, $method->getName());
if (!$closure) {
continue;
}
$result[(string)$method->getFQSEN()] = $closure;
}
return $result;
}
/**
* @return ?Closure(CodeBase, Context, FunctionInterface, array, ?Node):void
* @suppress PhanAccessClassConstantInternal, PhanAccessMethodInternal
*/
private function createClosureForMethod(CodeBase $code_base, Method $method, string $name): ?Closure
{
// TODO: Add a helper method which will convert a doc comment and a stub php function source code to a closure for a param index (or indices)
switch (\strtolower($name)) {
case 'asserttrue':
case 'assertnotfalse':
return $method->createClosureForAssertion(
$code_base,
new Assertion(UnionType::empty(), 'unusedParamName', Assertion::IS_TRUE),
0
);
case 'assertfalse':
case 'assertnottrue':
return $method->createClosureForAssertion(
$code_base,
new Assertion(UnionType::empty(), 'unusedParamName', Assertion::IS_FALSE),
0
);
// TODO: Rest of https://github.com/sebastianbergmann/phpunit/issues/3368
case 'assertisstring':
// TODO: Could convert to real types?
return $method->createClosureForAssertion(
$code_base,
new Assertion(UnionType::fromFullyQualifiedPHPDocString('string'), 'unusedParamName', Assertion::IS_OF_TYPE),
0
);
case 'assertnull':
return $method->createClosureForAssertion(
$code_base,
new Assertion(UnionType::fromFullyQualifiedPHPDocString('null'), 'unusedParamName', Assertion::IS_OF_TYPE),
0
);
case 'assertnotnull':
return $method->createClosureForAssertion(
$code_base,
new Assertion(UnionType::fromFullyQualifiedPHPDocString('null'), 'unusedParamName', Assertion::IS_NOT_OF_TYPE),
0
);
case 'assertsame':
// Sets the type of $actual to $expected
//
// This is equivalent to the side effects of the below doc comment.
// Note that the doc comment would make phan emit warnings about invalid classes, etc.
// TODO: Reuse the code for templates here
//
// (at)template T
// (at)param T $expected
// (at)param mixed $actual
// (at)phan-assert T $actual
return $method->createClosureForUnionTypeExtractorAndAssertionType(
/**
* @param list<Node|string|int|float> $args
*/
static function (CodeBase $code_base, Context $context, array $args): UnionType {
if (\count($args) < 2) {
return UnionType::empty();
}
return UnionTypeVisitor::unionTypeFromNode($code_base, $context, $args[0]);
},
Assertion::IS_OF_TYPE,
1
);
case 'assertinternaltype':
return $method->createClosureForUnionTypeExtractorAndAssertionType(
/**
* @param list<Node|string|int|float> $args
*/
function (CodeBase $code_base, Context $context, array $args): UnionType {
if (\count($args) < 2) {
return UnionType::empty();
}
$string = $args[0];
if ($string instanceof ast\Node) {
$string = (UnionTypeVisitor::unionTypeFromNode($code_base, $context, $string))->asSingleScalarValueOrNull();
}
if (!is_string($string)) {
return UnionType::empty();
}
$original_type = (UnionTypeVisitor::unionTypeFromNode($code_base, $context, $args[1]));
switch ($string) {
case 'numeric':
return UnionType::fromFullyQualifiedPHPDocString('int|float|string');
case 'integer':
case 'int':
return UnionType::fromFullyQualifiedPHPDocString('int');
case 'double':
case 'float':
case 'real':
return UnionType::fromFullyQualifiedPHPDocString('float');
case 'string':
return UnionType::fromFullyQualifiedPHPDocString('string');
case 'boolean':
case 'bool':
return UnionType::fromFullyQualifiedPHPDocString('bool');
case 'null':
return UnionType::fromFullyQualifiedPHPDocString('null');
case 'array':
$result = $original_type->arrayTypes();
if ($result->isEmpty()) {
return UnionType::fromFullyQualifiedPHPDocString('array');
}
return $result;
case 'object':
$result = $original_type->objectTypes();
if ($result->isEmpty()) {
return UnionType::fromFullyQualifiedPHPDocString('object');
}
return $result;
case 'resource':
return UnionType::fromFullyQualifiedPHPDocString('resource');
case 'scalar':
$result = $original_type->scalarTypes();
if ($result->isEmpty()) {
return UnionType::fromFullyQualifiedPHPDocString('int|string|float|bool');
}
return $result;
case 'callable':
$result = $original_type->callableTypes($code_base);
if ($result->isEmpty()) {
return UnionType::fromFullyQualifiedPHPDocString('callable');
}
return $result;
}
// Warn about possibly invalid assertion
// NOTE: This is only emitted for variables
$this->emitPluginIssue(
$code_base,
$context,
'PhanPluginPHPUnitAssertionInvalidInternalType',
'Unknown type {STRING_LITERAL} in call to assertInternalType',
[$string]
);
return UnionType::empty();
},
Assertion::IS_OF_TYPE,
1
);
case 'assertinstanceof':
// This is equivalent to the side effects of the below doc comment.
// Note that the doc comment would make phan emit warnings about invalid classes, etc.
// TODO: Reuse the code for class-string<T> here.
//
// (at)template T
// (at)param class-string<T> $expected
// (at)param mixed $actual
// (at)phan-assert T $actual
return $method->createClosureForUnionTypeExtractorAndAssertionType(
/**
* @param list<Node|string|int|float> $args
*/
static function (CodeBase $code_base, Context $context, array $args): UnionType {
if (\count($args) < 2) {
return UnionType::empty();
}
$string = (UnionTypeVisitor::unionTypeFromNode($code_base, $context, $args[0]))->asSingleScalarValueOrNull();
if (!is_string($string)) {
return UnionType::empty();
}
try {
return FullyQualifiedClassName::fromFullyQualifiedString($string)->asType()->asPHPDocUnionType();
} catch (\Exception) {
return UnionType::empty();
}
},
Assertion::IS_OF_TYPE,
1
);
}
return null;
}
}
// Every plugin needs to return an instance of itself at the
// end of the file in which it's defined.
return new PHPUnitAssertionPlugin();