forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReconcilerTest.php
312 lines (284 loc) · 13.9 KB
/
ReconcilerTest.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace Psalm\Tests\TypeReconciliation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\Type\AssertionReconciler;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Storage\Assertion;
use Psalm\Storage\Assertion\Falsy;
use Psalm\Storage\Assertion\IsIdentical;
use Psalm\Storage\Assertion\IsLooselyEqual;
use Psalm\Storage\Assertion\IsNotIdentical;
use Psalm\Storage\Assertion\IsNotType;
use Psalm\Storage\Assertion\IsType;
use Psalm\Storage\Assertion\NonEmpty;
use Psalm\Storage\Assertion\Truthy;
use Psalm\Tests\TestCase;
use Psalm\Tests\TestConfig;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TClassConstant;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIterable;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Union;
class ReconcilerTest extends TestCase
{
/** @var FileAnalyzer */
protected $file_analyzer;
/** @var StatementsAnalyzer */
protected $statements_analyzer;
public function setUp(): void
{
parent::setUp();
$this->file_analyzer = new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
$this->file_analyzer->context = new Context();
$this->statements_analyzer = new StatementsAnalyzer(
$this->file_analyzer,
new NodeDataProvider()
);
$this->addFile('newfile.php', '
<?php
class SomeClass {}
class SomeChildClass extends SomeClass {}
class A {}
class B {}
interface SomeInterface {}
');
$this->project_analyzer->getCodebase()->scanFiles();
}
/**
* @dataProvider providerTestReconcilation
*/
public function testReconcilation(string $expected_type, Assertion $assertion, string $original_type): void
{
$reconciled = AssertionReconciler::reconcile(
$assertion,
Type::parseString($original_type),
null,
$this->statements_analyzer,
false,
[]
);
$this->assertSame(
$expected_type,
$reconciled->getId()
);
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getAtomicTypes());
}
/**
* @dataProvider providerTestTypeIsContainedBy
*
* @param string $input
* @param string $container
*
*/
public function testTypeIsContainedBy($input, $container): void
{
$this->assertTrue(
UnionTypeComparator::isContainedBy(
$this->project_analyzer->getCodebase(),
Type::parseString($input),
Type::parseString($container)
)
);
}
/**
* @return array<string,array{string,Assertion,string}>
*/
public function providerTestReconcilation(): array
{
return [
'notNullWithObject' => ['SomeClass', new IsNotType(new TNull()), 'SomeClass'],
'notNullWithObjectPipeNull' => ['SomeClass', new IsNotType(new TNull()), 'SomeClass|null'],
'notNullWithSomeClassPipeFalse' => ['SomeClass|false', new IsNotType(new TNull()), 'SomeClass|false'],
'notNullWithMixed' => ['mixed', new IsNotType(new TNull()), 'mixed'],
'notEmptyWithSomeClass' => ['SomeClass', new Truthy(), 'SomeClass'],
'notEmptyWithSomeClassPipeNull' => ['SomeClass', new Truthy(), 'SomeClass|null'],
'notEmptyWithSomeClassPipeFalse' => ['SomeClass', new Truthy(), 'SomeClass|false'],
'notEmptyWithMixed' => ['non-empty-mixed', new Truthy(), 'mixed'],
// @todo in the future this should also work
//'notEmptyWithSomeClassFalseTrue' => ['SomeClass|true', '!falsy', 'SomeClass|bool'],
'nullWithSomeClassPipeNull' => ['null', new IsType(new TNull()), 'SomeClass|null'],
'nullWithMixed' => ['null', new IsType(new TNull()), 'mixed'],
'falsyWithSomeClass' => ['never', new Falsy(), 'SomeClass'],
'falsyWithSomeClassPipeFalse' => ['false', new Falsy(), 'SomeClass|false'],
'falsyWithSomeClassPipeBool' => ['false', new Falsy(), 'SomeClass|bool'],
'falsyWithMixed' => ['empty-mixed', new Falsy(), 'mixed'],
'falsyWithBool' => ['false', new Falsy(), 'bool'],
'falsyWithStringOrNull' => ["''|'0'|null", new Falsy(), 'string|null'],
'falsyWithScalarOrNull' => ['empty-scalar', new Falsy(), 'scalar'],
'trueWithBool' => ['true', new IsType(new TTrue()), 'bool'],
'falseWithBool' => ['false', new IsType(new TFalse()), 'bool'],
'notTrueWithBool' => ['false', new IsNotIdentical(new TTrue()), 'bool'],
'notFalseWithBool' => ['true', new IsNotIdentical(new TFalse()), 'bool'],
'notSomeClassWithSomeClassPipeBool' => ['bool', new IsNotType(new TNamedObject('SomeClass')), 'SomeClass|bool'],
'notSomeClassWithSomeClassPipeNull' => ['null', new IsNotType(new TNamedObject('SomeClass')), 'SomeClass|null'],
'notSomeClassWithAPipeB' => ['B', new IsNotType(new TNamedObject('A')), 'A|B'],
'notDateTimeWithDateTimeInterface' => ['DateTimeImmutable', new IsNotType(new TNamedObject('DateTime')), 'DateTimeInterface'],
'notDateTimeImmutableWithDateTimeInterface' => ['DateTime', new IsNotType(new TNamedObject('DateTimeImmutable')), 'DateTimeInterface'],
'myObjectWithSomeClassPipeBool' => ['SomeClass', new IsType(new TNamedObject('SomeClass')), 'SomeClass|bool'],
'myObjectWithAPipeB' => ['A', new IsType(new TNamedObject('A')), 'A|B'],
'array' => ['array<array-key, mixed>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'array|null'],
'2dArray' => ['array<array-key, array<array-key, string>>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'array<array<string>>|null'],
'numeric' => ['numeric-string', new IsType(new TNumeric()), 'string'],
'nullableClassString' => ['null', new Falsy(), '?class-string'],
'mixedOrNullNotFalsy' => ['non-empty-mixed', new Truthy(), 'mixed|null'],
'mixedOrNullFalsy' => ['empty-mixed|null', new Falsy(), 'mixed|null'],
'nullableClassStringFalsy' => ['null', new Falsy(), 'class-string<SomeClass>|null'],
'nullableClassStringEqualsNull' => ['null', new IsIdentical(new TNull()), 'class-string<SomeClass>|null'],
'nullableClassStringTruthy' => ['class-string<SomeClass>', new Truthy(), 'class-string<SomeClass>|null'],
'iterableToArray' => ['array<int, int>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'iterable<int, int>'],
'iterableToTraversable' => ['Traversable<int, int>', new IsType(new TNamedObject('Traversable')), 'iterable<int, int>'],
'callableToCallableArray' => ['callable-array{0: class-string|object, 1: string}', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'callable'],
'SmallKeyedArrayAndCallable' => ['array{test: string}', new IsType(new TKeyedArray(['test' => Type::getString()])), 'callable'],
'BigKeyedArrayAndCallable' => ['array{foo: string, test: string, thing: string}', new IsType(new TKeyedArray(['foo' => Type::getString(), 'test' => Type::getString(), 'thing' => Type::getString()])), 'callable'],
'callableOrArrayToCallableArray' => ['array<array-key, mixed>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'callable|array'],
'traversableToIntersection' => ['Countable&Traversable', new IsType(new TNamedObject('Traversable')), 'Countable'],
'iterableWithoutParamsToTraversableWithoutParams' => ['Traversable', new IsNotType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'iterable'],
'iterableWithParamsToTraversableWithParams' => ['Traversable<int, string>', new IsNotType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'iterable<int, string>'],
'iterableAndObject' => ['Traversable<int, string>', new IsType(new TObject()), 'iterable<int, string>'],
'iterableAndNotObject' => ['array<int, string>', new IsNotType(new TObject()), 'iterable<int, string>'],
'boolNotEmptyIsTrue' => ['true', new NonEmpty(), 'bool'],
'interfaceAssertionOnClassInterfaceUnion' => ['SomeInterface|SomeInterface&SomeClass', new IsType(new TNamedObject('SomeInterface')), 'SomeClass|SomeInterface'],
'classAssertionOnClassInterfaceUnion' => ['SomeClass|SomeClass&SomeInterface', new IsType(new TNamedObject('SomeClass')), 'SomeClass|SomeInterface'],
'stringToNumericStringWithInt' => ['numeric-string', new IsLooselyEqual(new TInt()), 'string'],
'stringToNumericStringWithFloat' => ['numeric-string', new IsLooselyEqual(new TFloat()), 'string'],
'filterKeyedArrayWithIterable' => ['array{some: string}',new IsType(new TIterable([Type::getMixed(), Type::getString()])), 'array{some: mixed}'],
'SimpleXMLElementNotAlwaysTruthy' => ['SimpleXMLElement', new Truthy(), 'SimpleXMLElement'],
'SimpleXMLElementNotAlwaysTruthy2' => ['SimpleXMLElement', new Falsy(), 'SimpleXMLElement'],
'SimpleXMLIteratorNotAlwaysTruthy' => ['SimpleXMLIterator', new Truthy(), 'SimpleXMLIterator'],
'SimpleXMLIteratorNotAlwaysTruthy2' => ['SimpleXMLIterator', new Falsy(), 'SimpleXMLIterator'],
];
}
/**
* @return array<string,array{string,string}>
*/
public function providerTestTypeIsContainedBy(): array
{
return [
'arrayContainsWithArrayOfStrings' => ['array<string>', 'array'],
'arrayContainsWithArrayOfExceptions' => ['array<Exception>', 'array'],
'arrayOfIterable' => ['array', 'iterable'],
'arrayOfIterableWithType' => ['array<SomeClass>', 'iterable<SomeClass>'],
'arrayOfIterableWithSubclass' => ['array<SomeChildClass>', 'iterable<SomeClass>'],
'arrayOfSubclassOfParent' => ['array<SomeChildClass>', 'array<SomeClass>'],
'subclassOfParent' => ['SomeChildClass', 'SomeClass'],
'unionContainsWithstring' => ['string', 'string|false'],
'unionContainsWithFalse' => ['false', 'string|false'],
'objectLikeTypeWithPossiblyUndefinedToGeneric' => [
'array{0: array{a: string}, 1: array{c: string, e: string}}',
'array<int, array<string, string>>',
],
'objectLikeTypeWithPossiblyUndefinedToEmpty' => [
'array<never, never>',
'array{a?: string, b?: string}',
],
'literalNumericStringInt' => [
'"0"',
'numeric',
],
'literalNumericString' => [
'"10.03"',
'numeric',
],
];
}
/**
* @dataProvider constantAssertions
*/
public function testReconciliationOfClassConstantInAssertions(Assertion $assertion, string $expected_type): void
{
$this->addFile(
'psalm-assert.php',
'
<?php
namespace ReconciliationTest;
class Foo
{
const PREFIX_BAR = \'bar\';
const PREFIX_BAZ = \'baz\';
const PREFIX_QOO = Foo::PREFIX_BAR;
}
'
);
$this->project_analyzer->getCodebase()->scanFiles();
$reconciled = AssertionReconciler::reconcile(
$assertion,
new Union([
new TLiteralString(''),
]),
null,
$this->statements_analyzer,
false,
[]
);
$this->assertSame(
$expected_type,
$reconciled->getId()
);
}
/**
* @return array<non-empty-string,array{Assertion,string}>
*/
public function constantAssertions(): array
{
return [
'constant-with-prefix' => [
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_*')),
"'bar'|'baz'",
],
'single-class-constant' => [
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_BAR')),
"'bar'",
],
'referencing-another-class-constant' => [
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_QOO')),
"'bar'",
],
'referencing-all-class-constants' => [
new IsType(new TClassConstant('ReconciliationTest\\Foo', '*')),
"'bar'|'baz'",
],
'referencing-some-class-constants-with-wildcard' => [
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_B*')),
"'bar'|'baz'",
],
];
}
/**
* @test
*/
public function arrayReferencesAreHandled(): void
{
$this->addFile(
'somefile.php',
<<< 'EOT'
<?php
$a = 'a';
$b = false;
$doesNotMatter = ['a' => ['id' => 1]];
$reference = &$doesNotMatter[$a];
$result = ($a === 'not-a' && ($b || false));
EOT
);
TestConfig::getInstance()->throw_exception = false;
$codebase = $this->project_analyzer->getCodebase();
$codebase->scanner->addFileToDeepScan('somefile.php');
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
$codebase->scanFiles();
$this->file_analyzer->analyze($this->file_analyzer->context);
// just asserting this test reaches the end
self::assertTrue(true);
}
}