Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sayuprc committed Nov 24, 2024
1 parent c4948fe commit 18529dc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
24 changes: 18 additions & 6 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,27 +504,39 @@ public function testClassString(): void
$this->analyse([__DIR__ . '/data/class-string.php'], [
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
26,
65,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
27,
66,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
28,
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.',
31,
85,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
32,
86,
],
[
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
34,
87,
],
]);
}
Expand Down
73 changes: 63 additions & 10 deletions tests/PHPStan/Rules/Classes/data/class-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,77 @@ public function __construct(public int $i)
}
}

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

class C extends B
{
}

interface D
{
}

class Foo
{
/**
* @return class-string<A>
*/
public static function sayHelloBug(): string
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 = HelloWorld::sayHelloBug();
$bug = new (HelloWorld::sayHelloBug())('O_O');
$bug = new ($classString)('O_O');
$bug = new $classString('O_O');
$classString = Foo::returnClassStringA();
$error = new (Foo::returnClassStringA())('O_O');
$error = new ($classString)('O_O');
$error = new $classString('O_O');

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

$ok = new A('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');

0 comments on commit 18529dc

Please sign in to comment.