2017-03-16 16:46:07 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class TypeAlgebraTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
protected static $parser;
|
|
|
|
|
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
self::$config = new TestConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
|
|
|
$this->project_checker->setConfig(self::$config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-03-18 06:34:29 +01:00
|
|
|
function takesString(string $s) : void {}
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
function foo(?string $a, ?string $b) : void {
|
|
|
|
if ($a !== null || $b !== null) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$c = $a;
|
|
|
|
} else {
|
|
|
|
$c = $b;
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
takesString($c);
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-03-18 06:34:29 +01:00
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
takesString($d);
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage NullArgument
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogicWithChange()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-03-18 06:34:29 +01:00
|
|
|
function takesString(string $s) : void {}
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
$c = null;
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
takesString($d);
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage NullArgument
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogicWithException()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-03-18 06:34:29 +01:00
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($c !== null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
takesString($d);
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
2017-03-18 06:34:29 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNested()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if (!$a && !$b) return "bad";
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithAllPathsReturning()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if (!$a && !$b) {
|
|
|
|
return "bad";
|
2017-03-16 16:46:07 +01:00
|
|
|
} else {
|
2017-03-18 06:34:29 +01:00
|
|
|
if (!$a) {
|
|
|
|
return $b;
|
|
|
|
} else {
|
|
|
|
return $a;
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
2017-03-18 06:34:29 +01:00
|
|
|
}
|
|
|
|
');
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2017-03-18 06:34:29 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithAssignmentBeforeReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if (!$a && !$b) {
|
|
|
|
$a = 5;
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) {
|
|
|
|
$a = 7;
|
|
|
|
return $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a;
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-03-18 06:34:29 +01:00
|
|
|
public function testInvertedTwoVarLogicNotNested()
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
2017-03-18 06:34:29 +01:00
|
|
|
if ($a || $b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:46:07 +01:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-16 19:45:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-03-18 06:34:29 +01:00
|
|
|
public function testInvertedTwoVarLogicNotNestedWithAssignmentBeforeReturn()
|
2017-03-16 19:45:45 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a || $b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
2017-03-18 06:34:29 +01:00
|
|
|
$a = 5;
|
2017-03-16 19:45:45 +01:00
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-03-18 17:18:17 +01:00
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
2017-03-16 19:45:45 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvertedTwoVarLogicNotNestedWithVarChange()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a || $b) {
|
|
|
|
$b = null;
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-03-18 17:18:17 +01:00
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
2017-03-16 19:45:45 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvertedTwoVarLogicNotNestedWithElseif()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
2017-03-18 06:34:29 +01:00
|
|
|
if (rand(0, 1)) {
|
2017-03-16 19:45:45 +01:00
|
|
|
// do nothing
|
|
|
|
} elseif ($a || $b) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-18 17:21:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithElseif()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
2017-03-18 19:04:26 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogicNotNested()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogicNotNestedAndOr()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b || $c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testThreeVarLogicWithElseifAndAnd()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b && $c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
2017-03-18 17:21:52 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithElseifNegatedInIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
$a = null;
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithElseifCorrectlyNegatedInElseIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($b) {
|
|
|
|
$a = null;
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTwoVarLogicNotNestedWithElseifCorrectlyReinforcedInIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
$a = "hello";
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-04-02 21:26:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage ParadoxicalCondition
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRepeatedIfStatements()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @return string|null */
|
|
|
|
function foo(?string $a) {
|
|
|
|
if ($a) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage ParadoxicalCondition
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRepeatedConditionals()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(?string $a) : void {
|
|
|
|
if ($a) {
|
|
|
|
// do something
|
|
|
|
} elseif ($a) {
|
|
|
|
// can never get here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This shouldn't throw an error
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDifferentValueChecks()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(string $a) : void {
|
|
|
|
if ($a === "foo") {
|
|
|
|
// do something
|
|
|
|
} elseif ($a === "bar") {
|
|
|
|
// can never get here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This shouldn't throw an error
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRepeatedSet()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo() : void {
|
|
|
|
if ($a = rand(0, 1) ? "" : null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRepeatedSetInsideWhile()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo() : void {
|
|
|
|
if ($a = rand(0, 1) ? "" : null) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-04-02 23:37:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testByRefAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo() : void {
|
|
|
|
$matches = rand(0, 1) ? ["hello"] : null;
|
|
|
|
|
|
|
|
if (!$matches) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
preg_match("/hello/", "hello dolly", $matches);
|
|
|
|
|
|
|
|
if ($matches) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|