2016-04-04 01:47:06 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-04-04 01:47:06 +02:00
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-04-04 01:47:06 +02:00
|
|
|
|
|
|
|
class TypeTest 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-04-04 06:17:19 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-06-27 04:40:57 +02:00
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-06-27 04:40:57 +02:00
|
|
|
$config->throw_exception = true;
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 01:47:06 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 01:47:06 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodCall()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 01:47:06 +02:00
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$a->fooFoo();
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 02:14:19 +02:00
|
|
|
public function testNullableMethodWithTernaryGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 02:14:19 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = $a ? $a->fooFoo() : null;
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 02:14:19 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 02:14:19 +02:00
|
|
|
public function testNullableMethodWithTernaryIfNullGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 02:14:19 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = $a === null ? null : $a->fooFoo();
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 02:14:19 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 02:14:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithTernaryEmptyGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 22:33:26 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = empty($a) ? null : $a->fooFoo();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithTernaryIsNullGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 22:33:26 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = is_null($a) ? null : $a->fooFoo();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 02:14:19 +02:00
|
|
|
public function testNullableMethodWithIfGuard()
|
2016-04-04 01:47:06 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 01:47:06 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
2016-04-04 01:47:06 +02:00
|
|
|
if ($a) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->fooFoo();
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 07:21:28 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 07:21:28 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodCallWithThis()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->a = $a;
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->a->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithTernaryGuardWithThis()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var A|null */
|
2016-10-15 19:10:05 +02:00
|
|
|
public $a;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->a = $a;
|
2016-12-30 19:09:00 +01:00
|
|
|
$b = $this->a ? $this->a->fooFoo() : null;
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithTernaryIfNullGuardWithThis()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var A|null */
|
2016-10-15 19:10:05 +02:00
|
|
|
public $a;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->a = $a;
|
2016-12-30 19:09:00 +01:00
|
|
|
$b = $this->a === null ? null : $this->a->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithIfGuardWithThis()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var A|null */
|
2016-10-15 19:10:05 +02:00
|
|
|
public $a;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->a = $a;
|
|
|
|
|
|
|
|
if ($this->a) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->a->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongIfGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$two->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-09 14:56:07 +02:00
|
|
|
public function testNullableMethodWithExceptionThrown()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-05-09 14:56:07 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-05-09 14:56:07 +02:00
|
|
|
if (!$one) {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-09 14:56:07 +02:00
|
|
|
public function testNullableMethodWithRedefinitionAndElse()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-05-09 14:56:07 +02:00
|
|
|
class One {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var int|null */
|
2016-10-15 19:10:05 +02:00
|
|
|
public $two;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-05-09 14:56:07 +02:00
|
|
|
if (!$one) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one->two = 3;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongBooleanIfGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one || $two) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$two->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithBooleanIfGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one && $two) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$two->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithNonNullBooleanIfGuard()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
if ($one !== null && $two) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithNonNullBooleanIfGuardAndBooleanAnd()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
if ($one !== null && ($two || 1 + 1 === 3)) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodInConditionWithIfGuardBefore()
|
2016-04-04 06:17:19 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var string */
|
2016-04-04 22:33:26 +02:00
|
|
|
public $a;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
if (!$one->a && $one->fooFoo()) {
|
2016-04-04 22:33:26 +02:00
|
|
|
// do something
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongIfGuardBefore()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithBooleanIfGuardBefore()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one === null || $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongBooleanIfGuardBefore()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one === null && $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithGuardedRedefinition()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one === null) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-16 07:19:52 +02:00
|
|
|
public function testNullableMethodWithGuardedRedefinitionInElse()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-16 07:19:52 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-16 07:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-06-16 07:19:52 +02:00
|
|
|
if ($one) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-06-16 07:19:52 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-16 07:19:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-20 07:05:44 +02:00
|
|
|
*/
|
2016-06-17 00:52:12 +02:00
|
|
|
public function testMethodWithMeaninglessCheck()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-17 00:52:12 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one) {
|
2016-06-17 00:52:12 +02:00
|
|
|
if (!$one) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithGuardedNestedIncompleteRedefinition()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinition()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 20:29:02 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchRedefinition()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 20:29:02 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 20:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 20:29:02 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 20:29:02 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 20:29:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:15:51 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-30 00:15:51 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithGuardedSwitchRedefinitionNoDefault()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-30 00:15:51 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-06-30 00:15:51 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-30 00:15:51 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithGuardedSwitchRedefinitionEmptyDefault()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-30 00:15:51 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-06-30 00:15:51 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-30 00:15:51 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchRedefinitionDueToException()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-30 00:15:51 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-10-15 19:10:05 +02:00
|
|
|
/**
|
2016-11-11 23:13:24 +01:00
|
|
|
* @return void
|
2016-10-15 19:10:05 +02:00
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-06-30 00:15:51 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-08 00:10:01 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchThatAlwaysReturns()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-07-08 00:10:01 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-07-08 00:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-07-08 00:10:01 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-07-08 00:10:01 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-07-08 00:10:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithReturn()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithElseReturn()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 22:33:26 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 22:33:26 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithUselessElseReturn()
|
2016-04-04 06:17:19 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 06:17:19 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
2016-04-04 22:33:26 +02:00
|
|
|
else if ($a === 3) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|
2016-04-04 07:21:28 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithElseifReturn()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 22:33:26 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 22:33:26 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else if ($a === 3) {
|
|
|
|
// do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-27 00:18:05 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchBreak()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-27 00:18:05 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-27 00:18:05 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
if ($one === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->fooFoo();
|
2016-04-27 00:18:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithGuardedRedefinitionOnThis()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-04 07:21:28 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var One|null */
|
2016-10-15 19:10:05 +02:00
|
|
|
public $one;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(One $one = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->one = $one;
|
|
|
|
|
|
|
|
if ($this->one === null) {
|
|
|
|
$this->one = new One();
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->one->fooFoo();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-04 07:21:28 +02:00
|
|
|
}
|
2016-04-12 17:28:36 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-12 17:28:36 +02:00
|
|
|
public function testArrayUnionTypeAssertion()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 17:28:36 +02:00
|
|
|
/** @var array|null */
|
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
|
|
|
|
|
|
|
if ($ids === null) {
|
|
|
|
$ids = [];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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('array<empty, empty>', (string) $context->vars_in_scope['$ids']);
|
2016-04-12 17:28:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-12 17:28:36 +02:00
|
|
|
public function testArrayUnionTypeAssertionWithIsArray()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 17:28:36 +02:00
|
|
|
/** @var array|null */
|
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
|
|
|
|
|
|
|
if (!is_array($ids)) {
|
|
|
|
$ids = [];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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('array<empty, empty>', (string) $context->vars_in_scope['$ids']);
|
2016-04-12 17:28:36 +02:00
|
|
|
}
|
2016-04-12 22:12:38 +02:00
|
|
|
|
2017-01-19 23:45:42 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test2DArrayUnionTypeAssertionWithIsArray()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @return array<array<string>>|null */
|
|
|
|
function foo() {
|
|
|
|
$ids = rand(0, 1) ? [["hello"]] : null;
|
|
|
|
|
|
|
|
if (is_array($ids)) {
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-12 22:12:38 +02:00
|
|
|
public function testVariableReassignment()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 22:12:38 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
$one = new Two();
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->barBar();
|
2016-04-12 22:12:38 +02:00
|
|
|
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-12 22:12:38 +02:00
|
|
|
public function testVariableReassignmentInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 22:12:38 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->barBar();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-04-12 22:12:38 +02:00
|
|
|
*/
|
|
|
|
public function testVariableReassignmentInIfWithOutsideCall()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 22:12:38 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->barBar();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$one->barBar();
|
2016-04-12 22:12:38 +02:00
|
|
|
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-12 22:12:38 +02:00
|
|
|
public function testUnionTypeFlow()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-04-12 22:12:38 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Three {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-04-12 22:12:38 +02:00
|
|
|
public function baz() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var One|Two|Three|null */
|
|
|
|
$var = null;
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->fooFoo();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($var instanceof Two) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->barBar();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
|
|
|
else if ($var) {
|
|
|
|
$var->baz();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-04-12 22:12:38 +02:00
|
|
|
}
|
2016-04-27 00:18:05 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-23 06:35:22 +02:00
|
|
|
public function testUnionTypeFlowWithThrow()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-23 06:35:22 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-10-15 19:10:05 +02:00
|
|
|
function a(One $var = null) {
|
|
|
|
if (!$var) {
|
|
|
|
throw new \Exception("some exception");
|
|
|
|
}
|
|
|
|
else {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->fooFoo();
|
2016-10-15 19:10:05 +02:00
|
|
|
}
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
2016-10-15 19:10:05 +02:00
|
|
|
|
2016-08-23 06:35:22 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-23 06:35:22 +02:00
|
|
|
public function testUnionTypeFlowWithElseif()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-23 06:35:22 +02:00
|
|
|
class One {
|
2016-12-07 20:13:39 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var One|null */
|
|
|
|
$var = null;
|
|
|
|
|
|
|
|
if (rand(0,100) === 5) {
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif (!$var) {
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->fooFoo();
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 05:02:17 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-08-23 05:02:17 +02:00
|
|
|
*/
|
2016-06-13 07:48:29 +02:00
|
|
|
public function testUnnecessaryInstanceof()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-13 07:48:29 +02:00
|
|
|
class One {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$var = new One();
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->fooFoo();
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-13 07:48:29 +02:00
|
|
|
*/
|
|
|
|
public function testUnNegatableInstanceof()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-13 07:48:29 +02:00
|
|
|
class One {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {}
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$var = new One();
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$var->fooFoo();
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-13 07:48:29 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-27 00:18:05 +02:00
|
|
|
public function testTypeAdjustment()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
$var = 0;
|
2016-04-27 00:18:05 +02:00
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = "hello";
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
2016-06-20 07:05:44 +02:00
|
|
|
|
|
|
|
echo $var;
|
2016-04-27 00:18:05 +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-09 19:04:08 +01:00
|
|
|
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-30 20:14:22 +02:00
|
|
|
public function testTypeMixedAdjustment()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
$var = 0;
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
$arr = ["hello"];
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = $arr[0];
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
2016-06-20 07:05:44 +02:00
|
|
|
|
|
|
|
echo $var;
|
2016-04-30 20:14:22 +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-09 19:04:08 +01:00
|
|
|
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testTypeAdjustmentIfNull()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
$var = rand(0,10) > 5 ? new A : null;
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
if ($var === null) {
|
|
|
|
$var = new B;
|
|
|
|
}
|
|
|
|
');
|
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-09 19:04:08 +01:00
|
|
|
$this->assertEquals('A|B', (string) $context->vars_in_scope['$var']);
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-17 22:05:28 +02:00
|
|
|
public function testWhileTrue()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-17 22:05:28 +02:00
|
|
|
class One {
|
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(){
|
2016-06-17 22:05:28 +02:00
|
|
|
return rand(0,100) ? ["hello"] : false;
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(){
|
|
|
|
while ($row = $this->fooFoo()) {
|
2016-06-17 22:05:28 +02:00
|
|
|
$row[0] = "bad";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|
2016-06-20 07:05:44 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-20 07:05:44 +02:00
|
|
|
*/
|
|
|
|
public function testWrongParam()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a) {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->barBar(5);
|
2016-06-20 07:05:44 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testPassingParam()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a) {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->barBar(new A);
|
2016-06-20 07:05:44 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testNullToNullableParam()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->barBar(null);
|
2016-06-20 07:05:44 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-20 07:05:44 +02:00
|
|
|
*/
|
|
|
|
public function testIntToNullableObjectParam()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->barBar(5);
|
2016-06-20 07:05:44 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testObjectToNullableObjectParam()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar(A $a = null) {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->barBar(new A);
|
2016-06-20 07:05:44 +02:00
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-06-20 07:05:44 +02:00
|
|
|
*/
|
|
|
|
public function testParamCoercionWithBadArg()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-06-20 07:05:44 +02:00
|
|
|
public function blab() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(A $a) {
|
2016-06-20 07:05:44 +02:00
|
|
|
if ($a instanceof B) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testParamCoercion()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(A $a) {
|
2016-06-20 07:05:44 +02:00
|
|
|
if ($a instanceof B) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 07:05:44 +02:00
|
|
|
public function testParamElseifCoercion()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 07:05:44 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {}
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
class C extends A {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-06-20 07:05:44 +02:00
|
|
|
public function baz() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(A $a) {
|
2016-06-20 07:05:44 +02:00
|
|
|
if ($a instanceof B) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
|
|
|
elseif ($a instanceof C) {
|
|
|
|
$a->baz();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-06-20 07:05:44 +02:00
|
|
|
}
|
2016-08-23 06:35:22 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-23 06:35:22 +02:00
|
|
|
public function testAssignInsideForeach()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-23 06:35:22 +02:00
|
|
|
$b = false;
|
|
|
|
|
|
|
|
foreach ([1, 2, 3, 4] as $a) {
|
|
|
|
if ($a === rand(0, 10)) {
|
|
|
|
$b = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-08-23 06:35:22 +02:00
|
|
|
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-23 06:35:22 +02:00
|
|
|
public function testAssignInsideForeachWithBreak()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-23 06:35:22 +02:00
|
|
|
$b = false;
|
|
|
|
|
|
|
|
foreach ([1, 2, 3, 4] as $a) {
|
|
|
|
if ($a === rand(0, 10)) {
|
|
|
|
$b = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-08-23 06:35:22 +02:00
|
|
|
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
|
2016-08-23 06:35:22 +02:00
|
|
|
}
|
2016-08-24 06:30:55 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-08-24 06:30:55 +02:00
|
|
|
* @expectedExceptionMessage NullReference
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-08-24 06:30:55 +02:00
|
|
|
*/
|
|
|
|
public function testNullCheckInsideForeachWithNoLeaveStatement()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-24 06:30:55 +02:00
|
|
|
class A {
|
|
|
|
/** @return array<A|null> */
|
|
|
|
public static function loadMultiple()
|
|
|
|
{
|
|
|
|
return [new A, null];
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {
|
2016-08-24 06:30:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (A::loadMultiple() as $a) {
|
|
|
|
if ($a === null) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-08-24 06:30:55 +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();
|
2016-08-24 06:30:55 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-24 06:30:55 +02:00
|
|
|
public function testNullCheckInsideForeachWithContinue()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-24 06:30:55 +02:00
|
|
|
class A {
|
|
|
|
/** @return array<A|null> */
|
|
|
|
public static function loadMultiple()
|
|
|
|
{
|
|
|
|
return [new A, null];
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {
|
2016-08-24 06:30:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (A::loadMultiple() as $a) {
|
|
|
|
if ($a === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-08-24 06:30:55 +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();
|
2016-12-16 23:59:27 +01:00
|
|
|
}
|
2016-12-17 06:48:31 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 06:48:31 +01:00
|
|
|
public function testPlusPlus()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = 0;
|
|
|
|
$b = $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-17 06:48:31 +01:00
|
|
|
|
|
|
|
$this->assertSame('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
2017-01-07 20:35:07 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public function testTypedValueAssertion()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 20:35:07 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param array|string $a
|
|
|
|
*/
|
|
|
|
function fooFoo($a) : void {
|
|
|
|
$b = "aadad";
|
|
|
|
|
|
|
|
if ($a === $b) {
|
|
|
|
echo substr($a, 1);
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
2017-01-15 19:58:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIssetWithSimpleAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
if (isset($array[$a = 5])) {
|
|
|
|
print "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
print $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 19:58:20 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIssetWithMultipleAssignments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (rand(0, 4) > 2) {
|
|
|
|
$arr = [5 => [3 => "hello"]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($arr[$a = 5][$b = 3])) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-15 19:58:20 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|