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
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2869,7 +2869,7 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require
continue;
}
$firstExpr = $holders[array_key_first($holders)]->getTypeHolder()->getExpr();
if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $firstExpr, $this->getNodeKey($firstExpr), false, $invalidatingClass)) {
if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $firstExpr, $this->getNodeKey($firstExpr), $requireMoreCharacters, $invalidatingClass)) {
$invalidated = true;
continue;
}
Expand Down
112 changes: 112 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14545.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types = 1);

namespace Bug14545;

use function PHPStan\Testing\assertType;

interface SomeInterface {
public function test(): void;
}

class ObjectClass {
}

class OtherClass {
}

/**
* @template T of object
* @param class-string<T> $class_name
* @return T
*/
function getObject1(string $class_name): object {
return new $class_name;
}

function testStoredInstanceofWithGenericMethodCall(): void {
$obj = getObject1(ObjectClass::class);
$is_interface = $obj instanceof SomeInterface;
if($is_interface) {
assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj);
$obj->test();
}

if($is_interface) {
assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj);
$obj->test();
}
}

function testStoredInstanceofWithGenericFuncCall(): void {
$obj = getObject1(ObjectClass::class);
$is_interface = $obj instanceof SomeInterface;
if($is_interface) {
var_dump($obj);
}

if($is_interface) {
assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj);
}
}

function testStoredInstanceofWithConcreteClass(): void {
$obj = getObject1(OtherClass::class);
$is_interface = $obj instanceof SomeInterface;
if($is_interface) {
assertType('Bug14545\OtherClass&Bug14545\SomeInterface', $obj);
$obj->test();
}

if($is_interface) {
assertType('Bug14545\OtherClass&Bug14545\SomeInterface', $obj);
}
}

function getObject2(): object {
return new \stdClass();
}

function testStoredInstanceofWithAbstractObject(): void {
$obj = getObject2();
$is_interface = $obj instanceof SomeInterface;
if($is_interface) {
assertType('Bug14545\SomeInterface', $obj);
$obj->test();
}

if($is_interface) {
assertType('Bug14545\SomeInterface', $obj);
$obj->test();
}
}

function testThreeConsecutiveChecks(): void {
$obj = getObject1(ObjectClass::class);
$is_interface = $obj instanceof SomeInterface;
if($is_interface) {
$obj->test();
}
if($is_interface) {
$obj->test();
}
if($is_interface) {
assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj);
}
}

/**
* @param array<mixed, mixed> $data
*/
function testStoredIsArray(array $data): void {
$value = $data['key'] ?? null;
$isArray = is_array($value);
if ($isArray) {
assertType('array<mixed, mixed>', $value);
var_dump($value);
}
if ($isArray) {
assertType('array<mixed, mixed>', $value);
}
}
Loading