Skip to content

Commit

Permalink
Fixed new $object with a template type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 16, 2021
1 parent 18adc8c commit 956a7f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Analyser/MutatingScope.php
Expand Up @@ -4974,12 +4974,12 @@ private function getTypeToInstantiateForNew(Type $type): Type
if ($type instanceof ConstantStringType) {
return new ObjectType($type->getValue());
}
if ($type instanceof TypeWithClassName) {
return $type;
}
if ($type instanceof GenericClassStringType) {
return $type->getGenericType();
}
if ((new ObjectWithoutClassType())->isSuperTypeOf($type)->yes()) {
return $type;
}
return null;
};

Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/data/generics.php
Expand Up @@ -1462,3 +1462,28 @@ function foooo(UnionT $foo): void
{
assertType('string|null', $foo->doFoo('a'));
}

/**
* @template T1 of object
* @param T1 $type
* @return T1
*/
function newObject($type): void
{
assertType('T1 of object (function PHPStan\Generics\FunctionsAssertType\newObject(), argument)', new $type);
}

function newStdClass(\stdClass $std): void
{
assertType('stdClass', new $std);
}

/**
* @template T1 of object
* @param class-string<T1> $type
* @return T1
*/
function newClassString($type): void
{
assertType('T1 of object (function PHPStan\Generics\FunctionsAssertType\newClassString(), argument)', new $type);
}

0 comments on commit 956a7f4

Please sign in to comment.