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-07-26 00:37:44 +02:00
|
|
|
use Psalm\Type;
|
2016-06-16 02:16:40 +02:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
|
|
|
|
class TypeCombinationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected static $_parser;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
public function testIntOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'int|string',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('int'),
|
|
|
|
self::getAtomic('string')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayOfIntOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<int|string>',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<int>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayOfIntOrAlsoString()
|
|
|
|
{
|
2016-06-27 04:40:57 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'array<int>|string',
|
2016-06-16 02:16:40 +02:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<int>'),
|
|
|
|
self::getAtomic('string')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptyArrays()
|
|
|
|
{
|
2016-06-27 04:40:57 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'array<empty>',
|
2016-06-16 02:16:40 +02:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<empty>'),
|
|
|
|
self::getAtomic('array<empty>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayStringOrEmptyArray()
|
|
|
|
{
|
2016-06-27 04:40:57 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'array<string>',
|
2016-06-16 02:16:40 +02:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<empty>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayMixedOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<mixed>',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<mixed>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayMixedOrEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<mixed>',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<empty>'),
|
|
|
|
self::getAtomic('array<mixed>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayBigCombination()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<int|float|string>',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('array<int|float>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:19:52 +02:00
|
|
|
public function testFalseDestruction()
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-06-16 07:19:52 +02:00
|
|
|
'bool',
|
2016-06-16 02:16:40 +02:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('false'),
|
|
|
|
self::getAtomic('bool')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:19:52 +02:00
|
|
|
public function testOnlyFalse()
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('false')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:19:52 +02:00
|
|
|
public function testFalseFalseDestruction()
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 20:28:45 +02:00
|
|
|
self::getAtomic('false'),
|
|
|
|
self::getAtomic('false')
|
2016-06-16 02:16:40 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleValuedArray()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$var = [];
|
|
|
|
$var[] = new A();
|
|
|
|
$var[] = new B();
|
|
|
|
');
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
2016-06-16 02:16:40 +02:00
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
}
|