mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Improve how we disable clauses
This commit is contained in:
parent
63a75108f5
commit
b6455a2566
@ -293,6 +293,10 @@ class IfChecker
|
||||
$statements_checker->getSuppressedIssues()
|
||||
);
|
||||
|
||||
foreach ($changed_vars as $changed_var) {
|
||||
$outer_context->removeVarFromClauses($changed_var);
|
||||
}
|
||||
|
||||
if ($outer_context_vars_reconciled === false) {
|
||||
return false;
|
||||
}
|
||||
@ -305,11 +309,16 @@ class IfChecker
|
||||
// We only update vars that changed both at the start of the if block and then again by an assignment
|
||||
// in the if statement.
|
||||
if ($if_scope->negated_types && !$mic_drop) {
|
||||
$vars_to_update = array_intersect(
|
||||
array_keys($pre_assignment_else_redefined_vars),
|
||||
array_keys($if_scope->negated_types)
|
||||
);
|
||||
|
||||
$outer_context->update(
|
||||
$old_if_context,
|
||||
$if_context,
|
||||
$has_leaving_statements,
|
||||
array_intersect(array_keys($pre_assignment_else_redefined_vars), array_keys($if_scope->negated_types)),
|
||||
$vars_to_update,
|
||||
$if_scope->updated_vars
|
||||
);
|
||||
}
|
||||
|
@ -840,8 +840,8 @@ class ScopeTest extends PHPUnit_Framework_TestCase
|
||||
$a = rand(0, 10) ? "hello" : null;
|
||||
$b = rand(0, 10) ? "goodbye" : null;
|
||||
|
||||
if ($a || $b) {
|
||||
if ($a) {
|
||||
if ($a !== null || $b !== null) {
|
||||
if ($a !== null) {
|
||||
$c = $a;
|
||||
} else {
|
||||
$c = $b;
|
||||
@ -862,10 +862,10 @@ class ScopeTest extends PHPUnit_Framework_TestCase
|
||||
$b = rand(0, 10) ? "goodbye" : null;
|
||||
$c = rand(0, 10) ? "hello" : null;
|
||||
|
||||
if ($a || $b || $c) {
|
||||
if ($a) {
|
||||
if ($a !== null || $b !== null || $c !== null) {
|
||||
if ($a !== null) {
|
||||
$d = $a;
|
||||
} elseif ($b) {
|
||||
} elseif ($b !== null) {
|
||||
$d = $b;
|
||||
} else {
|
||||
$d = $c;
|
||||
@ -881,7 +881,7 @@ class ScopeTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Psalm\Exception\CodeException
|
||||
* @expectedExceptionMessage InvalidScalarArgument
|
||||
* @expectedExceptionMessage NullArgument
|
||||
*/
|
||||
public function testThreeVarLogicWithChange()
|
||||
{
|
||||
@ -890,12 +890,44 @@ class ScopeTest extends PHPUnit_Framework_TestCase
|
||||
$b = rand(0, 10) ? "goodbye" : null;
|
||||
$c = rand(0, 10) ? "hello" : null;
|
||||
|
||||
if ($a || $b || $c) {
|
||||
$c = false;
|
||||
if ($a !== null || $b !== null || $c !== null) {
|
||||
$c = null;
|
||||
|
||||
if ($a) {
|
||||
if ($a !== null) {
|
||||
$d = $a;
|
||||
} elseif ($b) {
|
||||
} elseif ($b !== null) {
|
||||
$d = $b;
|
||||
} else {
|
||||
$d = $c;
|
||||
}
|
||||
|
||||
echo strpos($d, "e");
|
||||
}
|
||||
');
|
||||
|
||||
$file_checker = new FileChecker('somefile.php', $stmts);
|
||||
$file_checker->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Psalm\Exception\CodeException
|
||||
* @expectedExceptionMessage NullArgument
|
||||
*/
|
||||
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) {
|
||||
$d = $b;
|
||||
} else {
|
||||
$d = $c;
|
||||
|
Loading…
Reference in New Issue
Block a user