2016-06-20 22:18:47 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-06-20 22:18:47 +02:00
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
2016-12-17 06:48:31 +01:00
|
|
|
use Psalm\Context;
|
2016-06-20 22:18:47 +02:00
|
|
|
|
|
|
|
class ScopeTest 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;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
2016-06-20 22:18:47 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-06-20 22:55:12 +02:00
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 18:26:24 +02:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $b, first seen on line 3
|
|
|
|
* @return void
|
2016-09-22 18:26:24 +02:00
|
|
|
*/
|
|
|
|
public function testPossiblyUndefinedVarInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-22 18:26:24 +02:00
|
|
|
if (rand(0,100) === 10) {
|
|
|
|
$b = "s";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $b;
|
|
|
|
');
|
|
|
|
|
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-09-22 18:26:24 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testNewVarInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
if (rand(0,100) === 10) {
|
|
|
|
$badge = "hello";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$badge = "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $badge;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testNewVarInIfWithElseReturn()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
if (rand(0,100) === 10) {
|
|
|
|
$badge = "hello";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $badge;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first seen on line 3
|
|
|
|
* @return void
|
2016-06-20 22:18:47 +02:00
|
|
|
*/
|
|
|
|
public function testPossiblyUndefinedArrayInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
if (rand(0,100) === 10) {
|
|
|
|
$array[] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $array;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first seen on line 3
|
|
|
|
* @return void
|
2016-06-20 22:18:47 +02:00
|
|
|
*/
|
|
|
|
public function testPossiblyUndefinedArrayInForeach()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$array[] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $array;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:4 - Possibly undefined variable $array, first seen on line 4
|
|
|
|
* @return void
|
2016-06-27 04:40:57 +02:00
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testPossiblyUndefinedArrayInWhileAndForeach()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
while (rand(0,10) === 5) {
|
|
|
|
$array[] = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $array;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $car, first seen on line 3
|
|
|
|
* @return void
|
2016-06-20 22:18:47 +02:00
|
|
|
*/
|
|
|
|
public function testPossiblyUndefinedVariableInForeach()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$car = "Volvo";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $car;
|
|
|
|
');
|
|
|
|
|
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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testSwitchVariableWithContinue()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-24 06:30:55 +02:00
|
|
|
foreach ([\'a\', \'b\', \'c\'] as $letter) {
|
|
|
|
switch ($letter) {
|
|
|
|
case \'a\':
|
|
|
|
$foo = 1;
|
|
|
|
break;
|
|
|
|
case \'b\':
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$moo = $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-24 06:30:55 +02:00
|
|
|
}
|
2016-06-20 22:18:47 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-24 06:30:55 +02:00
|
|
|
public function testSwitchVariableWithContinueAndIfs()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-24 06:30:55 +02:00
|
|
|
foreach ([\'a\', \'b\', \'c\'] as $letter) {
|
|
|
|
switch ($letter) {
|
|
|
|
case \'a\':
|
|
|
|
if (rand(0, 10) === 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$foo = 1;
|
|
|
|
break;
|
|
|
|
case \'b\':
|
|
|
|
if (rand(0, 10) === 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|
2016-08-24 06:30:55 +02:00
|
|
|
|
|
|
|
$moo = $foo;
|
|
|
|
}
|
|
|
|
');
|
2016-06-20 22:18:47 +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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-30 06:04:54 +02:00
|
|
|
public function testSwitchVariableWithFallthrough()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-30 06:04:54 +02:00
|
|
|
foreach ([\'a\', \'b\', \'c\'] as $letter) {
|
|
|
|
switch ($letter) {
|
|
|
|
case \'a\':
|
|
|
|
case \'b\':
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$foo = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$moo = $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-30 06:04:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-30 06:04:54 +02:00
|
|
|
public function testSwitchVariableWithFallthroughStatement()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-08-30 06:04:54 +02:00
|
|
|
foreach ([\'a\', \'b\', \'c\'] as $letter) {
|
|
|
|
switch ($letter) {
|
|
|
|
case \'a\':
|
|
|
|
$bar = 1;
|
|
|
|
|
|
|
|
case \'b\':
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$foo = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$moo = $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-30 06:04:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testTryCatchVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
try {
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
$worked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
');
|
|
|
|
|
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->assertEquals('bool', (string) $context->vars_in_scope['$worked']);
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testWhileVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
|
|
|
|
$worked = false;
|
|
|
|
|
|
|
|
while (rand(0,100) === 10) {
|
|
|
|
$worked = 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-12-17 06:48:31 +01:00
|
|
|
$this->assertEquals('bool', (string) $context->vars_in_scope['$worked']);
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testDoWhileVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
$worked = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
while (rand(0,100) === 10);
|
|
|
|
');
|
|
|
|
|
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->assertEquals('bool', (string) $context->vars_in_scope['$worked']);
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testAssignmentInIf()
|
|
|
|
{
|
2016-12-09 19:26:40 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if ($row = (rand(0, 10) ? [5] : null)) {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-09 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-09 19:26:40 +01:00
|
|
|
public function testNegatedAssignmentInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-09 19:04:08 +01:00
|
|
|
if (!($row = (rand(0, 10) ? [5] : null))) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
');
|
2016-06-20 22:18:47 +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-12-09 19:04:08 +01:00
|
|
|
}
|
2016-06-20 22:18:47 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-09 19:04:08 +01:00
|
|
|
public function testAssignInElseIf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (rand(0, 10) > 5) {
|
|
|
|
echo "hello";
|
|
|
|
} elseif ($row = (rand(0, 10) ? [5] : null)) {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-09 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-28 22:29:50 +01:00
|
|
|
public function testIfNotEqualsFalse()
|
2016-12-09 19:04:08 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-28 23:04:03 +01:00
|
|
|
if (($row = rand(0,10) ? [1] : false) !== false) {
|
2016-12-09 19:04:08 +01:00
|
|
|
echo $row[0];
|
2016-06-20 22:18:47 +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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-28 23:04:03 +01:00
|
|
|
public function testIfNotEqualsNull()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (($row = rand(0,10) ? [1] : null) !== null) {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-28 23:04:03 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-28 23:04:03 +01:00
|
|
|
public function testIfNullNotEquals()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (null !== ($row = rand(0,10) ? [1] : null)) {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-28 23:04:03 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-28 23:04:03 +01:00
|
|
|
public function testIfNullEquals()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
if (null === ($row = rand(0,10) ? [1] : null)) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
echo $row[0];
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-28 23:04:03 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testPassByRefInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
if (preg_match("/bad/", "badger", $matches)) {
|
2016-12-09 19:04:08 +01:00
|
|
|
echo (string)$matches[0];
|
2016-06-20 22:18:47 +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 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-02 04:47:50 +02:00
|
|
|
public function testPassByRefInIfCheckAfter()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-02 04:47:50 +02:00
|
|
|
if (!preg_match("/bad/", "badger", $matches)) {
|
|
|
|
exit();
|
|
|
|
}
|
2016-12-09 19:04:08 +01:00
|
|
|
echo (string)$matches[0];
|
2016-10-02 04:47:50 +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-10-02 04:47:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-06-20 22:18:47 +02:00
|
|
|
public function testPassByRefInIfWithBoolean()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:18:47 +02:00
|
|
|
$a = true;
|
|
|
|
if ($a && preg_match("/bad/", "badger", $matches)) {
|
2016-12-09 19:04:08 +01:00
|
|
|
echo (string)$matches[0];
|
2016-06-20 22:18:47 +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 22:18:47 +02:00
|
|
|
}
|
2016-06-20 22:55:12 +02:00
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:9 - Possibly undefined variable $a, first seen on line 4
|
|
|
|
* @return void
|
2016-06-27 04:40:57 +02:00
|
|
|
*/
|
2016-06-20 22:55:12 +02:00
|
|
|
public function testPossiblyUndefinedVariableInForeachAndIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-06-20 22:55:12 +02:00
|
|
|
foreach ([1,2,3,4] as $i) {
|
|
|
|
if ($i === 1) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
');
|
|
|
|
|
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 22:55:12 +02:00
|
|
|
}
|
2016-10-18 22:14:52 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:14:52 +02:00
|
|
|
public function testFunctionExists()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-18 22:14:52 +02:00
|
|
|
if (true && function_exists("flabble")) {
|
|
|
|
flabble();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-10-18 22:14:52 +02:00
|
|
|
}
|
2016-10-23 18:24:53 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-23 18:24:53 +02:00
|
|
|
public function testNestedPropertyFetchInElseif()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-23 18:24:53 +02:00
|
|
|
class A {
|
|
|
|
/** @var A|null */
|
|
|
|
public $foo;
|
2016-12-09 19:04:08 +01:00
|
|
|
|
|
|
|
public function __toString() : string {
|
|
|
|
return "boop";
|
|
|
|
}
|
2016-10-23 18:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) === 5 ? new A() : null;
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif ($a && $a->foo) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-10-23 18:24:53 +02:00
|
|
|
}
|
2016-11-05 22:53:30 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-05 22:53:30 +01:00
|
|
|
public function testGlobalReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$foo = "foo";
|
|
|
|
|
|
|
|
function a() : string {
|
|
|
|
global $foo;
|
|
|
|
|
|
|
|
return $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-11-05 22:53:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-05 22:53:30 +01:00
|
|
|
public function testStatic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function a() : string {
|
|
|
|
static $foo = "foo";
|
|
|
|
|
|
|
|
return $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-11-05 22:53:30 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 05:41:11 +01:00
|
|
|
* @expectedExceptionMessage InvalidGlobal
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
|
|
|
public function testInvalidGlobal()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = "heli";
|
|
|
|
|
|
|
|
global $a;
|
|
|
|
');
|
|
|
|
|
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-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 05:41:11 +01:00
|
|
|
* @expectedExceptionMessage InvalidStaticVariable
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
|
|
|
public function testThisInStatic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public static function fooFoo() {
|
2016-12-12 05:41:11 +01:00
|
|
|
echo $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-12 05:41:11 +01:00
|
|
|
}
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testTwoVarLogic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
$b = rand(0, 10) ? "goodbye" : null;
|
|
|
|
|
2016-12-29 00:55:16 +01:00
|
|
|
if ($a !== null || $b !== null) {
|
|
|
|
if ($a !== null) {
|
2016-12-27 19:58:58 +01:00
|
|
|
$c = $a;
|
|
|
|
} else {
|
|
|
|
$c = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo strpos($c, "e");
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-27 19:58:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testThreeVarLogic()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
$b = rand(0, 10) ? "goodbye" : null;
|
|
|
|
$c = rand(0, 10) ? "hello" : null;
|
|
|
|
|
2016-12-29 00:55:16 +01:00
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($a !== null) {
|
2016-12-27 19:58:58 +01:00
|
|
|
$d = $a;
|
2016-12-29 00:55:16 +01:00
|
|
|
} elseif ($b !== null) {
|
2016-12-27 19:58:58 +01:00
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo strpos($d, "e");
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-27 19:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-29 00:55:16 +01:00
|
|
|
* @expectedExceptionMessage NullArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-27 19:58:58 +01:00
|
|
|
*/
|
|
|
|
public function testThreeVarLogicWithChange()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
$b = rand(0, 10) ? "goodbye" : null;
|
|
|
|
$c = rand(0, 10) ? "hello" : null;
|
|
|
|
|
2016-12-29 00:55:16 +01:00
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
$c = null;
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-12-29 00:55:16 +01:00
|
|
|
if ($a !== null) {
|
2016-12-27 19:58:58 +01:00
|
|
|
$d = $a;
|
2016-12-29 00:55:16 +01:00
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo strpos($d, "e");
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-29 00:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-29 00:55:16 +01:00
|
|
|
* @expectedExceptionMessage NullArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-29 00:55:16 +01:00
|
|
|
*/
|
|
|
|
public function testThreeVarLogicWithException()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
$b = rand(0, 10) ? "goodbye" : null;
|
|
|
|
$c = rand(0, 10) ? "hello" : null;
|
|
|
|
|
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($c !== null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
2016-12-27 19:58:58 +01:00
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo strpos($d, "e");
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12-27 19:58:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testNegateAssertionAndOther()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
|
|
|
|
if (rand(0, 10) > 1 && is_string($a)) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-27 19:58:58 +01:00
|
|
|
$this->assertEquals('string|null', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-28 20:53:49 +01:00
|
|
|
public function testRepeatAssertionWithOther()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = rand(0, 10) ? "hello" : null;
|
|
|
|
|
|
|
|
if (rand(0, 10) > 1 || is_string($a)) {
|
|
|
|
if (is_string($a)) {
|
|
|
|
echo strpos("e", $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-28 20:53:49 +01:00
|
|
|
$this->assertEquals('string|null', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-27 19:58:58 +01:00
|
|
|
public function testRefineORedType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function doThing() : void
|
|
|
|
{
|
|
|
|
if ($this instanceof B || $this instanceof C) {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends 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-27 19:58:58 +01:00
|
|
|
}
|
2017-01-15 07:16:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIntstanceOfSubtraction()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {}
|
|
|
|
class FooBar extends Foo{}
|
|
|
|
class FooBarBat extends FooBar{}
|
|
|
|
class FooMoo extends Foo{}
|
|
|
|
|
|
|
|
$a = new Foo();
|
|
|
|
|
|
|
|
if ($a instanceof FooBar && !$a instanceof FooBarBat) {
|
|
|
|
|
|
|
|
} elseif ($a instanceof FooMoo) {
|
|
|
|
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
2016-06-20 22:18:47 +02:00
|
|
|
}
|