2019-12-06 20:58:18 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\TypeReconciliation;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\NodeDataProvider;
|
|
|
|
use Psalm\Internal\Type\AssertionReconciler;
|
2020-07-22 01:40:35 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2019-12-06 20:58:18 +01:00
|
|
|
use Psalm\Type;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ReconcilerTest extends TestCase
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
/** @var FileAnalyzer */
|
|
|
|
protected $file_analyzer;
|
|
|
|
|
|
|
|
/** @var StatementsAnalyzer */
|
|
|
|
protected $statements_analyzer;
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
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,
|
2021-12-03 20:11:20 +01:00
|
|
|
new NodeDataProvider()
|
2019-12-06 20:58:18 +01:00
|
|
|
);
|
2020-03-14 19:48:45 +01:00
|
|
|
|
|
|
|
$this->addFile('newfile.php', '
|
|
|
|
<?php
|
2021-02-24 04:37:26 +01:00
|
|
|
class SomeClass {}
|
|
|
|
class SomeChildClass extends SomeClass {}
|
2020-03-14 19:48:45 +01:00
|
|
|
class A {}
|
2021-02-24 04:37:26 +01:00
|
|
|
class B {}
|
|
|
|
interface SomeInterface {}
|
2020-03-14 19:48:45 +01:00
|
|
|
');
|
|
|
|
$this->project_analyzer->getCodebase()->scanFiles();
|
2019-12-06 20:58:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestReconcilation
|
|
|
|
*/
|
2020-11-26 16:00:03 +01:00
|
|
|
public function testReconcilation(string $expected_type, string $assertion, string $original_type): void
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
$reconciled = AssertionReconciler::reconcile(
|
2020-11-26 16:00:03 +01:00
|
|
|
$assertion,
|
|
|
|
Type::parseString($original_type),
|
2019-12-06 20:58:18 +01:00
|
|
|
null,
|
|
|
|
$this->statements_analyzer,
|
|
|
|
false,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
2020-11-26 16:00:03 +01:00
|
|
|
$expected_type,
|
2019-12-06 20:58:18 +01:00
|
|
|
$reconciled->getId()
|
|
|
|
);
|
|
|
|
|
2020-02-22 17:28:24 +01:00
|
|
|
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getAtomicTypes());
|
2019-12-06 20:58:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestTypeIsContainedBy
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* @param string $container
|
|
|
|
*
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTypeIsContainedBy($input, $container): void
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
$this->assertTrue(
|
2020-07-22 01:40:35 +02:00
|
|
|
UnionTypeComparator::isContainedBy(
|
2019-12-06 20:58:18 +01:00
|
|
|
$this->project_analyzer->getCodebase(),
|
|
|
|
Type::parseString($input),
|
|
|
|
Type::parseString($container)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string,array{string,string,string}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerTestReconcilation(): array
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
return [
|
2021-02-24 04:37:26 +01:00
|
|
|
'notNullWithObject' => ['SomeClass', '!null', 'SomeClass'],
|
|
|
|
'notNullWithObjectPipeNull' => ['SomeClass', '!null', 'SomeClass|null'],
|
|
|
|
'notNullWithSomeClassPipeFalse' => ['SomeClass|false', '!null', 'SomeClass|false'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'notNullWithMixed' => ['mixed', '!null', 'mixed'],
|
|
|
|
|
2021-02-24 04:37:26 +01:00
|
|
|
'notEmptyWithSomeClass' => ['SomeClass', '!falsy', 'SomeClass'],
|
|
|
|
'notEmptyWithSomeClassPipeNull' => ['SomeClass', '!falsy', 'SomeClass|null'],
|
|
|
|
'notEmptyWithSomeClassPipeFalse' => ['SomeClass', '!falsy', 'SomeClass|false'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'notEmptyWithMixed' => ['non-empty-mixed', '!falsy', 'mixed'],
|
|
|
|
// @todo in the future this should also work
|
2021-02-24 04:37:26 +01:00
|
|
|
//'notEmptyWithSomeClassFalseTrue' => ['SomeClass|true', '!falsy', 'SomeClass|bool'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2021-02-24 04:37:26 +01:00
|
|
|
'nullWithSomeClassPipeNull' => ['null', 'null', 'SomeClass|null'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'nullWithMixed' => ['null', 'null', 'mixed'],
|
|
|
|
|
2021-10-19 20:58:19 +02:00
|
|
|
'falsyWithSomeClass' => ['empty', 'falsy', 'SomeClass'],
|
2021-02-24 04:37:26 +01:00
|
|
|
'falsyWithSomeClassPipeFalse' => ['false', 'falsy', 'SomeClass|false'],
|
|
|
|
'falsyWithSomeClassPipeBool' => ['false', 'falsy', 'SomeClass|bool'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'falsyWithMixed' => ['empty-mixed', 'falsy', 'mixed'],
|
|
|
|
'falsyWithBool' => ['false', 'falsy', 'bool'],
|
2021-01-26 05:41:42 +01:00
|
|
|
'falsyWithStringOrNull' => ['""|"0"|null', 'falsy', 'string|null'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'falsyWithScalarOrNull' => ['empty-scalar', 'falsy', 'scalar'],
|
|
|
|
|
2021-02-24 04:37:26 +01:00
|
|
|
'notSomeClassWithSomeClassPipeBool' => ['bool', '!SomeClass', 'SomeClass|bool'],
|
|
|
|
'notSomeClassWithSomeClassPipeNull' => ['null', '!SomeClass', 'SomeClass|null'],
|
|
|
|
'notSomeClassWithAPipeB' => ['B', '!A', 'A|B'],
|
2021-07-26 09:50:12 +02:00
|
|
|
'notDateTimeWithDateTimeInterface' => ['DateTimeImmutable', '!DateTime', 'DateTimeInterface'],
|
|
|
|
'notDateTimeImmutableWithDateTimeInterface' => ['DateTime', '!DateTimeImmutable', 'DateTimeInterface'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2021-02-24 04:37:26 +01:00
|
|
|
'myObjectWithSomeClassPipeBool' => ['SomeClass', 'SomeClass', 'SomeClass|bool'],
|
|
|
|
'myObjectWithAPipeB' => ['A', 'A', 'A|B'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
|
|
|
'array' => ['array<array-key, mixed>', 'array', 'array|null'],
|
|
|
|
|
|
|
|
'2dArray' => ['array<array-key, array<array-key, string>>', 'array', 'array<array<string>>|null'],
|
|
|
|
|
|
|
|
'numeric' => ['numeric-string', 'numeric', 'string'],
|
|
|
|
|
|
|
|
'nullableClassString' => ['null', 'falsy', '?class-string'],
|
|
|
|
'mixedOrNullNotFalsy' => ['non-empty-mixed', '!falsy', 'mixed|null'],
|
|
|
|
'mixedOrNullFalsy' => ['empty-mixed|null', 'falsy', 'mixed|null'],
|
2021-02-24 04:37:26 +01:00
|
|
|
'nullableClassStringFalsy' => ['null', 'falsy', 'class-string<SomeClass>|null'],
|
|
|
|
'nullableClassStringEqualsNull' => ['null', '=null', 'class-string<SomeClass>|null'],
|
|
|
|
'nullableClassStringTruthy' => ['class-string<SomeClass>', '!falsy', 'class-string<SomeClass>|null'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'iterableToArray' => ['array<int, int>', 'array', 'iterable<int, int>'],
|
|
|
|
'iterableToTraversable' => ['Traversable<int, int>', 'Traversable', 'iterable<int, int>'],
|
2020-01-02 14:37:54 +01:00
|
|
|
'callableToCallableArray' => ['callable-array{0: class-string|object, 1: string}', 'array', 'callable'],
|
2021-01-22 22:14:29 +01:00
|
|
|
'SmallKeyedArrayAndCallable' => ['array{test: string}', 'array{test: string}', 'callable'],
|
|
|
|
'BigKeyedArrayAndCallable' => ['array{foo: string, test: string, thing: string}', 'array{foo: string, test: string, thing: string}', 'callable'],
|
2020-04-11 23:50:24 +02:00
|
|
|
'callableOrArrayToCallableArray' => ['array<array-key, mixed>', 'array', 'callable|array'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'traversableToIntersection' => ['Countable&Traversable', 'Traversable', 'Countable'],
|
|
|
|
'iterableWithoutParamsToTraversableWithoutParams' => ['Traversable', '!array', 'iterable'],
|
|
|
|
'iterableWithParamsToTraversableWithParams' => ['Traversable<int, string>', '!array', 'iterable<int, string>'],
|
2020-11-27 15:10:55 +01:00
|
|
|
'iterableAndObject' => ['Traversable<int, string>', 'object', 'iterable<int, string>'],
|
2020-11-26 15:25:49 +01:00
|
|
|
'iterableAndNotObject' => ['array<int, string>', '!object', 'iterable<int, string>'],
|
2021-01-19 23:19:47 +01:00
|
|
|
'boolNotEmptyIsTrue' => ['true', '!empty', 'bool'],
|
2021-02-24 06:05:12 +01:00
|
|
|
'interfaceAssertionOnClassInterfaceUnion' => ['SomeInterface|SomeInterface&SomeClass', 'SomeInterface', 'SomeClass|SomeInterface'],
|
2021-10-11 14:33:21 +02:00
|
|
|
'stringToNumericStringWithInt' => ['numeric-string', '~int', 'string'],
|
|
|
|
'stringToNumericStringWithFloat' => ['numeric-string', '~float', 'string'],
|
2021-10-11 13:36:09 +02:00
|
|
|
'filterKeyedArrayWithIterable' => ['array{some: string}', 'iterable<string>', 'array{some: mixed}'],
|
2021-10-12 15:46:02 +02:00
|
|
|
'SimpleXMLElementNotAlwaysTruthy' => ['SimpleXMLElement', '!falsy', 'SimpleXMLElement'],
|
|
|
|
'SimpleXMLElementNotAlwaysTruthy2' => ['SimpleXMLElement', 'falsy', 'SimpleXMLElement'],
|
2021-11-12 13:49:00 +01:00
|
|
|
'SimpleXMLIteratorNotAlwaysTruthy' => ['SimpleXMLIterator', '!falsy', 'SimpleXMLIterator'],
|
|
|
|
'SimpleXMLIteratorNotAlwaysTruthy2' => ['SimpleXMLIterator', 'falsy', 'SimpleXMLIterator'],
|
2019-12-06 20:58:18 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string,array{string,string}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerTestTypeIsContainedBy(): array
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayContainsWithArrayOfStrings' => ['array<string>', 'array'],
|
|
|
|
'arrayContainsWithArrayOfExceptions' => ['array<Exception>', 'array'],
|
2020-03-14 19:48:45 +01:00
|
|
|
'arrayOfIterable' => ['array', 'iterable'],
|
2021-02-24 04:37:26 +01:00
|
|
|
'arrayOfIterableWithType' => ['array<SomeClass>', 'iterable<SomeClass>'],
|
|
|
|
'arrayOfIterableWithSubclass' => ['array<SomeChildClass>', 'iterable<SomeClass>'],
|
|
|
|
'arrayOfSubclassOfParent' => ['array<SomeChildClass>', 'array<SomeClass>'],
|
|
|
|
'subclassOfParent' => ['SomeChildClass', 'SomeClass'],
|
2019-12-06 20:58:18 +01:00
|
|
|
'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<empty, empty>',
|
|
|
|
'array{a?: string, b?: string}',
|
|
|
|
],
|
2020-09-08 05:29:46 +02:00
|
|
|
'literalNumericStringInt' => [
|
|
|
|
'"0"',
|
|
|
|
'numeric',
|
|
|
|
],
|
|
|
|
'literalNumericString' => [
|
|
|
|
'"10.03"',
|
|
|
|
'numeric',
|
|
|
|
],
|
2019-12-06 20:58:18 +01:00
|
|
|
];
|
|
|
|
}
|
2021-09-24 01:47:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider constantAssertions
|
|
|
|
*/
|
|
|
|
public function testReconciliationOfClassConstantInAssertions(string $assertion, string $expected_type): void
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'psalm-assert.php',
|
|
|
|
'
|
|
|
|
<?php
|
2021-09-25 18:56:44 +02:00
|
|
|
namespace ReconciliationTest;
|
2021-09-24 01:47:57 +02:00
|
|
|
class Foo
|
|
|
|
{
|
2021-09-26 13:07:30 +02:00
|
|
|
const PREFIX_BAR = \'bar\';
|
|
|
|
const PREFIX_BAZ = \'baz\';
|
|
|
|
const PREFIX_QOO = Foo::PREFIX_BAR;
|
2021-09-24 01:47:57 +02:00
|
|
|
}
|
|
|
|
'
|
|
|
|
);
|
|
|
|
$this->project_analyzer->getCodebase()->scanFiles();
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$reconciled = AssertionReconciler::reconcile(
|
2021-09-24 01:47:57 +02:00
|
|
|
$assertion,
|
|
|
|
new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString(''),
|
|
|
|
]),
|
|
|
|
null,
|
|
|
|
$this->statements_analyzer,
|
|
|
|
false,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected_type,
|
|
|
|
$reconciled->getId()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<non-empty-string,array{non-empty-string,string}>
|
|
|
|
*/
|
|
|
|
public function constantAssertions(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-09-26 13:07:30 +02:00
|
|
|
'constant-with-prefix' => [
|
|
|
|
'class-constant(ReconciliationTest\\Foo::PREFIX_*)',
|
|
|
|
'"bar"|"baz"',
|
|
|
|
],
|
|
|
|
'single-class-constant' => [
|
|
|
|
'class-constant(ReconciliationTest\\Foo::PREFIX_BAR)',
|
2021-09-24 01:47:57 +02:00
|
|
|
'"bar"',
|
|
|
|
],
|
2021-09-26 13:07:30 +02:00
|
|
|
'referencing-another-class-constant' => [
|
|
|
|
'class-constant(ReconciliationTest\\Foo::PREFIX_QOO)',
|
2021-09-24 01:47:57 +02:00
|
|
|
'"bar"',
|
|
|
|
],
|
2021-09-26 13:07:30 +02:00
|
|
|
'referencing-all-class-constants' => [
|
|
|
|
'class-constant(ReconciliationTest\\Foo::*)',
|
|
|
|
'"bar"|"baz"',
|
|
|
|
],
|
|
|
|
'referencing-some-class-constants-with-wildcard' => [
|
|
|
|
'class-constant(ReconciliationTest\\Foo::PREFIX_B*)',
|
2021-09-24 01:47:57 +02:00
|
|
|
'"bar"|"baz"',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2019-12-06 20:58:18 +01:00
|
|
|
}
|