Skip to content

Commit

Permalink
Inferring new from parent constructor - reject types that would fai…
Browse files Browse the repository at this point in the history
…l bound check of the child class
ondrejmirtes committed Jan 21, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent dce063a commit d06f792
Showing 2 changed files with 77 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
@@ -5575,7 +5575,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
continue;
}

$ancestorMapping[$typeName] = $templateType->getName();
$ancestorMapping[$typeName] = $templateType;
}

$resolvedTypeMap = [];
@@ -5584,12 +5584,17 @@ private function exactInstantiation(New_ $node, string $className): ?Type
continue;
}

if (!array_key_exists($ancestorMapping[$typeName], $resolvedTypeMap)) {
$resolvedTypeMap[$ancestorMapping[$typeName]] = $type;
$ancestorType = $ancestorMapping[$typeName];
if (!$ancestorType->getBound()->isSuperTypeOf($type)->yes()) {
continue;
}

$resolvedTypeMap[$ancestorMapping[$typeName]] = TypeCombinator::union($resolvedTypeMap[$ancestorMapping[$typeName]], $type);
if (!array_key_exists($ancestorType->getName(), $resolvedTypeMap)) {
$resolvedTypeMap[$ancestorType->getName()] = $type;
continue;
}

$resolvedTypeMap[$ancestorType->getName()] = TypeCombinator::union($resolvedTypeMap[$ancestorType->getName()], $type);
}

return new GenericObjectType(
68 changes: 68 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12386.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Bug12386;

use function PHPStan\Testing\assertType;

function doFoo() {
$landMapper = new Application_Model_Mapper_Land();
assertType('Bug12386\\Clx_Model_Iterator<Bug12386\\Application_Model_Land>', $landMapper->fetchAllActivePrependDefault(12));
}

/**
* @template T of Clx_Model_Abstract
*/
abstract class Clx_Model_Mapper_Abstract
{
public function __construct()
{
}
}

/**
* @template T of Application_Model_Land
*
* @extends Clx_Model_Mapper_Abstract<T>
*/
class ClxProductNet_Model_Mapper_Land extends Clx_Model_Mapper_Abstract
{
/**
* @param int $defaultLandid
*
* @return Clx_Model_Iterator<T>
*/
public function fetchAllActivePrependDefault($defaultLandid): Clx_Model_Iterator
{}
}

/**
* @template T of Application_Model_Land
*
* @extends ClxProductNet_Model_Mapper_Land<T>
*/
final class Application_Model_Mapper_Land extends ClxProductNet_Model_Mapper_Land
{
}

/**
* @template T of Clx_Model_Abstract
*
* @implements \Iterator<T>
*/
abstract class Clx_Model_Iterator implements \Countable, \Iterator
{}

abstract class Clx_Model_Abstract implements \Stringable
{}

abstract class ClxProductNet_Model_Land extends Clx_Model_Abstract
{}

final class Application_Model_Land extends ClxProductNet_Model_Land
{
public function __toString()
{
return 'foo';
}

}

0 comments on commit d06f792

Please sign in to comment.