2019-12-06 20:58:18 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-12-06 20:58:18 +01:00
|
|
|
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;
|
2022-01-20 23:33:06 +01:00
|
|
|
use Psalm\Storage\Assertion;
|
2022-09-18 17:02:09 +02:00
|
|
|
use Psalm\Storage\Assertion\Any;
|
2022-01-20 23:33:06 +01:00
|
|
|
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;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2019-12-06 20:58:18 +01:00
|
|
|
use Psalm\Type;
|
2022-01-20 23:33:06 +01:00
|
|
|
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;
|
2021-12-13 04:45:57 +01:00
|
|
|
use Psalm\Type\Atomic\TLiteralString;
|
2022-01-20 23:33:06 +01:00
|
|
|
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;
|
2021-12-13 16:28:14 +01:00
|
|
|
use Psalm\Type\Union;
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ReconcilerTest extends TestCase
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
protected FileAnalyzer $file_analyzer;
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-12-16 19:58:47 +01:00
|
|
|
protected StatementsAnalyzer $statements_analyzer;
|
2019-12-06 20:58:18 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2022-01-20 23:33:06 +01:00
|
|
|
public function testReconcilation(string $expected_type, Assertion $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
|
|
|
|
*/
|
2022-12-15 03:26:17 +01:00
|
|
|
public function testTypeIsContainedBy(string $input, string $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)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{string,Assertion,string}>
|
2019-12-06 20:58:18 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerTestReconcilation(): array
|
2019-12-06 20:58:18 +01:00
|
|
|
{
|
|
|
|
return [
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'notEmptyWithSomeClass' => ['SomeClass', new Truthy(), 'SomeClass'],
|
|
|
|
'notEmptyWithSomeClassPipeNull' => ['SomeClass', new Truthy(), 'SomeClass|null'],
|
|
|
|
'notEmptyWithSomeClassPipeFalse' => ['SomeClass', new Truthy(), 'SomeClass|false'],
|
|
|
|
'notEmptyWithMixed' => ['non-empty-mixed', new Truthy(), 'mixed'],
|
2019-12-06 20:58:18 +01:00
|
|
|
// @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
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'nullWithSomeClassPipeNull' => ['null', new IsType(new TNull()), 'SomeClass|null'],
|
|
|
|
'nullWithMixed' => ['null', new IsType(new TNull()), 'mixed'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2022-01-16 16:07:56 +01:00
|
|
|
'falsyWithStringOrNull' => ["''|'0'|null", new Falsy(), 'string|null'],
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'myObjectWithSomeClassPipeBool' => ['SomeClass', new IsType(new TNamedObject('SomeClass')), 'SomeClass|bool'],
|
|
|
|
'myObjectWithAPipeB' => ['A', new IsType(new TNamedObject('A')), 'A|B'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'array' => ['array<array-key, mixed>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'array|null'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'2dArray' => ['array<array-key, array<array-key, string>>', new IsType(new TArray([Type::getArrayKey(), Type::getMixed()])), 'array<array<string>>|null'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'numeric' => ['numeric-string', new IsType(new TNumeric()), 'string'],
|
2019-12-06 20:58:18 +01:00
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2022-11-12 02:14:21 +01:00
|
|
|
'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'],
|
2022-01-20 23:33:06 +01:00
|
|
|
'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'],
|
2022-11-12 02:14:21 +01:00
|
|
|
'filterKeyedArrayWithIterable' => ['array{some: string}',new IsType(new TIterable([Type::getMixed(), Type::getString()])), 'array{some: mixed}'],
|
2022-01-20 23:33:06 +01:00
|
|
|
'SimpleXMLElementNotAlwaysTruthy' => ['SimpleXMLElement', new Truthy(), 'SimpleXMLElement'],
|
|
|
|
'SimpleXMLElementNotAlwaysTruthy2' => ['SimpleXMLElement', new Falsy(), 'SimpleXMLElement'],
|
|
|
|
'SimpleXMLIteratorNotAlwaysTruthy' => ['SimpleXMLIterator', new Truthy(), 'SimpleXMLIterator'],
|
|
|
|
'SimpleXMLIteratorNotAlwaysTruthy2' => ['SimpleXMLIterator', new Falsy(), 'SimpleXMLIterator'],
|
2022-09-18 17:02:09 +02:00
|
|
|
'stringWithAny' => ['string', new Any(), 'string'],
|
2019-12-06 20:58:18 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{string,string}>
|
2019-12-06 20:58:18 +01:00
|
|
|
*/
|
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' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'array{0: array{a: string}, 1: array{c: string, e: string}}',
|
2019-12-06 20:58:18 +01:00
|
|
|
'array<int, array<string, string>>',
|
|
|
|
],
|
|
|
|
'objectLikeTypeWithPossiblyUndefinedToEmpty' => [
|
2021-10-13 19:37:47 +02:00
|
|
|
'array<never, never>',
|
2022-11-12 02:14:21 +01:00
|
|
|
'array{a?: string, b?: string}',
|
2019-12-06 20:58:18 +01:00
|
|
|
],
|
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
|
|
|
|
*/
|
2022-01-20 23:33:06 +01:00
|
|
|
public function testReconciliationOfClassConstantInAssertions(Assertion $assertion, string $expected_type): void
|
2021-09-24 01:47:57 +02:00
|
|
|
{
|
|
|
|
$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,
|
2021-12-13 16:28:14 +01:00
|
|
|
new Union([
|
2021-12-13 04:45:57 +01:00
|
|
|
new TLiteralString(''),
|
2021-09-24 01:47:57 +02:00
|
|
|
]),
|
|
|
|
null,
|
|
|
|
$this->statements_analyzer,
|
|
|
|
false,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected_type,
|
|
|
|
$reconciled->getId()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<non-empty-string,array{Assertion,string}>
|
2021-09-24 01:47:57 +02:00
|
|
|
*/
|
|
|
|
public function constantAssertions(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-09-26 13:07:30 +02:00
|
|
|
'constant-with-prefix' => [
|
2022-01-20 23:33:06 +01:00
|
|
|
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_*')),
|
2022-01-16 16:07:56 +01:00
|
|
|
"'bar'|'baz'",
|
2021-09-26 13:07:30 +02:00
|
|
|
],
|
|
|
|
'single-class-constant' => [
|
2022-01-20 23:33:06 +01:00
|
|
|
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_BAR')),
|
2022-01-16 16:07:56 +01:00
|
|
|
"'bar'",
|
2021-09-24 01:47:57 +02:00
|
|
|
],
|
2021-09-26 13:07:30 +02:00
|
|
|
'referencing-another-class-constant' => [
|
2022-01-20 23:33:06 +01:00
|
|
|
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_QOO')),
|
2022-01-16 16:07:56 +01:00
|
|
|
"'bar'",
|
2021-09-24 01:47:57 +02:00
|
|
|
],
|
2021-09-26 13:07:30 +02:00
|
|
|
'referencing-all-class-constants' => [
|
2022-01-20 23:33:06 +01:00
|
|
|
new IsType(new TClassConstant('ReconciliationTest\\Foo', '*')),
|
2022-01-16 16:07:56 +01:00
|
|
|
"'bar'|'baz'",
|
2021-09-26 13:07:30 +02:00
|
|
|
],
|
|
|
|
'referencing-some-class-constants-with-wildcard' => [
|
2022-01-20 23:33:06 +01:00
|
|
|
new IsType(new TClassConstant('ReconciliationTest\\Foo', 'PREFIX_B*')),
|
2022-01-16 16:07:56 +01:00
|
|
|
"'bar'|'baz'",
|
2021-09-24 01:47:57 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2019-12-06 20:58:18 +01:00
|
|
|
}
|