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;
|
2018-11-18 18:41:47 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Type;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2017-04-25 05:45:02 +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-02 21:31:18 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestValidTypeCombination
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $expected
|
2019-10-09 00:44:46 +02:00
|
|
|
* @param list<string> $types
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
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);
|
|
|
|
$converted_type->setFromDocblock();
|
|
|
|
$converted_types[] = $converted_type;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2017-04-25 05:45:02 +02:00
|
|
|
$expected,
|
2019-10-09 00:44:46 +02:00
|
|
|
(string) TypeCombination::combineTypes($converted_types)
|
2017-04-25 05:45:02 +02:00
|
|
|
);
|
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[]}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'multipleValuedArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$var = [];
|
|
|
|
$var[] = new A();
|
2017-05-27 02:05:57 +02:00
|
|
|
$var[] = new B();',
|
|
|
|
],
|
2019-01-29 16:34:31 +01:00
|
|
|
'preventLiteralAndClassString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param "array"|class-string $type_name
|
|
|
|
*/
|
|
|
|
function foo(string $type_name) : bool {
|
|
|
|
return $type_name === "array";
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-09 00:44:46 +02:00
|
|
|
* @return array<string,array{string,list<string>}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
|
|
|
public function providerTestValidTypeCombination()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'intOrString' => [
|
|
|
|
'int|string',
|
|
|
|
[
|
|
|
|
'int',
|
2017-05-27 02:05:57 +02:00
|
|
|
'string',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
'mixedOrNull' => [
|
|
|
|
'mixed|null',
|
|
|
|
[
|
|
|
|
'mixed',
|
|
|
|
'null',
|
|
|
|
],
|
|
|
|
],
|
2018-12-20 02:01:27 +01:00
|
|
|
'mixedOrEmpty' => [
|
|
|
|
'mixed',
|
|
|
|
[
|
|
|
|
'empty',
|
|
|
|
'mixed',
|
|
|
|
],
|
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
'mixedOrObject' => [
|
|
|
|
'mixed|object',
|
|
|
|
[
|
|
|
|
'mixed',
|
|
|
|
'object',
|
|
|
|
],
|
|
|
|
],
|
2018-12-14 18:30:13 +01:00
|
|
|
'mixedOrEmptyArray' => [
|
2018-12-14 21:10:10 +01:00
|
|
|
'array<empty, empty>|mixed',
|
2018-12-14 18:30:13 +01:00
|
|
|
[
|
|
|
|
'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' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'bool|int',
|
2017-12-09 21:51:38 +01:00
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'false',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'intOrBoolOrTrueToBool' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'bool|int',
|
2017-12-09 21:51:38 +01:00
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'bool',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'intOrTrueOrBoolToBool' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'bool|int',
|
2017-12-09 21:51:38 +01:00
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'true',
|
|
|
|
'bool',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'arrayOfIntOrString' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'array<array-key, int|string>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayOfIntOrAlsoString' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'array<array-key, int>|string',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'string',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'emptyArrays' => [
|
|
|
|
'array<empty, empty>',
|
|
|
|
[
|
|
|
|
'array<empty,empty>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<empty,empty>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayStringOrEmptyArray' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'array<array-key, string>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrString' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'array<array-key, mixed|string>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<mixed>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrStringKeys' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array<int|mixed|string, string>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<int|string,string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<mixed,string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrEmpty' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'array<array-key, mixed>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<mixed>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayBigCombination' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array<array-key, float|int|string>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'array<int|float>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'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' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'iterable<int, bool|string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
[
|
|
|
|
'array<int, string>',
|
|
|
|
'Traversable<int, bool>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayIterableToIterableWithParams' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'iterable<int, bool|string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
[
|
|
|
|
'array<int, string>',
|
|
|
|
'iterable<int, bool>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'iterableArrayToIterableWithParams' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'iterable<int, bool|string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
[
|
|
|
|
'iterable<int, string>',
|
|
|
|
'array<int, bool>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'traversableIterableToIterableWithParams' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'iterable<int, bool|string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
[
|
|
|
|
'Traversable<int, string>',
|
|
|
|
'iterable<int, bool>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'iterableTraversableToIterableWithParams' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'iterable<int, bool|string>',
|
2019-01-25 20:43:08 +01:00
|
|
|
[
|
|
|
|
'iterable<int, string>',
|
|
|
|
'Traversable<int, bool>',
|
2019-01-26 22:58:49 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayObjectAndParamsWithEmptyArray' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'ArrayObject<int, string>|array<empty, empty>',
|
2019-01-26 22:58:49 +01:00
|
|
|
[
|
|
|
|
'ArrayObject<int, string>',
|
|
|
|
'array<empty, empty>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'emptyArrayWithArrayObjectAndParams' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'ArrayObject<int, string>|array<empty, empty>',
|
2019-01-26 22:58:49 +01:00
|
|
|
[
|
|
|
|
'array<empty, empty>',
|
|
|
|
'ArrayObject<int, string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'falseDestruction' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'false',
|
2017-05-27 02:05:57 +02:00
|
|
|
'bool',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'onlyFalse' => [
|
2017-12-15 22:36:42 +01:00
|
|
|
'false',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
2017-05-27 02:05:57 +02:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-12-15 22:36:42 +01:00
|
|
|
'onlyTrue' => [
|
|
|
|
'true',
|
|
|
|
[
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'falseFalseDestruction' => [
|
2017-12-19 06:06:05 +01:00
|
|
|
'false',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'false',
|
2017-05-27 02:05:57 +02:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'aAndAOfB' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'A|A<B>',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
|
|
|
'A',
|
2017-05-27 02:05:57 +02:00
|
|
|
'A<B>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'combineObjectType1' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a?: int, b?: string}',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int}',
|
|
|
|
'array{b: string}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'combineObjectType2' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int|string, b?: string}',
|
2017-04-25 05:45:02 +02:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int}',
|
|
|
|
'array{a: string,b: string}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
|
|
|
],
|
2017-11-17 22:33:36 +01:00
|
|
|
'combineObjectTypeWithIntKeyedArray' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array<int|string, int|string>',
|
2017-11-17 22:33:36 +01:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int}',
|
2017-11-17 22:33:36 +01:00
|
|
|
'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineNestedObjectTypeWithObjectLikeIntKeyedArray' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array{a: array<int|string, int|string>}',
|
2017-11-17 22:33:36 +01:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: array{a: int}}',
|
|
|
|
'array{a: array<int, string>}',
|
2017-11-17 22:33:36 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineIntKeyedObjectTypeWithNestedIntKeyedArray' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array<int, array<int|string, int|string>>',
|
2017-11-17 22:33:36 +01:00
|
|
|
[
|
|
|
|
'array<int, array{a:int}>',
|
|
|
|
'array<int, array<int, string>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineNestedObjectTypeWithNestedIntKeyedArray' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'array<int|string, array<int|string, int|string>>',
|
2017-11-17 22:33:36 +01:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: array{a: int}}',
|
2017-11-17 22:33:36 +01:00
|
|
|
'array<int, array<int, string>>',
|
|
|
|
],
|
|
|
|
],
|
2018-03-23 18:14:00 +01:00
|
|
|
'combinePossiblyUndefinedKeys' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: bool, b?: mixed, d?: mixed}',
|
2018-03-23 18:14:00 +01:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: false, b: mixed}',
|
|
|
|
'array{a: true, d: mixed}',
|
|
|
|
'array{a: true, d: mixed}',
|
2018-03-23 18:14:00 +01:00
|
|
|
],
|
|
|
|
],
|
2018-04-02 20:36:33 +02:00
|
|
|
'combinePossiblyUndefinedKeysAndString' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: string, b?: int}|string',
|
2018-04-02 20:36:33 +02:00
|
|
|
[
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: string, b?: int}',
|
2018-04-02 20:36:33 +02:00
|
|
|
'string',
|
|
|
|
],
|
|
|
|
],
|
2019-06-21 02:13:36 +02:00
|
|
|
'combineMixedArrayWithObjectLike' => [
|
|
|
|
'array<array-key, mixed>',
|
|
|
|
[
|
|
|
|
'array{a: int}',
|
|
|
|
'array',
|
|
|
|
],
|
|
|
|
],
|
2019-05-06 22:38:08 +02:00
|
|
|
'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>',
|
|
|
|
],
|
|
|
|
],
|
2019-05-08 19:23:47 +02:00
|
|
|
'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',
|
|
|
|
],
|
|
|
|
],
|
2019-05-13 17:00:17 +02:00
|
|
|
'objectLikePlusArrayEqualsArray' => [
|
|
|
|
'array<string, int>',
|
|
|
|
[
|
|
|
|
'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}',
|
2019-05-13 17:00:17 +02:00
|
|
|
],
|
|
|
|
],
|
2019-09-22 06:58:30 +02:00
|
|
|
'combineClosures' => [
|
|
|
|
'Closure(A):void|Closure(B):void',
|
|
|
|
[
|
|
|
|
'Closure(A):void',
|
|
|
|
'Closure(B):void',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 19:23:22 +01:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-24 19:23:22 +01:00
|
|
|
* @return Type\Atomic
|
|
|
|
*/
|
2016-06-28 20:28:45 +02:00
|
|
|
private static function getAtomic($string)
|
|
|
|
{
|
2018-01-09 21:05:48 +01:00
|
|
|
return array_values(Type::parseString($string)->getTypes())[0];
|
2016-06-28 20:28:45 +02:00
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|