2016-06-17 22:05:28 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
2016-10-11 04:49:43 +02:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\ParserFactory;
|
2016-06-17 22:05:28 +02:00
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-10-11 04:49:43 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Checker\TypeChecker;
|
|
|
|
use Psalm\Type;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
|
|
|
class TypeReconciliationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-10-11 04:49:43 +02:00
|
|
|
protected static $_parser;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
$config = \Psalm\Config::getInstance();
|
|
|
|
$config->throw_exception = true;
|
|
|
|
$config->use_docblock_types = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
\Psalm\Checker\FileChecker::clearCache();
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testNotNull()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|null'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject|false',
|
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|false'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('mixed'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|null'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|false'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('mixed'))
|
|
|
|
);
|
|
|
|
|
2016-07-26 00:31:03 +02:00
|
|
|
// @todo in the future this should also work
|
|
|
|
/*
|
2016-06-17 22:05:28 +02:00
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject|true',
|
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|bool'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
2016-07-26 00:31:03 +02:00
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNull()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject|null'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject|false'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('mixed'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'false',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|false'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'false',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|bool'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('mixed'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$reconciled = TypeChecker::reconcileTypes('empty', Type::parseString('bool'));
|
|
|
|
$this->assertEquals('false', (string) $reconciled);
|
2016-07-26 00:37:44 +02:00
|
|
|
$this->assertInstanceOf('Psalm\Type\Atomic', $reconciled->types['false']);
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:53:36 +02:00
|
|
|
public function testNotMyObject()
|
2016-06-17 22:05:28 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|bool'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2016-07-12 06:53:36 +02:00
|
|
|
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|null'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObjectB',
|
|
|
|
(string) TypeChecker::reconcileTypes('!MyObjectA', Type::parseString('MyObjectA|MyObjectB'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-12 06:53:36 +02:00
|
|
|
public function testMyObject()
|
2016-06-27 04:40:57 +02:00
|
|
|
{
|
2016-06-17 22:05:28 +02:00
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
|
|
|
(string) TypeChecker::reconcileTypes('MyObject', Type::parseString('MyObject|bool'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObjectA',
|
|
|
|
(string) TypeChecker::reconcileTypes('MyObjectA', Type::parseString('MyObjectA|MyObjectB'))
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
public function testAllMixed()
|
|
|
|
{
|
2016-06-17 22:05:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
|
|
|
(string) TypeChecker::reconcileTypes('mixed', Type::parseString('mixed'))
|
|
|
|
);
|
|
|
|
}
|
2016-10-11 04:49:43 +02:00
|
|
|
|
|
|
|
public function testNotInstanceOf()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
|
|
|
|
class B extends A { }
|
|
|
|
|
|
|
|
$out = null;
|
|
|
|
|
|
|
|
if ($a instanceof B) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$context->vars_in_scope['a'] = Type::parseString('A');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('null|A', (string) $context->vars_in_scope['out']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotInstanceOfProperty()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class B { }
|
|
|
|
|
|
|
|
class C extends B { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var B */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = null;
|
|
|
|
|
|
|
|
if ($a->foo instanceof C) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a->foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$context->vars_in_scope['a'] = Type::parseString('A');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('null|B', (string) $context->vars_in_scope['out']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTypeArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
$a = min(0, 1);
|
|
|
|
$b = min([0, 1]);
|
|
|
|
$c = min("a", "b");
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('mixed', (string) $context->vars_in_scope['a']);
|
|
|
|
$this->assertEquals('mixed', (string) $context->vars_in_scope['b']);
|
|
|
|
}
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|