Skip to content

Commit

Permalink
narrow down $this/static for type specifier 2
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Apr 13, 2024
1 parent d3a2990 commit ddce768
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Expand Up @@ -1645,7 +1645,7 @@ private function createForExpr(
) {
$methodName = $expr->name->toString();
if ($expr->class instanceof Name) {
$calledOnType = $scope->resolveTypeByName($expr->class);
$calledOnType = $scope->getType(new ClassConstFetch($expr->class, 'class'))->getClassStringObjectType();
} else {
$calledOnType = $scope->getType($expr->class);
}
Expand Down
Expand Up @@ -6,6 +6,8 @@ interface FooInterface
{
/** @phpstan-assert-if-true null $v */
public static function isNull(?int $v): bool;

public static function foo(): int;
}

class FooBase
Expand All @@ -16,12 +18,20 @@ public function bar(?int $v): void
if ($this->isNull($v)) \PHPStan\Testing\assertType('null', $v);
if ($this::isNull($v)) \PHPStan\Testing\assertType('null', $v);
if (static::isNull($v)) \PHPStan\Testing\assertType('null', $v);

if ($this::foo() === 5) \PHPStan\Testing\assertType('5', $this::foo());
if ($this->foo() === 5) \PHPStan\Testing\assertType('5', $this->foo());
if (static::foo() === 5) \PHPStan\Testing\assertType('5', static::foo());
}

if (is_a(static::class, FooInterface::class, true)) {
if ($this->isNull($v)) \PHPStan\Testing\assertType('null', $v);
if ($this::isNull($v)) \PHPStan\Testing\assertType('null', $v);
if (static::isNull($v)) \PHPStan\Testing\assertType('null', $v);

if ($this::foo() === 5) \PHPStan\Testing\assertType('5', $this::foo());
if ($this->foo() === 5) \PHPStan\Testing\assertType('5', $this->foo());
if (static::foo() === 5) \PHPStan\Testing\assertType('5', static::foo());
}
}
}

0 comments on commit ddce768

Please sign in to comment.