Skip to content

Commit 1c25bde

Browse files
committed
Fix PHPStan issue by enabling constructor check for class-string variables
1 parent adcabb3 commit 1c25bde

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Rules/Classes/InstantiationRule.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
use PHPStan\Rules\Rule;
1717
use PHPStan\Rules\RuleErrorBuilder;
1818
use PHPStan\ShouldNotHappenException;
19+
use PHPStan\Type\CompoundType;
1920
use PHPStan\Type\Constant\ConstantStringType;
21+
use PHPStan\Type\Generic\TemplateGenericObjectType;
22+
use PHPStan\Type\StaticType;
23+
use PHPStan\Type\VerbosityLevel;
2024
use function array_map;
2125
use function array_merge;
2226
use function count;
2327
use function sprintf;
28+
use function str_starts_with;
2429
use function strtolower;
2530

2631
/**
@@ -245,6 +250,21 @@ private function getClassNames(Node $node, Scope $scope): array
245250

246251
$type = $scope->getType($node->class);
247252

253+
if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) {
254+
$classStringObjectType = $type->getClassStringObjectType();
255+
256+
if (
257+
!$classStringObjectType instanceof StaticType
258+
&& !$classStringObjectType instanceof CompoundType
259+
&& !$classStringObjectType instanceof TemplateGenericObjectType
260+
) {
261+
return array_map(
262+
static fn (string $name): array => [$name, true],
263+
$classStringObjectType->getObjectClassNames(),
264+
);
265+
}
266+
}
267+
248268
return array_merge(
249269
array_map(
250270
static fn (ConstantStringType $type): array => [$type->getValue(), true],

0 commit comments

Comments
 (0)