1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/ScopeTest.php

416 lines
10 KiB
PHP
Raw Normal View History

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
2016-07-26 00:37:44 +02:00
use Psalm\Type;
2016-06-20 22:18:47 +02:00
use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
class ScopeTest extends PHPUnit_Framework_TestCase
{
protected static $_parser;
2016-06-20 22:55:12 +02:00
protected static $_file_filter;
2016-06-20 22:18:47 +02:00
public static function setUpBeforeClass()
{
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-06-20 22:55:12 +02:00
2016-07-26 00:37:44 +02:00
$config = \Psalm\Config::getInstance();
$config->throw_exception = true;
2016-07-26 00:37:44 +02:00
$filter = new \Psalm\Config\FileFilter();
2016-06-20 22:55:12 +02:00
$filter->addExcludeFile('somefile.php');
$filter->makeExclusive();
self::$_file_filter = $filter;
2016-06-20 22:18:47 +02:00
}
public function setUp()
{
\Psalm\Checker\FileChecker::clearCache();
2016-07-26 00:37:44 +02:00
\Psalm\Config::getInstance()->setIssueHandler('PossiblyUndefinedVariable', null);
2016-06-20 22:18:47 +02:00
}
2016-09-22 18:26:24 +02:00
/**
* @expectedException Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $b, first seen on line 3
*/
public function testPossiblyUndefinedVarInIf()
{
$stmts = self::$_parser->parse('<?php
if (rand(0,100) === 10) {
$b = "s";
}
echo $b;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-06-20 22:18:47 +02:00
public function testNewVarInIf()
{
$stmts = self::$_parser->parse('<?php
if (rand(0,100) === 10) {
$badge = "hello";
}
else {
$badge = "goodbye";
}
echo $badge;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
public function testNewVarInIfWithElseReturn()
{
$stmts = self::$_parser->parse('<?php
if (rand(0,100) === 10) {
$badge = "hello";
}
else {
throw new \Exception();
}
echo $badge;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
/**
2016-07-26 00:37:44 +02:00
* @expectedException Psalm\Exception\CodeException
2016-06-20 22:18:47 +02:00
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first seen on line 3
*/
public function testPossiblyUndefinedArrayInIf()
{
$stmts = self::$_parser->parse('<?php
if (rand(0,100) === 10) {
$array[] = "hello";
}
echo $array;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
/**
2016-07-26 00:37:44 +02:00
* @expectedException Psalm\Exception\CodeException
2016-06-20 22:18:47 +02:00
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first seen on line 3
*/
public function testPossiblyUndefinedArrayInForeach()
{
$stmts = self::$_parser->parse('<?php
foreach ([1, 2, 3, 4] as $b) {
$array[] = "hello";
}
echo $array;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
/**
2016-07-26 00:37:44 +02:00
* @expectedException Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:4 - Possibly undefined variable $array, first seen on line 4
*/
2016-06-20 22:18:47 +02:00
public function testPossiblyUndefinedArrayInWhileAndForeach()
{
$stmts = self::$_parser->parse('<?php
for ($i = 0; $i < 4; $i++) {
while (rand(0,10) === 5) {
$array[] = "hello";
}
}
echo $array;
');
2016-07-26 00:37:44 +02:00
\Psalm\Config::getInstance()->setIssueHandler('PossiblyUndefinedVariable', self::$_file_filter);
2016-06-20 22:18:47 +02:00
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
/**
2016-07-26 00:37:44 +02:00
* @expectedException Psalm\Exception\CodeException
2016-06-20 22:18:47 +02:00
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $car, first seen on line 3
*/
public function testPossiblyUndefinedVariableInForeach()
{
$stmts = self::$_parser->parse('<?php
foreach ([1, 2, 3, 4] as $b) {
$car = "Volvo";
}
echo $car;
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
public function testSwitchVariableWithContinue()
{
$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;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-06-20 22:18:47 +02:00
2016-08-24 06:30:55 +02:00
public function testSwitchVariableWithContinueAndIfs()
{
$stmts = self::$_parser->parse('<?php
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
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
2016-08-30 06:04:54 +02:00
public function testSwitchVariableWithFallthrough()
{
$stmts = self::$_parser->parse('<?php
foreach ([\'a\', \'b\', \'c\'] as $letter) {
switch ($letter) {
case \'a\':
case \'b\':
$foo = 2;
break;
default:
$foo = 3;
break;
}
$moo = $foo;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testSwitchVariableWithFallthroughStatement()
{
$stmts = self::$_parser->parse('<?php
foreach ([\'a\', \'b\', \'c\'] as $letter) {
switch ($letter) {
case \'a\':
$bar = 1;
case \'b\':
$foo = 2;
break;
default:
$foo = 3;
break;
}
$moo = $foo;
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-06-20 22:18:47 +02:00
public function testTryCatchVar()
{
$stmts = self::$_parser->parse('<?php
try {
$worked = true;
}
catch (\Exception $e) {
$worked = false;
}
if ($worked) {
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
$conditional = $stmts[1]->cond;
$this->assertSame('bool', (string) $conditional->inferredType);
}
public function testWhileVar()
{
$stmts = self::$_parser->parse('<?php
$worked = false;
while (rand(0,100) === 10) {
$worked = true;
}
if ($worked) {
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
$conditional = $stmts[2]->cond;
$this->assertSame('bool', (string) $conditional->inferredType);
}
public function testDoWhileVar()
{
$stmts = self::$_parser->parse('<?php
$worked = false;
do {
$worked = true;
}
while (rand(0,100) === 10);
if ($worked) {
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
$conditional = $stmts[2]->cond;
$this->assertSame('bool', (string) $conditional->inferredType);
}
public function testAssignmentInIf()
{
$stmts = self::$_parser->parse('<?php
class A {
public static function bar() {
return true;
}
public function baz() {
if (!($a = A::bar())) {
return;
}
echo $a;
}
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
public function testPassByRefInIf()
{
$stmts = self::$_parser->parse('<?php
if (preg_match("/bad/", "badger", $matches)) {
echo $matches[0];
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
2016-10-02 04:47:50 +02:00
public function testPassByRefInIfCheckAfter()
{
$stmts = self::$_parser->parse('<?php
if (!preg_match("/bad/", "badger", $matches)) {
exit();
}
echo $matches[0];
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-06-20 22:18:47 +02:00
public function testPassByRefInIfWithBoolean()
{
$stmts = self::$_parser->parse('<?php
$a = true;
if ($a && preg_match("/bad/", "badger", $matches)) {
echo $matches[0];
}
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:18:47 +02:00
$file_checker->check();
}
2016-06-20 22:55:12 +02:00
/**
2016-07-26 00:37:44 +02:00
* @expectedException Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:9 - Possibly undefined variable $a, first seen on line 4
*/
2016-06-20 22:55:12 +02:00
public function testPossiblyUndefinedVariableInForeachAndIf()
{
$stmts = self::$_parser->parse('<?php
foreach ([1,2,3,4] as $i) {
if ($i === 1) {
$a = true;
break;
}
}
echo $a;
');
2016-07-26 00:37:44 +02:00
\Psalm\Config::getInstance()->setIssueHandler('PossiblyUndefinedVariable', self::$_file_filter);
2016-06-20 22:55:12 +02:00
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
2016-06-20 22:55:12 +02:00
$file_checker->check();
}
2016-06-20 22:18:47 +02:00
}