diff --git a/tests/LoopScopeTest.php b/tests/LoopScopeTest.php index 990e3b01f..ec3d995af 100644 --- a/tests/LoopScopeTest.php +++ b/tests/LoopScopeTest.php @@ -327,6 +327,9 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase $this->assertEquals('B', (string) $context->vars_in_scope['$a']); } + /** + * @return void + */ public function testSecondLoopWithNotNullCheck() { $stmts = self::$parser->parse('visitAndAnalyzeMethods(); } + /** + * @return void + */ public function testSecondLoopWithIntCheck() { $stmts = self::$parser->parse('visitAndAnalyzeMethods(); } + /** + * @return void + */ public function testSecondLoopWithIntCheckAndConditionalSet() { $stmts = self::$parser->parse('visitAndAnalyzeMethods(); } + /** + * @return void + */ + public function testSecondLoopWithIntCheckAndAssignmentsInIfAndElse() + { + $stmts = self::$parser->parse('project_checker, $stmts); + $file_checker->visitAndAnalyzeMethods(); + } + + /** + * @return void + */ public function testSecondLoopWithIntCheckAndLoopSet() { $stmts = self::$parser->parse('visitAndAnalyzeMethods(); } + /** + * @return void + */ public function testThirdLoopWithIntCheckAndLoopSet() { $stmts = self::$parser->parse('project_checker, $stmts); $file_checker->visitAndAnalyzeMethods(); } + + /** + * @return void + */ + public function testAssignInsideForeach() + { + $stmts = self::$parser->parse('project_checker, $stmts); + $context = new Context(); + $file_checker->visitAndAnalyzeMethods($context); + + $this->assertSame('bool', (string) $context->vars_in_scope['$b']); + } + + /** + * @return void + */ + public function testAssignInsideForeachWithBreak() + { + $stmts = self::$parser->parse('project_checker, $stmts); + $context = new Context(); + $file_checker->visitAndAnalyzeMethods($context); + + $this->assertSame('bool', (string) $context->vars_in_scope['$b']); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage PossiblyNullReference + * @return void + */ + public function testPossiblyNullCheckInsideForeachWithNoLeaveStatement() + { + $stmts = self::$parser->parse(' */ + public static function loadMultiple() + { + return [new A, null]; + } + + /** @return void */ + public function barBar() { + + } + } + + foreach (A::loadMultiple() as $a) { + if ($a === null) { + // do nothing + } + + $a->barBar(); + } + '); + + $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); + $file_checker->visitAndAnalyzeMethods(); + } + + /** + * @return void + */ + public function testNullCheckInsideForeachWithContinue() + { + $stmts = self::$parser->parse(' */ + public static function loadMultiple() + { + return [new A, null]; + } + + /** @return void */ + public function barBar() { + + } + } + + foreach (A::loadMultiple() as $a) { + if ($a === null) { + continue; + } + + $a->barBar(); + } + '); + + $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); + $file_checker->visitAndAnalyzeMethods(); + } } diff --git a/tests/TypeReconciliationTest.php b/tests/TypeReconciliationTest.php index fb3c257f3..fc8d48dac 100644 --- a/tests/TypeReconciliationTest.php +++ b/tests/TypeReconciliationTest.php @@ -16,9 +16,6 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase /** @var \PhpParser\Parser */ protected static $parser; - /** @var TestConfig */ - protected static $config; - /** @var \Psalm\Checker\ProjectChecker */ protected $project_checker; @@ -31,7 +28,6 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - self::$config = new TestConfig(); } /** @@ -42,7 +38,7 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase FileChecker::clearCache(); $this->project_checker = new ProjectChecker(); - $this->project_checker->setConfig(self::$config); + $this->project_checker->setConfig(new TestConfig()); $this->file_checker = new FileChecker('somefile.php', $this->project_checker); $this->file_checker->context = new Context(); @@ -886,4 +882,40 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); $file_checker->visitAndAnalyzeMethods(); } + + /** + * @return void + */ + public function testIgnoreNullCheckAndMaintainNullValue() + { + Config::getInstance()->setCustomErrorLevel('FailedTypeResolution', Config::REPORT_SUPPRESS); + + $stmts = self::$parser->parse('project_checker, $stmts); + $context = new Context(); + $file_checker->visitAndAnalyzeMethods($context); + $this->assertEquals('null', (string) $context->vars_in_scope['$b']); + } + + /** + * @return void + */ + public function testIgnoreNullCheckAndMaintainNullableValue() + { + $stmts = self::$parser->parse('project_checker, $stmts); + $context = new Context(); + $file_checker->visitAndAnalyzeMethods($context); + $this->assertEquals('int|null', (string) $context->vars_in_scope['$b']); + } } diff --git a/tests/TypeTest.php b/tests/TypeTest.php index cd6d4a4d2..aa0ed7697 100644 --- a/tests/TypeTest.php +++ b/tests/TypeTest.php @@ -1753,85 +1753,6 @@ class TypeTest extends PHPUnit_Framework_TestCase $file_checker->visitAndAnalyzeMethods(); } - /** - * @return void - */ - public function testAssignInsideForeach() - { - $stmts = self::$parser->parse('project_checker, $stmts); - $context = new Context(); - $file_checker->visitAndAnalyzeMethods($context); - - $this->assertSame('bool', (string) $context->vars_in_scope['$b']); - } - - /** - * @return void - */ - public function testAssignInsideForeachWithBreak() - { - $stmts = self::$parser->parse('project_checker, $stmts); - $context = new Context(); - $file_checker->visitAndAnalyzeMethods($context); - - $this->assertSame('bool', (string) $context->vars_in_scope['$b']); - } - - /** - * @expectedException \Psalm\Exception\CodeException - * @expectedExceptionMessage PossiblyNullReference - * @return void - */ - public function testPossiblyNullCheckInsideForeachWithNoLeaveStatement() - { - $stmts = self::$parser->parse(' */ - public static function loadMultiple() - { - return [new A, null]; - } - - /** @return void */ - public function barBar() { - - } - } - - foreach (A::loadMultiple() as $a) { - if ($a === null) { - // do nothing - } - - $a->barBar(); - } - '); - - $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); - $file_checker->visitAndAnalyzeMethods(); - } - /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage NullReference @@ -1849,38 +1770,6 @@ class TypeTest extends PHPUnit_Framework_TestCase $file_checker->visitAndAnalyzeMethods(); } - /** - * @return void - */ - public function testNullCheckInsideForeachWithContinue() - { - $stmts = self::$parser->parse(' */ - public static function loadMultiple() - { - return [new A, null]; - } - - /** @return void */ - public function barBar() { - - } - } - - foreach (A::loadMultiple() as $a) { - if ($a === null) { - continue; - } - - $a->barBar(); - } - '); - - $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); - $file_checker->visitAndAnalyzeMethods(); - } - /** * @return void */