Skip to content
Closed
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
27 changes: 18 additions & 9 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,7 @@ public function specifyTypesInCondition(
&& isset($exprNode->getArgs()[0])
&& $constantType instanceof ConstantStringType
) {
return $this->specifyTypesInCondition(
$scope,
new Instanceof_(
$exprNode->getArgs()[0]->value,
new Name($constantType->getValue()),
),
$context,
$rootExpr,
);
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
}
}

Expand Down Expand Up @@ -1042,6 +1034,23 @@ private function specifyTypesForConstantStringBinaryExpression(
}
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
&& strtolower($exprNode->name->toString()) === 'get_class'
&& isset($exprNode->getArgs()[0])
) {
return $this->specifyTypesInCondition(
$scope,
new Instanceof_(
$exprNode->getArgs()[0]->value,
new Name($constantType->getValue()),
),
$context,
$rootExpr,
);
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-offset-unset.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8008.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5552.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1239.php');
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/data/bug-1239.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug1239;

use function PHPStan\Testing\assertType;

class Foo
{
public function __construct()
{
if (get_class($this) === Bar::class) {

@staabm staabm Aug 12, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just working on one of the different calltypes of bug 1239.
will work on the other types in separate PRs

assertType('$this(Bug1239\Foo)&Bug1239\Bar', $this);
$this->bat();
}
}
}

class Bar extends Foo
{
public function bat(): void
{
}
}