Skip to content

Commit

Permalink
Merge pull request #8571 from Nicelocal/fix_8562
Browse files Browse the repository at this point in the history
Fix #8562
  • Loading branch information
orklah committed Oct 12, 2022
2 parents eb6a347 + a7af027 commit 8d874aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Expand Up @@ -36,6 +36,8 @@
use Psalm\Issue\TypeDoesNotContainType;
use Psalm\Issue\UnevaluatedCode;
use Psalm\IssueBuffer;
use Psalm\Node\Expr\BinaryOp\VirtualIdentical;
use Psalm\Node\Expr\BinaryOp\VirtualNotIdentical;
use Psalm\Storage\Assertion;
use Psalm\Storage\Assertion\ArrayKeyExists;
use Psalm\Storage\Assertion\DoesNotHaveAtLeastCount;
Expand Down Expand Up @@ -2195,6 +2197,7 @@ private static function getFalseInequalityAssertions(
&& $var_type->isSingle()
&& $var_type->hasBool()
&& !$var_type->from_docblock
&& !$conditional instanceof VirtualNotIdentical
) {
IssueBuffer::maybeAdd(
new RedundantIdentityWithTrue(
Expand Down Expand Up @@ -2897,6 +2900,7 @@ private static function getTrueEqualityAssertions(
&& $var_type->isSingle()
&& $var_type->hasBool()
&& !$var_type->from_docblock
&& !$conditional instanceof VirtualIdentical
) {
IssueBuffer::maybeAdd(
new RedundantIdentityWithTrue(
Expand Down
45 changes: 45 additions & 0 deletions tests/BinaryOperationTest.php
Expand Up @@ -145,6 +145,51 @@ public function testDecimalOperations(): void
$this->assertSame($assertions, $actual_vars);
}

public function testMatchOnBoolean(): void
{
$config = Config::getInstance();
$config->strict_binary_operands = true;

$this->addFile(
'somefile.php',
'<?php
class a {}
class b {}
/** @var a|b */
$obj = new a;
$result1 = match (true) {
$obj instanceof a => 123,
$obj instanceof b => 321,
};
$result2 = match (false) {
$obj instanceof a => 123,
$obj instanceof b => 321,
};
'
);

$assertions = [
'$obj' => 'a|b',
'$result1' => '123|321',
'$result2' => '123|321',
];

$context = new Context();

$this->project_analyzer->setPhpVersion('8.0', 'tests');
$this->analyzeFile('somefile.php', $context);

$actual_vars = [];
foreach ($assertions as $var => $_) {
if (isset($context->vars_in_scope[$var])) {
$actual_vars[$var] = $context->vars_in_scope[$var]->getId(true);
}
}

$this->assertSame($assertions, $actual_vars);
}

public function testStrictTrueEquivalence(): void
{
$config = Config::getInstance();
Expand Down

0 comments on commit 8d874aa

Please sign in to comment.