Skip to content
Permalink
Browse files

Fix #2081 - allow Assert::true to replace assert() calls

  • Loading branch information...
muglug committed Aug 29, 2019
1 parent 1f0aca0 commit 22ba3ada7b12665a1b1b66197edc737b37c35be6
Showing with 58 additions and 7 deletions.
  1. +19 −7 src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php
  2. +39 −0 tests/AssertTest.php
@@ -3285,13 +3285,25 @@ protected static function applyAssertionsToContext(
} else {
$type_assertions[$assertion_var_id] = $assertion->rule;
}
} elseif ($arg_value && $assertion->rule === [['!falsy']]) {
$assert_clauses = \Psalm\Type\Algebra::getFormula(
$arg_value,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
$statements_analyzer->getCodebase()
);
} elseif ($arg_value && ($assertion->rule === [['!falsy']] || $assertion->rule === [['true']])) {
if ($assertion->rule === [['true']]) {
$assert_clauses = \Psalm\Type\Algebra::getFormula(
new PhpParser\Node\Expr\BinaryOp\Identical(
$arg_value,
new PhpParser\Node\Expr\ConstFetch(new PhpParser\Node\Name('true'))
),
$statements_analyzer->getFQCLN(),
$statements_analyzer,
$statements_analyzer->getCodebase()
);
} else {
$assert_clauses = \Psalm\Type\Algebra::getFormula(
$arg_value,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
$statements_analyzer->getCodebase()
);
}
$simplified_clauses = \Psalm\Type\Algebra::simplifyCNF(
array_merge($context->clauses, $assert_clauses)
@@ -51,6 +51,25 @@ function bar(?string $s) : string {
return $s;
}',
],
'sortOfReplacementForAssert' => [
'<?php
namespace Bar;
/**
* @param mixed $_b
* @psalm-assert true $_b
*/
function myAssert($_b) : void {
if ($_b !== true) {
throw new \Exception("bad");
}
}
function bar(?string $s) : string {
myAssert($s !== null);
return $s;
}',
],
'assertInstanceOfInterface' => [
'<?php
namespace Bar;
@@ -816,6 +835,26 @@ function assertFooBar(string $s) : void {
assertFooBar($a);',
'error_message' => 'InvalidDocblock',
],
'sortOfReplacementForAssert' => [
'<?php
namespace Bar;
/**
* @param mixed $_b
* @psalm-assert true $_b
*/
function myAssert($_b) : void {
if ($_b !== true) {
throw new \Exception("bad");
}
}
function bar(?string $s) : string {
myAssert($s);
return $s;
}',
'error_message' => 'TypeDoesNotContainType',
],
];
}
}

0 comments on commit 22ba3ad

Please sign in to comment.
You can’t perform that action at this time.