Skip to content
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

Fix ReflectionClass::newInstanceArgs() - error on argument type #2176

Merged
merged 1 commit into from Jan 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion stubs/ReflectionClass.stub
Expand Up @@ -31,7 +31,7 @@ class ReflectionClass
public function newInstance(...$args) {}

/**
* @param array<int, mixed> $args
* @param array<int|string, mixed> $args
*
* @return T
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Expand Up @@ -2710,4 +2710,13 @@ public function testImagickPixel(): void
$this->analyse([__DIR__ . '/data/imagick-pixel.php'], []);
}

public function testNewInstanceArgsIssue8679(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/reflection-class-issue-8679.php'], []);
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Methods/data/reflection-class-issue-8679.php
@@ -0,0 +1,45 @@
<?php

namespace ReflectionClassIssue8679;

class FooClass
{
public function __construct(
private int $test1 = 0,
private int $test2 = 0,
private int $test3 = 0
)
{}

public function showTest(): string
{
return $this->test1.$this->test2.$this->test3."\n";
}
}

class FooClassSimpleFactory
{
/**
* @param array<int, mixed> $options Options for MyClass
*/
public static function getClassA(array $options = []): FooClass
{
return (new \ReflectionClass('FooClass'))->newInstanceArgs($options);
}

/**
* @param array<string, mixed> $options Options for MyClass
*/
public static function getClassB(array $options = []): FooClass
{
return (new \ReflectionClass('FooClass'))->newInstanceArgs($options);
}

/**
* @param array<int|string, mixed> $options Options for MyClass
*/
public static function getClassC(array $options = []): FooClass
{
return (new \ReflectionClass('FooClass'))->newInstanceArgs($options);
}
}