Skip to content

Commit 298e55e

Browse files
committed
Simplify
1 parent 615d5e6 commit 298e55e

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Rules/Classes/InstantiationRule.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\New_;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Internal\SprintfHelper;
9+
use PHPStan\Reflection\ClassReflection;
910
use PHPStan\Reflection\ParametersAcceptorSelector;
1011
use PHPStan\Reflection\Php\PhpMethodReflection;
1112
use PHPStan\Reflection\ReflectionProvider;
@@ -17,13 +18,11 @@
1718
use PHPStan\Rules\RuleErrorBuilder;
1819
use PHPStan\ShouldNotHappenException;
1920
use PHPStan\Type\Constant\ConstantStringType;
20-
use PHPStan\Type\VerbosityLevel;
2121
use function array_filter;
2222
use function array_map;
2323
use function array_merge;
2424
use function count;
2525
use function sprintf;
26-
use function str_starts_with;
2726
use function strtolower;
2827

2928
/**
@@ -248,16 +247,18 @@ private function getClassNames(Node $node, Scope $scope): array
248247

249248
$type = $scope->getType($node->class);
250249

251-
if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) {
252-
$classStringObjectType = $type->getClassStringObjectType();
253-
254-
return array_map(
255-
static fn (string $name): array => [$name, true],
256-
array_filter($classStringObjectType->getObjectClassNames(), function (string $name): bool {
257-
$reflectionClass = $this->reflectionProvider->getClass($name);
258-
return !$reflectionClass->isAbstract() && !$reflectionClass->isInterface();
259-
}),
250+
if ($type->isClassString()->yes()) {
251+
$concretes = array_filter(
252+
$type->getClassStringObjectType()->getObjectClassReflections(),
253+
static fn (ClassReflection $classReflection): bool => !$classReflection->isAbstract() && !$classReflection->isInterface(),
260254
);
255+
256+
if (0 < count($concretes)) {
257+
return array_map(
258+
static fn (ClassReflection $classReflection): array => [$classReflection->getName(), true],
259+
$concretes,
260+
);
261+
}
261262
}
262263

263264
return array_merge(

0 commit comments

Comments
 (0)