Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions src/Rules/Classes/InstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\VerbosityLevel;
use function array_filter;
use function array_map;
use function array_merge;
use function count;
use function sprintf;
use function str_starts_with;
use function strtolower;

/**
Expand Down Expand Up @@ -245,6 +248,18 @@ private function getClassNames(Node $node, Scope $scope): array

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

if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry, rule logic can't rely on string type descriptions.

Copy link
Contributor

Choose a reason for hiding this comment

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

could this use Type->isClassStringType()?

$classStringObjectType = $type->getClassStringObjectType();

return array_map(
static fn (string $name): array => [$name, true],
array_filter($classStringObjectType->getObjectClassNames(), function (string $name): bool {
$reflectionClass = $this->reflectionProvider->getClass($name);
return !$reflectionClass->isAbstract() && !$reflectionClass->isInterface();
}),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

could this be shortened with Type->getObjectClassReflections()?

}

return array_merge(
array_map(
static fn (ConstantStringType $type): array => [$type->getValue(), true],
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,46 @@ public function testBug11815(): void
$this->analyse([__DIR__ . '/data/bug-11815.php'], []);
}

public function testClassString(): void
{
$this->analyse([__DIR__ . '/data/class-string.php'], [
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
65,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
66,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
67,
],
[
'Parameter #1 $i of class ClassString\C constructor expects int, string given.',
75,
],
[
'Parameter #1 $i of class ClassString\C constructor expects int, string given.',
76,
],
[
'Parameter #1 $i of class ClassString\C constructor expects int, string given.',
77,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
85,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
86,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
87,
],
]);
}

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

declare(strict_types = 1);

namespace ClassString;

class A
{
public function __construct(public int $i)
{
}
}

abstract class B
{
public function __construct(public int $i)
{
}
}

class C extends B
{
}

interface D
{
}

class Foo
{
/**
* @return class-string<A>
*/
public static function returnClassStringA(): string
{
return A::class;
}

/**
* @return class-string<B>
*/
public static function returnClassStringB(): string
{
return B::class;
}

/**
* @return class-string<C>
*/
public static function returnClassStringC(): string
{
return C::class;
}

/**
* @return class-string<D>
*/
public static function returnClassStringD(): string
{
return D::class;
}
}

$classString = Foo::returnClassStringA();
$error = new (Foo::returnClassStringA())('O_O');
$error = new ($classString)('O_O');
$error = new $classString('O_O');

$classString = Foo::returnClassStringB();
$ok = new (Foo::returnClassStringB())('O_O');
$ok = new ($classString)('O_O');
$ok = new $classString('O_O');

$classString = Foo::returnClassStringC();
$error = new (Foo::returnClassStringC())('O_O');
$error = new ($classString)('O_O');
$error = new $classString('O_O');

$classString = Foo::returnClassStringD();
$ok = new (Foo::returnClassStringD())('O_O');
$ok = new ($classString)('O_O');
$ok = new $classString('O_O');

$className = A::class;
$error = new ($className)('O_O');
$error = new $className('O_O');
$error = new A('O_O');
Loading