1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/TypeCombinationTest.php

646 lines
18 KiB
PHP
Raw Normal View History

2016-06-16 02:16:40 +02:00
<?php
2016-07-26 00:37:44 +02:00
namespace Psalm\Tests;
2016-06-16 02:16:40 +02:00
2019-07-05 22:24:00 +02:00
use function array_values;
use Psalm\Internal\Type\TypeCombination;
2019-03-23 19:27:54 +01:00
use Psalm\Type;
2016-06-16 02:16:40 +02:00
class TypeCombinationTest extends TestCase
2016-06-16 02:16:40 +02:00
{
2018-11-06 03:57:36 +01:00
use Traits\ValidCodeAnalysisTestTrait;
2017-01-13 20:07:23 +01:00
/**
* @dataProvider providerTestValidTypeCombination
2017-05-27 02:16:18 +02:00
*
* @param string $expected
2020-01-30 05:55:27 +01:00
* @param non-empty-list<string> $types
2017-05-27 02:16:18 +02:00
*
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testValidTypeCombination($expected, $types)
2016-06-16 02:16:40 +02:00
{
2019-10-09 00:44:46 +02:00
$converted_types = [];
foreach ($types as $type) {
$converted_type = self::getAtomic($type);
2020-03-15 04:54:42 +01:00
$converted_type->from_docblock = true;
2019-10-09 00:44:46 +02:00
$converted_types[] = $converted_type;
}
2017-05-27 02:05:57 +02:00
$this->assertSame(
$expected,
2020-04-09 15:27:14 +02:00
TypeCombination::combineTypes($converted_types)->getId()
);
2016-06-16 02:16:40 +02:00
}
2017-01-13 20:07:23 +01:00
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
{
return [
'multipleValuedArray' => [
'<?php
class A {}
class B {}
$var = [];
$var[] = new A();
2017-05-27 02:05:57 +02:00
$var[] = new B();',
],
'preventLiteralAndClassString' => [
'<?php
/**
* @param "array"|class-string $type_name
*/
function foo(string $type_name) : bool {
return $type_name === "array";
}',
],
];
}
/**
2020-01-30 05:55:27 +01:00
* @return array<string,array{string,non-empty-list<string>}>
*/
public function providerTestValidTypeCombination()
{
return [
'intOrString' => [
'int|string',
[
'int',
2017-05-27 02:05:57 +02:00
'string',
],
],
'mixedOrNull' => [
'mixed|null',
[
'mixed',
'null',
],
],
2018-12-20 02:01:27 +01:00
'mixedOrEmpty' => [
'mixed',
[
'empty',
'mixed',
],
],
'mixedOrObject' => [
'mixed|object',
[
'mixed',
'object',
],
],
'mixedOrEmptyArray' => [
'array<empty, empty>|mixed',
[
'mixed',
'array<empty, empty>',
],
],
2017-12-09 21:51:38 +01:00
'falseTrueToBool' => [
'bool',
[
'false',
'true',
],
],
'trueFalseToBool' => [
'bool',
[
'true',
'false',
],
],
'trueBoolToBool' => [
'bool',
[
'true',
'bool',
],
],
'boolTrueToBool' => [
'bool',
[
'bool',
'true',
],
],
'intOrTrueOrFalseToBool' => [
'bool|int',
2017-12-09 21:51:38 +01:00
[
'int',
'false',
'true',
],
],
'intOrBoolOrTrueToBool' => [
'bool|int',
2017-12-09 21:51:38 +01:00
[
'int',
'bool',
'true',
],
],
'intOrTrueOrBoolToBool' => [
'bool|int',
2017-12-09 21:51:38 +01:00
[
'int',
'true',
'bool',
],
],
'arrayOfIntOrString' => [
'array<array-key, int|string>',
[
'array<int>',
2017-05-27 02:05:57 +02:00
'array<string>',
],
],
'arrayOfIntOrAlsoString' => [
'array<array-key, int>|string',
[
'array<int>',
2017-05-27 02:05:57 +02:00
'string',
],
],
'emptyArrays' => [
'array<empty, empty>',
[
'array<empty,empty>',
2017-05-27 02:05:57 +02:00
'array<empty,empty>',
],
],
'arrayStringOrEmptyArray' => [
'array<array-key, string>',
[
'array<empty>',
2017-05-27 02:05:57 +02:00
'array<string>',
],
],
'arrayMixedOrString' => [
'array<array-key, mixed|string>',
[
'array<mixed>',
2017-05-27 02:05:57 +02:00
'array<string>',
],
],
'arrayMixedOrStringKeys' => [
2020-02-22 17:28:24 +01:00
'array<array-key, string>',
[
'array<int|string,string>',
2017-05-27 02:05:57 +02:00
'array<mixed,string>',
],
],
'arrayMixedOrEmpty' => [
'array<array-key, mixed>',
[
'array<empty>',
2017-05-27 02:05:57 +02:00
'array<mixed>',
],
],
'arrayBigCombination' => [
'array<array-key, float|int|string>',
[
'array<int|float>',
2017-05-27 02:05:57 +02:00
'array<string>',
],
],
'arrayTraversableToIterable' => [
'iterable<array-key|mixed, mixed>',
[
'array',
'Traversable',
],
],
'arrayIterableToIterable' => [
'iterable<mixed, mixed>',
[
'array',
'iterable',
],
],
'iterableArrayToIterable' => [
'iterable<mixed, mixed>',
[
'iterable',
'array',
],
],
'traversableIterableToIterable' => [
'iterable<mixed, mixed>',
[
'Traversable',
'iterable',
],
],
'iterableTraversableToIterable' => [
'iterable<mixed, mixed>',
[
'iterable',
'Traversable',
],
],
'arrayTraversableToIterableWithParams' => [
'iterable<int, bool|string>',
[
'array<int, string>',
'Traversable<int, bool>',
],
],
'arrayIterableToIterableWithParams' => [
'iterable<int, bool|string>',
[
'array<int, string>',
'iterable<int, bool>',
],
],
'iterableArrayToIterableWithParams' => [
'iterable<int, bool|string>',
[
'iterable<int, string>',
'array<int, bool>',
],
],
'traversableIterableToIterableWithParams' => [
'iterable<int, bool|string>',
[
'Traversable<int, string>',
'iterable<int, bool>',
],
],
'iterableTraversableToIterableWithParams' => [
'iterable<int, bool|string>',
[
'iterable<int, string>',
'Traversable<int, bool>',
],
],
'arrayObjectAndParamsWithEmptyArray' => [
'ArrayObject<int, string>|array<empty, empty>',
[
'ArrayObject<int, string>',
'array<empty, empty>',
],
],
'emptyArrayWithArrayObjectAndParams' => [
'ArrayObject<int, string>|array<empty, empty>',
[
'array<empty, empty>',
'ArrayObject<int, string>',
2017-05-27 02:05:57 +02:00
],
],
'falseDestruction' => [
'bool',
[
'false',
2017-05-27 02:05:57 +02:00
'bool',
],
],
'onlyFalse' => [
'false',
[
2017-05-27 02:05:57 +02:00
'false',
],
],
'onlyTrue' => [
'true',
[
'true',
],
],
'falseFalseDestruction' => [
'false',
[
'false',
2017-05-27 02:05:57 +02:00
'false',
],
],
'aAndAOfB' => [
'A|A<B>',
[
'A',
2017-05-27 02:05:57 +02:00
'A<B>',
],
],
'combineObjectType1' => [
2019-06-16 15:42:34 +02:00
'array{a?: int, b?: string}',
[
2019-06-16 15:42:34 +02:00
'array{a: int}',
'array{b: string}',
2017-05-27 02:05:57 +02:00
],
],
'combineObjectType2' => [
2019-06-16 15:42:34 +02:00
'array{a: int|string, b?: string}',
[
2019-06-16 15:42:34 +02:00
'array{a: int}',
'array{a: string,b: string}',
2017-05-27 02:05:57 +02:00
],
],
'combineObjectTypeWithIntKeyedArray' => [
'array<int|string, int|string>',
[
2019-06-16 15:42:34 +02:00
'array{a: int}',
'array<int, string>',
],
],
'combineNestedObjectTypeWithObjectLikeIntKeyedArray' => [
'array{a: array<int|string, int|string>}',
[
2019-06-16 15:42:34 +02:00
'array{a: array{a: int}}',
'array{a: array<int, string>}',
],
],
'combineIntKeyedObjectTypeWithNestedIntKeyedArray' => [
'array<int, array<int|string, int|string>>',
[
'array<int, array{a:int}>',
'array<int, array<int, string>>',
],
],
'combineNestedObjectTypeWithNestedIntKeyedArray' => [
'array<int|string, array<int|string, int|string>>',
[
2019-06-16 15:42:34 +02:00
'array{a: array{a: int}}',
'array<int, array<int, string>>',
],
],
'combinePossiblyUndefinedKeys' => [
2019-06-16 15:42:34 +02:00
'array{a: bool, b?: mixed, d?: mixed}',
[
2019-06-16 15:42:34 +02:00
'array{a: false, b: mixed}',
'array{a: true, d: mixed}',
'array{a: true, d: mixed}',
],
],
'combinePossiblyUndefinedKeysAndString' => [
2019-06-16 15:42:34 +02:00
'array{a: string, b?: int}|string',
[
2019-06-16 15:42:34 +02:00
'array{a: string, b?: int}',
'string',
],
],
'combineMixedArrayWithObjectLike' => [
2020-04-11 23:50:24 +02:00
'array<array-key, mixed>',
[
'array{a: int}',
'array',
],
],
'traversableAorB' => [
'Traversable<mixed, A|B>',
[
'Traversable<A>',
'Traversable<B>',
],
],
'iterableAorB' => [
'iterable<mixed, A|B>',
[
'iterable<A>',
'iterable<B>',
],
],
'FooAorB' => [
'Foo<A>|Foo<B>',
[
'Foo<A>',
'Foo<B>',
],
],
'traversableOfMixed' => [
'Traversable<mixed, mixed>',
[
'Traversable',
'Traversable<mixed, mixed>',
],
],
'traversableAndIterator' => [
'Traversable&Iterator',
[
'Traversable&Iterator',
'Traversable&Iterator',
],
],
'traversableOfMixedAndIterator' => [
'Traversable<mixed, mixed>&Iterator',
[
'Traversable<mixed, mixed>&Iterator',
'Traversable<mixed, mixed>&Iterator',
],
],
'objectLikePlusArrayEqualsArray' => [
2020-04-09 15:27:14 +02:00
'array<string, int(1)|int(2)|int(3)>',
[
'array<"a"|"b"|"c", 1|2|3>',
2019-06-16 15:42:34 +02:00
'array{a: 1|2, b: 2|3, c: 1|3}',
],
],
'combineClosures' => [
'Closure(A):void|Closure(B):void',
[
'Closure(A):void',
'Closure(B):void',
],
],
'combineClassStringWithString' => [
'string',
[
'class-string',
'string',
],
],
'combineClassStringWithFalse' => [
'class-string|false',
[
'class-string',
'false',
],
],
'combineRefinedClassStringWithString' => [
'string',
[
'class-string<Exception>',
'string',
],
],
'combineRefinedClassStrings' => [
'class-string<Exception>|class-string<Iterator>',
[
'class-string<Exception>',
'class-string<Iterator>',
],
],
'combineClassStringsWithLiteral' => [
'class-string',
[
'class-string',
'Exception::class',
],
],
2020-01-02 22:59:12 +01:00
'combineCallableAndCallableString' => [
'callable',
[
'callable',
'callable-string',
],
],
'combineCallableStringAndCallable' => [
'callable',
[
'callable-string',
'callable'
],
],
'combineCallableAndCallableObject' => [
'callable',
[
'callable',
'callable-object',
],
],
'combineCallableObjectAndCallable' => [
'callable',
[
'callable-object',
'callable'
],
],
'combineCallableAndCallableArray' => [
'callable',
[
'callable',
'callable-array',
],
],
'combineCallableArrayAndCallable' => [
'callable',
[
'callable-array',
'callable'
],
],
'combineCallableArrayAndArray' => [
2020-04-11 23:50:24 +02:00
'array<array-key, mixed>',
[
'callable-array{class-string, string}',
'array',
],
],
2020-04-11 23:50:24 +02:00
'combineGenericArrayAndMixedArray' => [
2020-04-09 15:27:14 +02:00
'array<array-key, int|mixed>',
2020-04-11 23:50:24 +02:00
[
'array<string, int>',
'array<array-key, mixed>',
],
],
'combineObjectLikeArrayAndArray' => [
'array<array-key, mixed>',
2020-04-09 15:27:14 +02:00
[
'array{hello: int}',
'array<array-key, mixed>',
],
],
'combineObjectLikeArrayAndNestedArray' => [
2020-04-11 23:50:24 +02:00
'array<array-key, mixed>',
2020-04-09 15:27:14 +02:00
[
'array{hello: array{goodbye: int}}',
'array<array-key, mixed>',
],
],
'combineNumericStringWithLiteralString' => [
'numeric-string',
[
'numeric-string',
'"1"',
],
],
'combineLiteralStringWithNumericString' => [
'numeric-string',
[
'"1"',
'numeric-string',
],
],
2020-07-22 05:16:56 +02:00
'combineNonEmptyListWithObjectLikeList' => [
'array{0: null|string}<int, string>',
[
'non-empty-list<string>',
'array{null}'
],
],
2020-07-27 01:09:26 +02:00
'combineZeroAndPositiveInt' => [
'int(0)|positive-int',
[
'0',
'positive-int',
],
],
'combinePositiveIntAndZero' => [
'int(0)|positive-int',
[
'positive-int',
'0',
],
],
'combinePositiveIntAndMinusOne' => [
'int',
[
'positive-int',
'-1',
],
],
'combineMinusOneAndPositiveInt' => [
'int',
[
'-1',
'positive-int',
],
],
'combineZeroMinusOneAndPositiveInt' => [
'int',
[
'0',
'-1',
'positive-int',
],
],
'combineZeroOneAndPositiveInt' => [
'int(0)|positive-int',
[
'0',
'1',
'positive-int',
],
],
'combinePositiveIntOneAndZero' => [
'int(0)|positive-int',
[
'positive-int',
'1',
'0',
],
],
'combinePositiveInts' => [
'positive-int',
[
'positive-int',
'positive-int',
],
],
];
}
/**
* @param string $string
2017-05-27 02:16:18 +02:00
*
* @return Type\Atomic
*/
2016-06-28 20:28:45 +02:00
private static function getAtomic($string)
{
return array_values(Type::parseString($string)->getAtomicTypes())[0];
2016-06-28 20:28:45 +02:00
}
2016-06-16 02:16:40 +02:00
}