Skip to content

Resolve static in assert phpdoc #4222

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
private ?array $namedArgumentsVariants,
private ?Type $selfOutType,
private ?Type $throwType,
private Assertions $assertions,
)
{
}
Expand Down Expand Up @@ -133,7 +134,7 @@ public function hasSideEffects(): TrinaryLogic

public function getAsserts(): Assertions
{
return $this->reflection->getAsserts();
return $this->assertions;
}

public function acceptsNamedArguments(): TrinaryLogic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentVariants,
$selfOutType,
$throwType,
$method->getAsserts()->mapTypes($this->transformStaticTypeCallback),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentsVariants,
$selfOutType,
$throwType,
$method->getAsserts()->mapTypes(fn (Type $type): Type => $this->transformStaticType($type)),
);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,16 @@ public function testBug9141(): void
$this->analyse([__DIR__ . '/data/bug-9141.php'], []);
}

public function testBug12548(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-12548.php'], []);
}

public function testBug3589(): void
{
$this->checkThisOnly = false;
Expand Down
86 changes: 86 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12548.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types = 1);

namespace Bug12548;

class BaseSat extends \stdClass
{
/**
* @template T of object
*
* @param T $object
*
* @phpstan-assert-if-true =static $object
*/
public static function assertInstanceOf(object $object): bool
{
return $object instanceof static;
}

/**
* @template T of object
*
* @param T $object
*
* @return (T is static ? T : static)
*
* @phpstan-assert static $object
*/
public static function assertInstanceOf2(object $object)
{
if (!$object instanceof static) {
throw new \Error('Object is not an instance of static class');
}

return $object;
}
}

class StdSat extends BaseSat
{
public function foo(): void {}

/**
* @template T of object
*
* @param T $object
*
* @return (T is static ? T : static)
*
* @phpstan-assert static $object
*/
public static function assertInstanceOf3(object $object)
{
if (!$object instanceof static) {
throw new \Error('Object is not an instance of static class');
}

return $object;
}
}

class TestCase
{
private function createStdSat(): \stdClass
{
return new StdSat();
}

public function testAssertInstanceOf(): void
{
$o = $this->createStdSat();
$o->foo(); // @phpstan-ignore method.nonObject (EXPECTED)

$o = $this->createStdSat();
if (StdSat::assertInstanceOf($o)) {
$o->foo();
}

$o = $this->createStdSat();
StdSat::assertInstanceOf2($o);
$o->foo();

$o = $this->createStdSat();
StdSat::assertInstanceOf3($o);
$o->foo();
}
}
Loading