Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion src/Reflection/Type/UnionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ public function getDocComment(): ?string

public function getAsserts(): Assertions
{
return Assertions::createEmpty();
$mergedAssertions = Assertions::createEmpty();

foreach ($this->methods as $method) {
$mergedAssertions = $mergedAssertions->intersectWith($method->getAsserts());
}

return $mergedAssertions;
}

public function acceptsNamedArguments(): TrinaryLogic
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Methods/AssertIfTrueOnThisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Methods;

use PHPStan\Testing\TypeInferenceTestCase;

class AssertIfTrueOnThisTest extends TypeInferenceTestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be dropped when everything else moved into nsrt/

Copy link
Author

@yt-catpaw yt-catpaw Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
31b55ac

{

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13358.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(string $assertType, string $file, ...$args): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-13358.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Bug13358;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this also into nsrt/ folder

Copy link
Author

@yt-catpaw yt-catpaw Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
31b55ac


use function PHPStan\Testing\assertType;

/**
* @phpstan-sealed SystemActor|AnonymousVisitorActor
*/
abstract class Actor
{
/**
* @phpstan-assert-if-true SystemActor $this
*/
public function isSystem(): bool
{
return $this instanceof SystemActor;
}

/**
* @phpstan-assert-if-true AnonymousVisitorActor $this
*/
public function isAnonymousVisitor(): bool
{
return $this instanceof AnonymousVisitorActor;
}
}

class SystemActor extends Actor {}
class AnonymousVisitorActor extends Actor {}

function test(AnonymousVisitorActor|SystemActor $actor): void {
assertType('Bug13358\AnonymousVisitorActor|Bug13358\SystemActor', $actor);

if ($actor->isSystem()) {
assertType('Bug13358\SystemActor', $actor);
} else {
assertType('Bug13358\AnonymousVisitorActor', $actor);
}
}
Loading