forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodebaseTest.php
256 lines (224 loc) · 9.03 KB
/
CodebaseTest.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace Psalm\Tests;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Exception\UnpopulatedClasslikeException;
use Psalm\Issue\InvalidReturnStatement;
use Psalm\Issue\InvalidReturnType;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\BeforeAddIssueInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Plugin\EventHandler\Event\BeforeAddIssueEvent;
use Psalm\PluginRegistrationSocket;
use Psalm\Tests\Internal\Provider\ClassLikeStorageInstanceCacheProvider;
use Psalm\Type;
use function array_map;
use function array_values;
use function get_class;
class CodebaseTest extends TestCase
{
/** @var Codebase */
private $codebase;
public function setUp(): void
{
parent::setUp();
$this->codebase = $this->project_analyzer->getCodebase();
}
/**
* @test
* @dataProvider typeContainments
*
*/
public function isTypeContainedByType(string $input, string $container, bool $expected): void
{
$input = Type::parseString($input);
$container = Type::parseString($container);
$this->assertSame(
$expected,
$this->codebase->isTypeContainedByType($input, $container),
'Expected ' . $input->getId() . ($expected ? ' ' : ' not ')
. 'to be contained in ' . $container->getId()
);
}
/** @return iterable<int,array{string,string,bool}> */
public function typeContainments(): iterable
{
yield ['int', 'int|string', true];
yield ['int|string', 'int', false];
// This fails with 'could not get class storage' :(
// yield ['RuntimeException', 'Exception', true];
// yield ['Exception', 'RuntimeException', false];
}
/**
* @test
* @dataProvider typeIntersections
*
*/
public function canTypeBeContainedByType(string $input, string $container, bool $expected): void
{
$input = Type::parseString($input);
$container = Type::parseString($container);
$this->assertSame(
$expected,
$this->codebase->canTypeBeContainedByType($input, $container),
'Expected ' . $input->getId() . ($expected ? ' ' : ' not ')
. 'to be contained in ' . $container->getId()
);
}
/** @return iterable<int,array{string,string,bool}> */
public function typeIntersections(): iterable
{
yield ['int', 'int|string', true];
yield ['int|string', 'int', true];
yield ['int|string', 'string|float', true];
yield ['int', 'string', false];
yield ['int|string', 'array|float', false];
}
/**
* @test
* @dataProvider iterableParams
*
* @param array{string,string} $expected
*
*/
public function getKeyValueParamsForTraversableObject(string $input, array $expected): void
{
[$input] = array_values(Type::parseString($input)->getAtomicTypes());
$expected_key_type = Type::parseString($expected[0]);
$expected_value_type = Type::parseString($expected[1]);
$actual = $this->codebase->getKeyValueParamsForTraversableObject($input);
$this->assertTrue(
$expected_key_type->equals($actual[0]),
'Expected ' . $input->getId() . ' to have ' . $expected_key_type
. ' but got ' . $actual[0]->getId()
);
$this->assertTrue(
$expected_value_type->equals($actual[1]),
'Expected ' . $input->getId() . ' to have ' . $expected_value_type
. ' but got ' . $actual[1]->getId()
);
}
/** @return iterable<int,array{string,array{string,string}}> */
public function iterableParams(): iterable
{
yield ['iterable<int,string>', ['int', 'string']];
yield ['iterable<int|string,bool|float>', ['int|string', 'bool|float']];
}
/**
* @test
*
*/
public function customMetadataIsPersisted(): void
{
$this->addFile(
'somefile.php',
'<?php
namespace Psalm\CurrentTest;
abstract class A {}
interface I {}
class C extends A implements I
{
/** @var string */
private $prop = "";
/** @return void */
public function m(int $_i = 1) {}
}
'
);
$hook = new class implements AfterClassLikeVisitInterface {
/**
* @return void
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint
*/
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
{
$stmt = $event->getStmt();
$storage = $event->getStorage();
$codebase = $event->getCodebase();
if ($storage->name === 'Psalm\\CurrentTest\\C' && $stmt instanceof Class_) {
$storage->custom_metadata['fqcn'] = (string)($stmt->getAttribute('namespacedName') ?? $stmt->name);
$storage->custom_metadata['extends'] = $stmt->extends instanceof Name
? (string)$stmt->extends->getAttribute('resolvedName')
: '';
$storage->custom_metadata['implements'] = array_map(
fn(Name $aspect): string => (string)$aspect->getAttribute('resolvedName'),
$stmt->implements
);
$storage->custom_metadata['a'] = 'b';
$storage->methods['m']->custom_metadata['c'] = 'd';
$storage->properties['prop']->custom_metadata['e'] = 'f';
$storage->methods['m']->params[0]->custom_metadata['g'] = 'h';
$codebase->file_storage_provider->get('somefile.php')->custom_metadata['i'] = 'j';
}
}
};
(new PluginRegistrationSocket($this->codebase->config, $this->codebase))
->registerHooksFromClass(get_class($hook));
$this->codebase->classlike_storage_provider->cache = new ClassLikeStorageInstanceCacheProvider;
$this->analyzeFile('somefile.php', new Context);
$fixtureNamespace = 'Psalm\\CurrentTest\\';
$this->codebase->classlike_storage_provider->remove($fixtureNamespace . 'C');
$this->codebase->exhumeClassLikeStorage($fixtureNamespace . 'C', 'somefile.php');
$class_storage = $this->codebase->classlike_storage_provider->get($fixtureNamespace . 'C');
$file_storage = $this->codebase->file_storage_provider->get('somefile.php');
self::assertSame($fixtureNamespace . 'C', $class_storage->custom_metadata['fqcn']);
self::assertSame($fixtureNamespace . 'A', $class_storage->custom_metadata['extends']);
self::assertSame([$fixtureNamespace . 'I'], $class_storage->custom_metadata['implements']);
self::assertSame('b', $class_storage->custom_metadata['a']);
self::assertSame('d', $class_storage->methods['m']->custom_metadata['c']);
self::assertSame('f', $class_storage->properties['prop']->custom_metadata['e']);
self::assertSame('h', $class_storage->methods['m']->params[0]->custom_metadata['g']);
self::assertSame('j', $file_storage->custom_metadata['i']);
}
/**
* @test
*
*/
public function classExtendsRejectsUnpopulatedClasslikes(): void
{
$this->codebase->classlike_storage_provider->create('A');
$this->codebase->classlike_storage_provider->create('B');
$this->expectException(UnpopulatedClasslikeException::class);
$this->codebase->classExtends('A', 'B');
}
/**
* @test
*/
public function addingCodeIssueIsIntercepted(): void
{
$this->addFile(
'somefile.php',
'<?php
namespace Psalm\CurrentTest;
function invalidReturnType(int $value): string
{
return $value;
}
echo invalidReturnType(123);
'
);
$eventHandler = new class implements BeforeAddIssueInterface
{
public static function beforeAddIssue(BeforeAddIssueEvent $event): ?bool
{
$issue = $event->getIssue();
if ($issue->code_location->file_path !== 'somefile.php') {
return null;
}
if ($issue instanceof InvalidReturnStatement && $event->isFixable() === false) {
return false;
} elseif ($issue instanceof InvalidReturnType && $event->isFixable() === true) {
return false;
}
return null;
}
};
(new PluginRegistrationSocket($this->codebase->config, $this->codebase))
->registerHooksFromClass(get_class($eventHandler));
$this->analyzeFile('somefile.php', new Context);
self::assertSame(0, IssueBuffer::getErrorCount());
}
}