Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8562 #8571

Merged
merged 1 commit into from Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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