2016-04-04 01:47:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace CodeInspector\Tests;
|
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
use CodeInspector\Type;
|
|
|
|
|
2016-04-04 01:47:06 +02:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
|
|
|
|
class TypeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-04-04 06:17:19 +02:00
|
|
|
protected static $_parser;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
public function testReconciliation()
|
|
|
|
{
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('!null', ['Object']));
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('!null', ['Object', 'null']));
|
|
|
|
$this->assertEquals('Object|false', \CodeInspector\TypeChecker::reconcileTypes('!null', ['Object', 'false']));
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('!empty', ['Object']));
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('!empty', ['Object', 'null']));
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('!empty', ['Object', 'false']));
|
|
|
|
$this->assertEquals('null', \CodeInspector\TypeChecker::reconcileTypes('null', ['Object', 'null']));
|
|
|
|
$this->assertEquals('null', \CodeInspector\TypeChecker::reconcileTypes('null', ['Object']));
|
|
|
|
$this->assertEquals('null', \CodeInspector\TypeChecker::reconcileTypes('null', ['Object', 'false']));
|
|
|
|
$this->assertEquals('null', \CodeInspector\TypeChecker::reconcileTypes('empty', ['Object']));
|
|
|
|
$this->assertEquals('false', \CodeInspector\TypeChecker::reconcileTypes('empty', ['Object', 'false']));
|
|
|
|
$this->assertEquals('false', \CodeInspector\TypeChecker::reconcileTypes('empty', ['Object', 'bool']));
|
|
|
|
|
|
|
|
$this->assertEquals('bool', \CodeInspector\TypeChecker::reconcileTypes('!Object', ['Object', 'bool']));
|
|
|
|
$this->assertEquals('Object', \CodeInspector\TypeChecker::reconcileTypes('Object', ['Object', 'bool']));
|
|
|
|
$this->assertEquals('null', \CodeInspector\TypeChecker::reconcileTypes('!Object', ['Object', 'null']));
|
|
|
|
$this->assertEquals('ObjectA', \CodeInspector\TypeChecker::reconcileTypes('ObjectA', ['ObjectA', 'ObjectB']));
|
|
|
|
$this->assertEquals('ObjectB', \CodeInspector\TypeChecker::reconcileTypes('!ObjectA', ['ObjectA', 'ObjectB']));
|
|
|
|
|
|
|
|
$this->assertEquals('mixed', \CodeInspector\TypeChecker::reconcileTypes('!empty', ['mixed']));
|
|
|
|
$this->assertEquals('mixed', \CodeInspector\TypeChecker::reconcileTypes('!null', ['mixed']));
|
|
|
|
$this->assertEquals('mixed', \CodeInspector\TypeChecker::reconcileTypes('mixed', ['mixed']));
|
|
|
|
}
|
|
|
|
|
2016-04-04 01:47:06 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 01:47:06 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodCall()
|
|
|
|
{
|
2016-04-04 06:17:19 +02:00
|
|
|
$stmts = self::$_parser->parse('<?php
|
2016-04-04 01:47:06 +02:00
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:14:19 +02:00
|
|
|
public function testNullableMethodWithTernaryGuard()
|
|
|
|
{
|
2016-04-04 06:17:19 +02:00
|
|
|
$stmts = self::$_parser->parse('<?php
|
2016-04-04 02:14:19 +02:00
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$b = $a ? $a->foo() : null;
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 02:14:19 +02:00
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithTernaryIfNullGuard()
|
|
|
|
{
|
2016-04-04 06:17:19 +02:00
|
|
|
$stmts = self::$_parser->parse('<?php
|
2016-04-04 02:14:19 +02:00
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$b = $a === null ? null : $a->foo();
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
2016-04-04 02:14:19 +02:00
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithTernaryEmptyGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$b = empty($a) ? null : $a->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithTernaryIsNullGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$b = is_null($a) ? null : $a->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:14:19 +02:00
|
|
|
public function testNullableMethodWithIfGuard()
|
2016-04-04 01:47:06 +02:00
|
|
|
{
|
2016-04-04 06:17:19 +02:00
|
|
|
$stmts = self::$_parser->parse('<?php
|
2016-04-04 01:47:06 +02:00
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
if ($a) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 07:21:28 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 07:21:28 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodCallWithThis()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->a->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithTernaryGuardWithThis()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
$b = $this->a ? $this->a->foo() : null;
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithTernaryIfNullGuardWithThis()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
$b = $this->a === null ? null : $this->a->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithIfGuardWithThis()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
|
|
|
|
if ($this->a) {
|
|
|
|
$this->a->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongIfGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one) {
|
|
|
|
$two->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
public function testNullableMethodWithExceptionThrown()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null) {
|
|
|
|
if (!$one) {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithRedefinitionAndElse()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null) {
|
|
|
|
if (!$one) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one->two = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongBooleanIfGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one || $two) {
|
|
|
|
$two->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithBooleanIfGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one && $two) {
|
|
|
|
$two->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithNonNullBooleanIfGuard()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one !== null && $two) {
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithNonNullBooleanIfGuardAndBooleanAnd()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one !== null && ($two || 1 + 1 === 3)) {
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
|
|
|
|
public function testNullableMethodInConditionWithIfGuardBefore()
|
2016-04-04 06:17:19 +02:00
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
2016-04-04 22:33:26 +02:00
|
|
|
public $a;
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
if (!$one->a && $one->foo()) {
|
|
|
|
// do something
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongIfGuardBefore()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithBooleanIfGuardBefore()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null || $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithWrongBooleanIfGuardBefore()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null && $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithGuardedRedefinition()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(One $one = null) {
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($one === null) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:19:52 +02:00
|
|
|
public function testNullableMethodWithGuardedRedefinitionInElse()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(One $one = null) {
|
2016-06-16 07:19:52 +02:00
|
|
|
if ($one) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
public function testMethodWithMeaninglessCheck()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one) {
|
|
|
|
if (!$one) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
|
|
|
public function testNullableMethodWithGuardedNestedIncompleteRedefinition()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function bar(One $one = null, Two $two = null) {
|
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableMethodWithGuardedNestedRedefinition()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 20:29:02 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchRedefinition()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithElseReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
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
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
2016-04-04 01:47:06 +02:00
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-04-04 07:21:28 +02:00
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
public function testNullableMethodWithGuardedNestedRedefinitionWithElseifReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:18:05 +02:00
|
|
|
public function testNullableMethodWithGuardedSwitchBreak()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(One $one = null) {
|
2016-04-27 00:18:05 +02:00
|
|
|
$a = 4;
|
|
|
|
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
if ($one === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->foo();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-04 07:21:28 +02:00
|
|
|
public function testNullableMethodWithGuardedRedefinitionOnThis()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-06-17 00:52:12 +02:00
|
|
|
public function bar(One $one = null) {
|
2016-04-04 07:21:28 +02:00
|
|
|
$this->one = $one;
|
|
|
|
|
|
|
|
if ($this->one === null) {
|
|
|
|
$this->one = new One();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->one->foo();
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-04-12 17:28:36 +02:00
|
|
|
|
|
|
|
public function testArrayUnionTypeAssertion()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
/** @var array|null */
|
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
|
|
|
|
|
|
|
if ($ids === null) {
|
|
|
|
$ids = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayUnionTypeAssertionWithIsArray()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
/** @var array|null */
|
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
|
|
|
|
|
|
|
if (!is_array($ids)) {
|
|
|
|
$ids = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-04-12 22:12:38 +02:00
|
|
|
|
|
|
|
public function testVariableReassignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
$one = new Two();
|
|
|
|
|
|
|
|
$one->bar();
|
|
|
|
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testVariableReassignmentInIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
|
|
|
|
|
|
|
$one->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @expectedException CodeInspector\CodeException
|
2016-04-12 22:12:38 +02:00
|
|
|
*/
|
|
|
|
public function testVariableReassignmentInIfWithOutsideCall()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$one = new One();
|
|
|
|
|
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
|
|
|
|
|
|
|
$one->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
$one->bar();
|
|
|
|
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnionTypeFlow()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Three {
|
|
|
|
public function baz() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var One|Two|Three|null */
|
|
|
|
$var = null;
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
|
|
|
$var->foo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($var instanceof Two) {
|
|
|
|
$var->bar();
|
|
|
|
}
|
|
|
|
else if ($var) {
|
|
|
|
$var->baz();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-04-27 00:18:05 +02:00
|
|
|
|
2016-06-13 07:48:29 +02:00
|
|
|
public function testUnnecessaryInstanceof()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Three {
|
|
|
|
public function baz() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$var = new One();
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
|
|
|
$var->foo();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException CodeInspector\CodeException
|
|
|
|
*/
|
|
|
|
public function testUnNegatableInstanceof()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Two {
|
|
|
|
public function bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Three {
|
|
|
|
public function baz() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$var = new One();
|
|
|
|
|
|
|
|
if ($var instanceof One) {
|
|
|
|
$var->foo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:18:05 +02:00
|
|
|
public function testTypeAdjustment()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo(){
|
|
|
|
$var = 0;
|
|
|
|
|
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$stmts = $file_checker->check();
|
|
|
|
|
|
|
|
$method_stmts = $stmts[0]->stmts[0]->stmts;
|
|
|
|
|
|
|
|
$return_stmt = array_pop($method_stmts);
|
|
|
|
|
2016-06-16 18:04:55 +02:00
|
|
|
$this->assertSame('int|string', (string) $return_stmt->inferredType);
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTypeMixedAdjustment()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class One {
|
|
|
|
public function foo(){
|
|
|
|
$var = 0;
|
|
|
|
|
|
|
|
$arr = ["hello"];
|
|
|
|
|
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = $arr[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$stmts = $file_checker->check();
|
|
|
|
|
|
|
|
$method_stmts = $stmts[0]->stmts[0]->stmts;
|
|
|
|
|
|
|
|
$return_stmt = array_pop($method_stmts);
|
|
|
|
|
2016-06-16 18:04:55 +02:00
|
|
|
$this->assertSame('mixed', (string) $return_stmt->inferredType);
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
|
|
|
|
public function testSwitchVariableWithContinue()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
class B {
|
|
|
|
public function bar() {
|
|
|
|
foreach ([\'a\', \'b\', \'c\'] as $letter) {
|
|
|
|
switch ($letter) {
|
|
|
|
case \'a\':
|
|
|
|
$foo = 1;
|
|
|
|
break;
|
|
|
|
case \'b\':
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$moo = $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
|
|
|
|
public function testTryCatchVar()
|
|
|
|
{
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
$worked = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// something
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
$worked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($worked) {
|
|
|
|
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new \CodeInspector\FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
|
|
|
|
$conditional = $stmts[2]->cond;
|
|
|
|
|
|
|
|
$this->assertSame('bool', (string) $conditional->inferredType);
|
|
|
|
}
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|