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\ParserFactory;
|
2016-06-17 22:05:28 +02:00
|
|
|
use PHPUnit_Framework_TestCase;
|
2017-03-18 18:37:00 +01:00
|
|
|
use Psalm\Checker\AlgebraChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
2017-01-02 21:31:18 +01:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
2016-10-11 04:49:43 +02:00
|
|
|
use Psalm\Checker\TypeChecker;
|
2016-12-27 19:58:58 +01:00
|
|
|
use Psalm\Clause;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-10-11 04:49:43 +02:00
|
|
|
use Psalm\Type;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
|
|
|
class TypeReconciliationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 07:29:00 +01:00
|
|
|
protected static $parser;
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
|
|
/** @var FileChecker */
|
|
|
|
protected $file_checker;
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-11 04:49:43 +02:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-11 04:49:43 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
FileChecker::clearCache();
|
2017-02-01 01:22:05 +01:00
|
|
|
|
|
|
|
$this->project_checker = new ProjectChecker();
|
2017-03-13 23:07:36 +01:00
|
|
|
$this->project_checker->setConfig(new TestConfig());
|
2017-01-02 21:31:18 +01:00
|
|
|
|
|
|
|
$this->file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-17 00:33:04 +01:00
|
|
|
$this->file_checker->context = new Context();
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testNotNull()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|null'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject|false',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|false'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!null', Type::parseString('mixed'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testNotEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|null'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObject',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|false'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('mixed'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testNull()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject|null'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('null', Type::parseString('mixed'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testEmpty()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'false',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|false'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'false',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|bool'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'mixed',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('empty', Type::parseString('mixed'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var Type\Union */
|
2017-01-02 21:31:18 +01:00
|
|
|
$reconciled = TypeChecker::reconcileTypes('empty', Type::parseString('bool'), null, $this->file_checker);
|
2016-06-17 22:05:28 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-12 06:53:36 +02:00
|
|
|
public function testNotMyObject()
|
2016-06-17 22:05:28 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'bool',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|bool'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'null',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|null'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObjectB',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('!MyObjectA', Type::parseString('MyObjectA|MyObjectB'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
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',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('MyObject', Type::parseString('MyObject|bool'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-07-12 06:53:36 +02:00
|
|
|
'MyObjectA',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('MyObjectA', Type::parseString('MyObjectA|MyObjectB'), null, $this->file_checker)
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-19 23:45:42 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArray()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<mixed, mixed>',
|
|
|
|
(string) TypeChecker::reconcileTypes('array', Type::parseString('array|null'), null, $this->file_checker)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test2DArray()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'array<mixed, array<mixed, string>>',
|
|
|
|
(string) TypeChecker::reconcileTypes('array', Type::parseString('array<array<string>>|null'), null, $this->file_checker)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-24 19:23:22 +01:00
|
|
|
public function testArrayContains()
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
TypeChecker::isContainedBy(
|
|
|
|
Type::parseString('array<string>'),
|
2017-01-02 21:31:18 +01:00
|
|
|
Type::parseString('array'),
|
|
|
|
$this->file_checker
|
2016-12-24 19:23:22 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
TypeChecker::isContainedBy(
|
|
|
|
Type::parseString('array<Exception>'),
|
2017-01-02 21:31:18 +01:00
|
|
|
Type::parseString('array'),
|
|
|
|
$this->file_checker
|
2016-12-24 19:23:22 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-19 07:12:19 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUnionContains()
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
TypeChecker::isContainedBy(
|
|
|
|
Type::parseString('string'),
|
|
|
|
Type::parseString('string|false'),
|
|
|
|
$this->file_checker
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
TypeChecker::isContainedBy(
|
|
|
|
Type::parseString('false'),
|
|
|
|
Type::parseString('string|false'),
|
|
|
|
$this->file_checker
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-24 00:52:34 +01:00
|
|
|
public function testNumeric()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
'string',
|
2017-01-02 21:31:18 +01:00
|
|
|
(string) TypeChecker::reconcileTypes('numeric', Type::parseString('string'), null, $this->file_checker)
|
2016-12-24 00:52:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testNegateFormula()
|
|
|
|
{
|
|
|
|
$formula = [
|
|
|
|
new Clause(['$a' => ['!empty']])
|
|
|
|
];
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_formula = AlgebraChecker::negateFormula($formula);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
|
|
|
$this->assertSame(1, count($negated_formula));
|
|
|
|
$this->assertSame(['$a' => ['empty']], $negated_formula[0]->possibilities);
|
|
|
|
|
|
|
|
$formula = [
|
|
|
|
new Clause(['$a' => ['!empty'], '$b' => ['!empty']])
|
|
|
|
];
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_formula = AlgebraChecker::negateFormula($formula);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
|
|
|
$this->assertSame(2, count($negated_formula));
|
|
|
|
$this->assertSame(['$a' => ['empty']], $negated_formula[0]->possibilities);
|
|
|
|
$this->assertSame(['$b' => ['empty']], $negated_formula[1]->possibilities);
|
|
|
|
|
|
|
|
$formula = [
|
|
|
|
new Clause(['$a' => ['!empty']]),
|
|
|
|
new Clause(['$b' => ['!empty']]),
|
|
|
|
];
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_formula = AlgebraChecker::negateFormula($formula);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
|
|
|
$this->assertSame(1, count($negated_formula));
|
|
|
|
$this->assertSame(['$a' => ['empty'], '$b' => ['empty']], $negated_formula[0]->possibilities);
|
|
|
|
|
|
|
|
$formula = [
|
|
|
|
new Clause(['$a' => ['int', 'string'], '$b' => ['!empty']])
|
|
|
|
];
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_formula = AlgebraChecker::negateFormula($formula);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
|
|
|
$this->assertSame(3, count($negated_formula));
|
|
|
|
$this->assertSame(['$a' => ['!int']], $negated_formula[0]->possibilities);
|
|
|
|
$this->assertSame(['$a' => ['!string']], $negated_formula[1]->possibilities);
|
|
|
|
$this->assertSame(['$b' => ['empty']], $negated_formula[2]->possibilities);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testContainsClause()
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
(new Clause(
|
|
|
|
[
|
|
|
|
'$a' => ['!empty'],
|
|
|
|
'$b' => ['!empty']
|
|
|
|
]
|
|
|
|
))->contains(
|
|
|
|
new Clause(
|
|
|
|
[
|
|
|
|
'$a' => ['!empty']
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
(new Clause(
|
|
|
|
[
|
|
|
|
'$a' => ['!empty']
|
|
|
|
]
|
|
|
|
))->contains(
|
|
|
|
new Clause(
|
|
|
|
[
|
|
|
|
'$a' => ['!empty'],
|
|
|
|
'$b' => ['!empty']
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testSimplifyCNF()
|
|
|
|
{
|
|
|
|
$formula = [
|
|
|
|
new Clause(['$a' => ['!empty']]),
|
|
|
|
new Clause(['$a' => ['empty'], '$b' => ['empty']])
|
|
|
|
];
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$simplified_formula = AlgebraChecker::simplifyCNF($formula);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
|
|
|
$this->assertSame(2, count($simplified_formula));
|
|
|
|
$this->assertSame(['$a' => ['!empty']], $simplified_formula[0]->possibilities);
|
|
|
|
$this->assertSame(['$b' => ['empty']], $simplified_formula[1]->possibilities);
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-11 19:48:11 +01:00
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-11 19:48:11 +01:00
|
|
|
*/
|
|
|
|
public function testMakeNonNullableNull()
|
2016-06-27 04:40:57 +02:00
|
|
|
{
|
2016-12-11 19:48:11 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
$a = new A();
|
|
|
|
if ($a === null) {
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2016-12-12 05:41:11 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 19:50:46 +01:00
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 19:50:46 +01:00
|
|
|
*/
|
|
|
|
public function testMakeInstanceOfThingInElseif()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
class B { }
|
|
|
|
class C { }
|
|
|
|
$a = rand(0, 10) > 5 ? new A() : new B();
|
|
|
|
if ($a instanceof A) {
|
|
|
|
} elseif ($a instanceof C) {
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-12 19:50:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 20:53:45 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testFunctionValueIsNotType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (json_last_error() === "5") { }
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStringIsNotInt()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (5 === "5") { }
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStringIsNotNull()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (5 === null) { }
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStringIsNotFalse()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (5 === false) { }
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:50:46 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 05:41:11 +01:00
|
|
|
* @expectedExceptionMessage FailedTypeResolution
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
|
|
|
public function testFailedTypeResolution()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
2017-01-15 23:52:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo(A $a) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-15 23:52:01 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage FailedTypeResolution
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testFailedTypeResolutionWithDocblock()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo(A $a) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-15 23:52:01 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTypeResolutionFromDocblock()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo($a) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-15 23:52:01 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-02-04 04:07:14 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayTypeResolutionFromDocblock()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param string[] $strs
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo(array $strs) {
|
|
|
|
foreach ($strs as $str) {
|
|
|
|
if (is_string($str)) {} // Issue emitted here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:33:35 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTypeResolutionFromDocblockInside()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param int $length
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo($length) {
|
|
|
|
if (!is_int($length)) {
|
|
|
|
if (is_numeric($length)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-16 22:33:35 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-15 23:52:01 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage FailedTypeResolution
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTypeResolutionFromDocblockAndInstanceof()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo($a) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-11 04:49:43 +02:00
|
|
|
public function testNotInstanceOf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-11 04:49:43 +02:00
|
|
|
class A { }
|
|
|
|
|
|
|
|
class B extends A { }
|
|
|
|
|
|
|
|
$out = null;
|
|
|
|
|
|
|
|
if ($a instanceof B) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-10-15 19:10:05 +02:00
|
|
|
$context->vars_in_scope['$a'] = Type::parseString('A');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertEquals('null|A', (string) $context->vars_in_scope['$out']);
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-11 04:49:43 +02:00
|
|
|
public function testNotInstanceOfProperty()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-11 04:49:43 +02:00
|
|
|
class B { }
|
|
|
|
|
|
|
|
class C extends B { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var B */
|
|
|
|
public $foo;
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo = new B();
|
|
|
|
}
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
$a = new A();
|
|
|
|
|
2016-10-11 04:49:43 +02:00
|
|
|
$out = null;
|
|
|
|
|
|
|
|
if ($a->foo instanceof C) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a->foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-10-15 19:10:05 +02:00
|
|
|
$context->vars_in_scope['$a'] = Type::parseString('A');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertEquals('null|B', (string) $context->vars_in_scope['$out']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-15 19:10:05 +02:00
|
|
|
public function testNotInstanceOfPropertyElseif()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-15 19:10:05 +02:00
|
|
|
class B { }
|
|
|
|
|
|
|
|
class C extends B { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var string|B */
|
2017-01-27 07:23:12 +01:00
|
|
|
public $foo = "";
|
2016-10-15 19:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$out = null;
|
|
|
|
|
|
|
|
if (is_string($a->foo)) {
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif ($a->foo instanceof C) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a->foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-10-15 19:10:05 +02:00
|
|
|
$context->vars_in_scope['$a'] = Type::parseString('A');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertEquals('null|B', (string) $context->vars_in_scope['$out']);
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-11 04:49:43 +02:00
|
|
|
public function testTypeArguments()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-11 04:49:43 +02:00
|
|
|
$a = min(0, 1);
|
|
|
|
$b = min([0, 1]);
|
|
|
|
$c = min("a", "b");
|
2017-01-15 19:16:36 +01:00
|
|
|
$d = min(1, 2, 3, 4);
|
|
|
|
$e = min(1, 2, 3, 4, 5);
|
2017-01-16 18:39:38 +01:00
|
|
|
sscanf("10:05:03", "%d:%d:%d", $hours, $minutes, $seconds);
|
2016-10-11 04:49:43 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$b']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$c']);
|
2017-01-16 18:39:38 +01:00
|
|
|
$this->assertEquals('string', (string)$context->vars_in_scope['$hours']);
|
|
|
|
$this->assertEquals('string', (string)$context->vars_in_scope['$minutes']);
|
|
|
|
$this->assertEquals('string', (string)$context->vars_in_scope['$seconds']);
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
2016-12-24 00:52:34 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-24 00:52:34 +01:00
|
|
|
* @expectedExceptionMessage TypeDoesNotContainType
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-24 00:52:34 +01:00
|
|
|
*/
|
|
|
|
public function testTypeTransformation()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = "5";
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-12-24 00:52:34 +01:00
|
|
|
if (is_numeric($a)) {
|
|
|
|
if (is_int($a)) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
2017-01-18 17:07:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTypeRefinementWithIsNumeric()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @return void */
|
|
|
|
function fooFoo(string $a) {
|
|
|
|
if (is_numeric($a)) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTypeRefinementWithIsNumericAndIsString()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo ($a) {
|
|
|
|
if (is_numeric($a)) {
|
|
|
|
if (is_string($a)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-01-31 06:34:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUpdateMultipleIssetVars()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
|
|
|
|
|
|
|
$a = rand(0, 1) ? ["hello"] : null;
|
|
|
|
if (isset($a[0])) {
|
|
|
|
foo($a[0]);
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-01-31 07:35:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUpdateMultipleIssetVarsWithVariableOffset()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
|
|
|
|
|
|
|
$a = rand(0, 1) ? ["hello"] : null;
|
|
|
|
$b = 0;
|
|
|
|
if (isset($a[$b])) {
|
|
|
|
foo($a[$b]);
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-02-01 19:51:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRemoveEmptyArray()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$arr_or_string = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$arr_or_string = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
|
|
|
|
|
|
|
if (!empty($arr_or_string)) {
|
|
|
|
foo($arr_or_string);
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-02-21 22:15:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInstanceOfSubtypes()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
abstract class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
abstract class C {}
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
function makeA(): A {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeC(): C {
|
|
|
|
return new D();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 1) ? makeA() : makeC();
|
|
|
|
|
|
|
|
if ($a instanceof B || $a instanceof D) { }
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-11 18:05:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmptyArrayReconciliationThenIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param string|string[] $a
|
|
|
|
*/
|
|
|
|
function foo($a) : string {
|
|
|
|
if (is_string($a)) {
|
|
|
|
return $a;
|
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($a[0])) {
|
|
|
|
return $a[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
return "not found";
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-03-11 18:32:27 +01:00
|
|
|
public function testEmptyStringReconciliationThenIf()
|
2017-03-11 18:05:23 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
2017-03-11 18:32:27 +01:00
|
|
|
* @param Exception|string|string[] $a
|
2017-03-11 18:05:23 +01:00
|
|
|
*/
|
|
|
|
function foo($a) : string {
|
2017-03-11 18:32:27 +01:00
|
|
|
if (is_array($a)) {
|
|
|
|
return "hello";
|
2017-03-11 18:05:23 +01:00
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
2017-03-11 18:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($a)) {
|
|
|
|
return $a;
|
2017-03-11 18:05:23 +01:00
|
|
|
};
|
|
|
|
|
2017-03-11 18:32:27 +01:00
|
|
|
return "an exception";
|
2017-03-11 18:05:23 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-13 23:07:36 +01:00
|
|
|
|
2017-03-14 20:48:52 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmptyExceptionReconciliationAfterIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param Exception|null $a
|
|
|
|
*/
|
|
|
|
function foo($a) : string {
|
|
|
|
if ($a && $a->getMessage() === "hello") {
|
|
|
|
return "hello";
|
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a->getMessage();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:07:36 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIgnoreNullCheckAndMaintainNullValue()
|
|
|
|
{
|
|
|
|
Config::getInstance()->setCustomErrorLevel('FailedTypeResolution', Config::REPORT_SUPPRESS);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = null;
|
|
|
|
if ($a !== null) { }
|
|
|
|
$b = $a;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
$this->assertEquals('null', (string) $context->vars_in_scope['$b']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIgnoreNullCheckAndMaintainNullableValue()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 1) ? 5 : null;
|
|
|
|
if ($a !== null) { }
|
|
|
|
$b = $a;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
$this->assertEquals('int|null', (string) $context->vars_in_scope['$b']);
|
|
|
|
}
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|