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
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Type;
|
2016-06-15 20:16:40 -04:00
|
|
|
|
|
|
|
class TypeCombinationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 12:55:23 -05:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 02:29:00 -04:00
|
|
|
protected static $parser;
|
2016-06-15 20:16:40 -04:00
|
|
|
|
2017-01-02 15:31:18 -05:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-06-15 20:16:40 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 15:31:18 -05:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
|
|
|
}
|
|
|
|
|
2016-12-24 18:23:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
* @return Type\Atomic
|
|
|
|
*/
|
2016-06-28 14:28:45 -04:00
|
|
|
private static function getAtomic($string)
|
|
|
|
{
|
2016-06-28 15:25:28 -04:00
|
|
|
return array_values(Type::parseString($string)->types)[0];
|
2016-06-28 14:28:45 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testIntOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'int|string',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('int'),
|
|
|
|
self::getAtomic('string')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayOfIntOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, int|string>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<int>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayOfIntOrAlsoString()
|
|
|
|
{
|
2016-06-26 22:40:57 -04:00
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, int>|string',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<int>'),
|
|
|
|
self::getAtomic('string')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testEmptyArrays()
|
|
|
|
{
|
2016-06-26 22:40:57 -04:00
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<empty, empty>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-09-09 16:38:32 -04:00
|
|
|
self::getAtomic('array<empty,empty>'),
|
|
|
|
self::getAtomic('array<empty,empty>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayStringOrEmptyArray()
|
|
|
|
{
|
2016-06-26 22:40:57 -04:00
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, string>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<empty>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayMixedOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, mixed>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<mixed>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-09 18:36:35 -04:00
|
|
|
public function testArrayMixedOrStringKeys()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, string>',
|
2016-09-09 18:36:35 -04:00
|
|
|
(string) Type::combineTypes([
|
|
|
|
self::getAtomic('array<int|string,string>'),
|
|
|
|
self::getAtomic('array<mixed,string>')
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayMixedOrEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, mixed>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<empty>'),
|
|
|
|
self::getAtomic('array<mixed>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testArrayBigCombination()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array<mixed, int|float|string>',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('array<int|float>'),
|
|
|
|
self::getAtomic('array<string>')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-16 01:19:52 -04:00
|
|
|
public function testFalseDestruction()
|
2016-06-15 20:16:40 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-06-16 01:19:52 -04:00
|
|
|
'bool',
|
2016-06-15 20:16:40 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('false'),
|
|
|
|
self::getAtomic('bool')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-16 01:19:52 -04:00
|
|
|
public function testOnlyFalse()
|
2016-06-15 20:16:40 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('false')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-16 01:19:52 -04:00
|
|
|
public function testFalseFalseDestruction()
|
2016-06-15 20:16:40 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
|
|
|
(string) Type::combineTypes([
|
2016-06-28 14:28:45 -04:00
|
|
|
self::getAtomic('false'),
|
|
|
|
self::getAtomic('false')
|
2016-06-15 20:16:40 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-12 00:03:37 -04:00
|
|
|
public function testAAndAOfB()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'A<mixed>',
|
|
|
|
(string) Type::combineTypes([
|
|
|
|
self::getAtomic('A'),
|
|
|
|
self::getAtomic('A<B>')
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-02 18:59:58 -04:00
|
|
|
public function testCombineObjectType()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array{a:int, b:string}',
|
2016-10-02 18:59:58 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-10-28 13:24:06 -04:00
|
|
|
self::getAtomic('array{a:int}'),
|
|
|
|
self::getAtomic('array{b:string}')
|
2016-10-02 18:59:58 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-11-13 11:24:25 -05:00
|
|
|
'array{a:int|string, b:string}',
|
2016-10-02 18:59:58 -04:00
|
|
|
(string) Type::combineTypes([
|
2016-10-28 13:24:06 -04:00
|
|
|
self::getAtomic('array{a:int}'),
|
|
|
|
self::getAtomic('array{a:string,b:string}')
|
2016-10-02 18:59:58 -04:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-15 20:16:40 -04:00
|
|
|
public function testMultipleValuedArray()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-15 20:16:40 -04:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$var = [];
|
|
|
|
$var[] = new A();
|
|
|
|
$var[] = new B();
|
|
|
|
');
|
|
|
|
|
2017-01-02 15:31:18 -05:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 15:09:47 -05:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-15 20:16:40 -04:00
|
|
|
}
|
|
|
|
}
|