Skip to content

Fix "Unsafe use of new static when trait enforces constructor via abstract method" #4114

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

Merged
merged 2 commits into from
Jul 21, 2025
Merged
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
21 changes: 21 additions & 0 deletions src/Rules/Classes/NewStaticRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\TrinaryLogic;
use function strtolower;

/**
Expand All @@ -17,6 +19,12 @@
final class NewStaticRule implements Rule
{

public function __construct(
private PhpVersion $phpVersion,
)
{
}

public function getNodeType(): string
{
return Node\Expr\New_::class;
Expand Down Expand Up @@ -77,6 +85,19 @@ public function processNode(Node $node, Scope $scope): array
}
}

if (
$this->phpVersion->supportsAbstractTraitMethods()
&& $scope->isInTrait()
) {
$traitReflection = $scope->getTraitReflection();
if ($traitReflection->hasConstructor()) {
$isAbstract = $traitReflection->getConstructor()->isAbstract();
if ($isAbstract === true || ($isAbstract instanceof TrinaryLogic && $isAbstract->yes())) {
return [];
}
}
}

return $messages;
}

Expand Down
25 changes: 24 additions & 1 deletion tests/PHPStan/Rules/Classes/NewStaticRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<NewStaticRule>
Expand All @@ -13,7 +15,9 @@ class NewStaticRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new NewStaticRule();
return new NewStaticRule(
new PhpVersion(PHP_VERSION_ID),
);
}

public function testRule(): void
Expand All @@ -39,4 +43,23 @@ public function testRuleWithConsistentConstructor(): void
$this->analyse([__DIR__ . '/data/new-static-consistent-constructor.php'], []);
}

public function testBug9654(): void
{
$errors = [];
if (PHP_VERSION_ID < 80000) {
$errors[] = [
'Unsafe usage of new static()',
11,
'See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static',
];
$errors[] = [
'Unsafe usage of new static()',
11,
'See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static',
];
}

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

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-9654.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 8.0

namespace Bug9654;

trait Abc
{
abstract public function __construct();

public static function create(): static
{
return new static(); // this is safe as the constructor is defined abstract in this trait
}
}

class Foo
{
use Abc;

public function __construct() {

}
}

class Bar
{
use Abc;

public function __construct(int $i = 0)
{
$i = $i*2;
}
}
Loading