2016-06-15 20:16:40 -04:00
|
|
|
<?php
|
2016-07-25 18:37:44 -04:00
|
|
|
namespace Psalm\Tests;
|
2016-06-15 20:16:40 -04:00
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Type;
|
2018-05-19 19:44:03 -04:00
|
|
|
use Psalm\Type\TypeCombination;
|
2016-06-15 20:16:40 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class TypeCombinationTest extends TestCase
|
2016-06-15 20:16:40 -04:00
|
|
|
{
|
2018-11-05 21:57:36 -05:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-01-02 15:31:18 -05:00
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @dataProvider providerTestValidTypeCombination
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2017-04-24 23:45:02 -04:00
|
|
|
* @param string $expected
|
2017-11-19 12:14:02 -05:00
|
|
|
* @param array<int, string> $types
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2017-01-13 14:07:23 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-24 23:45:02 -04:00
|
|
|
public function testValidTypeCombination($expected, $types)
|
2016-06-15 20:16:40 -04:00
|
|
|
{
|
2017-04-24 23:45:02 -04:00
|
|
|
foreach ($types as $k => $type) {
|
|
|
|
$types[$k] = self::getAtomic($type);
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:59:31 -05:00
|
|
|
/** @psalm-suppress InvalidArgument */
|
2017-05-26 20:05:57 -04:00
|
|
|
$this->assertSame(
|
2017-04-24 23:45:02 -04:00
|
|
|
$expected,
|
2018-05-19 19:44:03 -04:00
|
|
|
(string) TypeCombination::combineTypes($types)
|
2017-04-24 23:45:02 -04:00
|
|
|
);
|
2016-06-15 20:16:40 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-05 21:57:36 -05:00
|
|
|
public function providerValidCodeParse()
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'multipleValuedArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$var = [];
|
|
|
|
$var[] = new A();
|
2017-05-26 20:05:57 -04:00
|
|
|
$var[] = new B();',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerTestValidTypeCombination()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'intOrString' => [
|
|
|
|
'int|string',
|
|
|
|
[
|
|
|
|
'int',
|
2017-05-26 20:05:57 -04:00
|
|
|
'string',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-12-09 15:51:38 -05:00
|
|
|
'falseTrueToBool' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'false',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'trueFalseToBool' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'true',
|
|
|
|
'false',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'trueBoolToBool' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'true',
|
|
|
|
'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'boolTrueToBool' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'bool',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'intOrTrueOrFalseToBool' => [
|
|
|
|
'int|bool',
|
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'false',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'intOrBoolOrTrueToBool' => [
|
|
|
|
'int|bool',
|
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'bool',
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'intOrTrueOrBoolToBool' => [
|
|
|
|
'int|bool',
|
|
|
|
[
|
|
|
|
'int',
|
|
|
|
'true',
|
|
|
|
'bool',
|
|
|
|
],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
'arrayOfIntOrString' => [
|
|
|
|
'array<mixed, int|string>',
|
|
|
|
[
|
|
|
|
'array<int>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayOfIntOrAlsoString' => [
|
|
|
|
'array<mixed, int>|string',
|
|
|
|
[
|
|
|
|
'array<int>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'string',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'emptyArrays' => [
|
|
|
|
'array<empty, empty>',
|
|
|
|
[
|
|
|
|
'array<empty,empty>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<empty,empty>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayStringOrEmptyArray' => [
|
|
|
|
'array<mixed, string>',
|
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayMixedOrString' => [
|
|
|
|
'array<mixed, mixed>',
|
|
|
|
[
|
|
|
|
'array<mixed>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayMixedOrStringKeys' => [
|
|
|
|
'array<mixed, string>',
|
|
|
|
[
|
|
|
|
'array<int|string,string>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<mixed,string>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayMixedOrEmpty' => [
|
|
|
|
'array<mixed, mixed>',
|
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<mixed>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'arrayBigCombination' => [
|
|
|
|
'array<mixed, int|float|string>',
|
|
|
|
[
|
|
|
|
'array<int|float>',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'falseDestruction' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'false',
|
2017-05-26 20:05:57 -04:00
|
|
|
'bool',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'onlyFalse' => [
|
2017-12-15 13:36:42 -08:00
|
|
|
'false',
|
2017-04-24 23:45:02 -04:00
|
|
|
[
|
2017-05-26 20:05:57 -04:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2017-12-15 13:36:42 -08:00
|
|
|
'onlyTrue' => [
|
|
|
|
'true',
|
|
|
|
[
|
|
|
|
'true',
|
|
|
|
],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
'falseFalseDestruction' => [
|
2017-12-19 00:06:05 -05:00
|
|
|
'false',
|
2017-04-24 23:45:02 -04:00
|
|
|
[
|
|
|
|
'false',
|
2017-05-26 20:05:57 -04:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'aAndAOfB' => [
|
2017-06-29 17:40:25 -04:00
|
|
|
'A',
|
2017-04-24 23:45:02 -04:00
|
|
|
[
|
|
|
|
'A',
|
2017-05-26 20:05:57 -04:00
|
|
|
'A<B>',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'combineObjectType1' => [
|
2018-03-17 16:53:11 -04:00
|
|
|
'array{a?:int, b?:string}',
|
2017-04-24 23:45:02 -04:00
|
|
|
[
|
|
|
|
'array{a:int}',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array{b:string}',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'combineObjectType2' => [
|
2018-03-17 16:53:11 -04:00
|
|
|
'array{a:int|string, b?:string}',
|
2017-04-24 23:45:02 -04:00
|
|
|
[
|
|
|
|
'array{a:int}',
|
2017-05-26 20:05:57 -04:00
|
|
|
'array{a:string,b:string}',
|
|
|
|
],
|
|
|
|
],
|
2017-11-17 16:33:36 -05:00
|
|
|
'combineObjectTypeWithIntKeyedArray' => [
|
|
|
|
'array<int|string, string|int>',
|
|
|
|
[
|
|
|
|
'array{a:int}',
|
|
|
|
'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineNestedObjectTypeWithObjectLikeIntKeyedArray' => [
|
|
|
|
'array{a:array<int|string, string|int>}',
|
|
|
|
[
|
|
|
|
'array{a:array{a:int}}',
|
|
|
|
'array{a:array<int, string>}',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineIntKeyedObjectTypeWithNestedIntKeyedArray' => [
|
|
|
|
'array<int, array<int|string, string|int>>',
|
|
|
|
[
|
|
|
|
'array<int, array{a:int}>',
|
|
|
|
'array<int, array<int, string>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'combineNestedObjectTypeWithNestedIntKeyedArray' => [
|
|
|
|
'array<int|string, array<int|string, string|int>>',
|
|
|
|
[
|
|
|
|
'array{a:array{a:int}}',
|
|
|
|
'array<int, array<int, string>>',
|
|
|
|
],
|
|
|
|
],
|
2018-03-23 13:14:00 -04:00
|
|
|
'combinePossiblyUndefinedKeys' => [
|
|
|
|
'array{a:bool, b?:mixed, d?:mixed}',
|
|
|
|
[
|
|
|
|
'array{a:false, b:mixed}',
|
|
|
|
'array{a:true, d:mixed}',
|
|
|
|
'array{a:true, d:mixed}',
|
|
|
|
],
|
|
|
|
],
|
2018-04-02 14:36:33 -04:00
|
|
|
'combinePossiblyUndefinedKeysAndString' => [
|
|
|
|
'array{a:string, b?:int}|string',
|
|
|
|
[
|
|
|
|
'array{a:string, b?:int}',
|
|
|
|
'string',
|
|
|
|
],
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
2017-01-02 15:31:18 -05:00
|
|
|
}
|
|
|
|
|
2016-12-24 18:23:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2016-12-24 18:23:22 +00:00
|
|
|
* @return Type\Atomic
|
|
|
|
*/
|
2016-06-28 14:28:45 -04:00
|
|
|
private static function getAtomic($string)
|
|
|
|
{
|
2018-01-09 15:05:48 -05:00
|
|
|
return array_values(Type::parseString($string)->getTypes())[0];
|
2016-06-28 14:28:45 -04:00
|
|
|
}
|
2016-06-15 20:16:40 -04:00
|
|
|
}
|