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
|
|
|
|
2016-11-02 07:29:00 +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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
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
|
|
|
|
* @param string $expected
|
|
|
|
* @param array<string> $types
|
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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ($types as $k => $type) {
|
|
|
|
$types[$k] = self::getAtomic($type);
|
|
|
|
}
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2017-04-25 05:45:02 +02:00
|
|
|
$expected,
|
|
|
|
(string) Type::combineTypes($types)
|
|
|
|
);
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'multipleValuedArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$var = [];
|
|
|
|
$var[] = new A();
|
2017-05-27 02:05:57 +02:00
|
|
|
$var[] = new B();',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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
|
|
|
],
|
|
|
|
'arrayOfIntOrString' => [
|
|
|
|
'array<mixed, int|string>',
|
|
|
|
[
|
|
|
|
'array<int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayOfIntOrAlsoString' => [
|
|
|
|
'array<mixed, int>|string',
|
|
|
|
[
|
|
|
|
'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' => [
|
|
|
|
'array<mixed, string>',
|
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrString' => [
|
|
|
|
'array<mixed, mixed>',
|
|
|
|
[
|
|
|
|
'array<mixed>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrStringKeys' => [
|
|
|
|
'array<mixed, string>',
|
|
|
|
[
|
|
|
|
'array<int|string,string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<mixed,string>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMixedOrEmpty' => [
|
|
|
|
'array<mixed, mixed>',
|
|
|
|
[
|
|
|
|
'array<empty>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<mixed>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayBigCombination' => [
|
|
|
|
'array<mixed, int|float|string>',
|
|
|
|
[
|
|
|
|
'array<int|float>',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array<string>',
|
|
|
|
],
|
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' => [
|
|
|
|
'bool',
|
|
|
|
[
|
2017-05-27 02:05:57 +02:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'falseFalseDestruction' => [
|
|
|
|
'bool',
|
|
|
|
[
|
|
|
|
'false',
|
2017-05-27 02:05:57 +02:00
|
|
|
'false',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'aAndAOfB' => [
|
|
|
|
'A<mixed>',
|
|
|
|
[
|
|
|
|
'A',
|
2017-05-27 02:05:57 +02:00
|
|
|
'A<B>',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'combineObjectType1' => [
|
|
|
|
'array{a:int, b:string}',
|
|
|
|
[
|
|
|
|
'array{a:int}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array{b:string}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'combineObjectType2' => [
|
|
|
|
'array{a:int|string, b:string}',
|
|
|
|
[
|
|
|
|
'array{a:int}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'array{a:string,b:string}',
|
|
|
|
],
|
|
|
|
],
|
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
|
|
|
|
* @return Type\Atomic
|
|
|
|
*/
|
2016-06-28 20:28:45 +02:00
|
|
|
private static function getAtomic($string)
|
|
|
|
{
|
2016-06-28 21:25:28 +02:00
|
|
|
return array_values(Type::parseString($string)->types)[0];
|
2016-06-28 20:28:45 +02:00
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|