Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ private function specifyTypesForConstantBinaryExpression(
{
if (!$context->null() && $constantType->isFalse()->yes()) {
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) {
if (!$context->true() && ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch)) {
return $types;
}

Expand All @@ -1148,7 +1148,7 @@ private function specifyTypesForConstantBinaryExpression(

if (!$context->null() && $constantType->isTrue()->yes()) {
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) {
if (!$context->true() && ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch)) {
return $types;
}

Expand Down
65 changes: 65 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12866.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug12866;

use function PHPStan\Testing\assertType;

interface I
{
/**
* @phpstan-assert-if-true A $this
*/
public function isA(): bool;
}

class A implements I
{
public function isA(): bool
{
return true;
}
}

class B implements I
{
public function isA(): bool
{
return false;
}
}

function takesI(I $i): void
{
if (!$i->isA()) {
return;
}

assertType('Bug12866\\A', $i);
}

function takesIStrictComparison(I $i): void
{
if ($i->isA() !== true) {
return;
}

assertType('Bug12866\\A', $i);
}

function takesNullableI(?I $i): void
{
if (!$i?->isA()) {
return;
}

assertType('Bug12866\\A', $i);
}

function takesNullableIStrictComparison(?I $i): void
{
if ($i?->isA() !== true) {
return;
}

assertType('Bug12866\\A', $i);
}
Loading