Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
35 changes: 34 additions & 1 deletion src/Analyser/ExprHandler/NewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use function array_map;
use function array_merge;
use function count;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -308,7 +309,39 @@
}

$exprType = $scope->getType($expr->class);
return $exprType->getObjectTypeOrClassStringObjectType();
$objectType = $exprType->getObjectTypeOrClassStringObjectType();

// When $class has been narrowed from class-string<T> to a concrete class string,
// preserve the template type relationship by intersecting with the template type.
// This ensures that `new $class()` is still compatible with return type T.
if (
$expr->class instanceof Expr\Variable
&& is_string($expr->class->name)
&& !$objectType->hasTemplateOrLateResolvableType()
) {
$function = $scope->getFunction();
if ($function !== null) {
foreach ($function->getParameters() as $param) {
if ($param->getName() !== $expr->class->name) {
continue;
}
$paramType = $param->getType();
if (!$paramType->isClassString()->yes()) {

Check warning on line 329 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $paramType = $param->getType(); - if (!$paramType->isClassString()->yes()) { + if ($paramType->isClassString()->no()) { break; } $classStringObjectType = $paramType->getClassStringObjectType();

Check warning on line 329 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $paramType = $param->getType(); - if (!$paramType->isClassString()->yes()) { + if ($paramType->isClassString()->no()) { break; } $classStringObjectType = $paramType->getClassStringObjectType();
break;
}
$classStringObjectType = $paramType->getClassStringObjectType();
if (!$classStringObjectType instanceof TemplateType) {
break;
}
if ($classStringObjectType->getBound()->isSuperTypeOf($objectType)->yes()) {

Check warning on line 336 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$classStringObjectType instanceof TemplateType) { break; } - if ($classStringObjectType->getBound()->isSuperTypeOf($objectType)->yes()) { + if (!$classStringObjectType->getBound()->isSuperTypeOf($objectType)->no()) { $objectType = TypeCombinator::intersect($classStringObjectType, $objectType); } break;

Check warning on line 336 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$classStringObjectType instanceof TemplateType) { break; } - if ($classStringObjectType->getBound()->isSuperTypeOf($objectType)->yes()) { + if (!$classStringObjectType->getBound()->isSuperTypeOf($objectType)->no()) { $objectType = TypeCombinator::intersect($classStringObjectType, $objectType); } break;
$objectType = TypeCombinator::intersect($classStringObjectType, $objectType);
}
break;
}
}
}

return $objectType;
}

private function exactInstantiation(MutatingScope $scope, New_ $node, Name $className): Type
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14188.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug14188Nsrt;

use function PHPStan\Testing\assertType;

interface MyInterface {}
class A implements MyInterface {}
class B implements MyInterface {}

class MyFactory {
/**
* @template T of MyInterface
*
* @param class-string<T> $class
*
* @return T
*/
public function create(string $class) {
if ($class === A::class) {
assertType("'Bug14188Nsrt\\\\A'", $class);
assertType('Bug14188Nsrt\A&T of Bug14188Nsrt\MyInterface (method Bug14188Nsrt\MyFactory::create(), argument)', new $class());
return new $class();
}

return new $class();
}
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,4 +1310,9 @@ public function testBug9669(): void
$this->analyse([__DIR__ . '/data/bug-9669.php'], []);
}

public function testBug14188(): void
{
$this->analyse([__DIR__ . '/data/bug-14188.php'], []);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-14188.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug14188;

interface MyInterface {}
class A implements MyInterface {}
class B implements MyInterface {}

class MyFactory {
/**
* @template T of MyInterface
*
* @param class-string<T> $class
*
* @return T
*/
public function create(string $class) {
if ($class === A::class) {
return new $class();
}

return new $class();
}
}
Loading