1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add tests for things we do not want to emit errors

This commit is contained in:
Matt Brown 2017-03-13 11:22:42 -04:00
parent a7da88bad3
commit 7a652ee4bf

View File

@ -326,4 +326,82 @@ class LoopScopeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('B', (string) $context->vars_in_scope['$a']);
}
public function testSecondLoopWithNotNullCheck()
{
$stmts = self::$parser->parse('<?php
/** @return void **/
function takesInt(int $i) {}
$a = null;
foreach ([1, 2, 3] as $i) {
if ($a !== null) takesInt($a);
$a = $i;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
public function testSecondLoopWithIntCheck()
{
$stmts = self::$parser->parse('<?php
/** @return void **/
function takesInt(int $i) {}
$a = null;
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
$a = $i;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
public function testSecondLoopWithIntCheckAndConditionalSet()
{
$stmts = self::$parser->parse('<?php
/** @return void **/
function takesInt(int $i) {}
$a = null;
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
if (rand(0, 1)) {
$a = $i;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
public function testSecondLoopWithIntCheckAndLoopSet()
{
$stmts = self::$parser->parse('<?php
/** @return void **/
function takesInt(int $i) {}
$a = null;
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
while (rand(0, 1)) {
$a = $i;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
}