Skip to content

Commit

Permalink
Delegate get_called_class() to static::class
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 23, 2023
1 parent 4e19347 commit 188acf4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Type/Php/GetCalledClassDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;

Expand All @@ -20,9 +21,8 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$classContext = $scope->getClassReflection();
if ($classContext !== null) {
return new ConstantStringType($classContext->getName(), true);
if ($scope->isInClass()) {
return $scope->getType(new ClassConstFetch(new Name('static'), 'class'));
}
return new ConstantBooleanType(false);
}
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 @@ -1197,6 +1197,7 @@ public function dataFileAsserts(): iterable
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/always-true-elseif.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7547.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9341.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathinfo.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8568.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9341.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug9341;

use function PHPStan\Testing\assertType;

interface MyInterface {}

trait MyTrait
{
public static function parse(): mixed
{
$class = get_called_class();
assertType('class-string<static(Bug9341\MyAbstractBase)>', $class);
if (!is_a($class, MyInterface::class, true)) {
return false;
}
assertType('class-string<Bug9341\MyInterface&static(Bug9341\MyAbstractBase)>', $class);
$fileObject = new $class();
assertType('Bug9341\MyInterface&static(Bug9341\MyAbstractBase)', $fileObject);
return $fileObject;
}
}

abstract class MyAbstractBase {
use MyTrait;
}

class MyClass extends MyAbstractBase implements MyInterface
{

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,9 @@ public function testBug3311a(): void
]);
}

public function testBug9341(): void
{
$this->analyse([__DIR__ . '/../../Analyser/data/bug-9341.php'], []);
}

}

0 comments on commit 188acf4

Please sign in to comment.