Skip to content

Commit

Permalink
Only for bleeding edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 11, 2023
1 parent 8b926b1 commit bbe8522
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5083,14 +5083,11 @@ private function exactInstantiation(New_ $node, string $className): ?Type
$typeMap = $parametersAcceptor->getResolvedTemplateTypeMap();
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
$templateType = $typeMap->getType($tag->getName());
$bound = $tag->getBound();
if ($templateType !== null) {
if ($templateType->isConstantValue()->yes() && (!$bound->isScalar()->yes() || $bound->describe(VerbosityLevel::precise()) === '(int|string)')) {
$templateType = $templateType->generalize(GeneralizePrecision::templateArgument());
}
$list[] = $templateType;
continue;
}
$bound = $tag->getBound();
if ($bound instanceof MixedType && $bound->isExplicitMixed()) {
$bound = new MixedType(false);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/ResolvedFunctionVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Reflection;

use PHPStan\DependencyInjection\BleedingEdgeToggle;
use PHPStan\Reflection\Php\DummyParameterWithPhpDocs;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\ErrorType;
Expand Down Expand Up @@ -217,7 +218,7 @@ private function resolveResolvableTemplateTypes(Type $type, TemplateTypeVariance
};

return TypeTraverser::map($type, function (Type $type, callable $traverse) use ($references, $objectCb): Type {
if ($type instanceof GenericObjectType) {
if (BleedingEdgeToggle::isBleedingEdge() && $type instanceof GenericObjectType) {
return TypeTraverser::map($type, $objectCb);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace PHPStan\Type\Generic;

use PHPStan\DependencyInjection\BleedingEdgeToggle;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
Expand Down Expand Up @@ -264,6 +266,13 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
TemplateTypeVariance::createStatic(),
));
if ($resolvedBound->isSuperTypeOf($receivedType)->yes()) {
if (!BleedingEdgeToggle::isBleedingEdge() && $this->shouldGeneralizeInferredType()) {
$generalizedType = $receivedType->generalize(GeneralizePrecision::templateArgument());
if ($resolvedBound->isSuperTypeOf($generalizedType)->yes()) {
$receivedType = $generalizedType;
}
}

return (new TemplateTypeMap([
$this->name => $receivedType,
]))->union($map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use function get_class;
use function sprintf;

class GenericParametersAcceptorResolverTest extends PHPStanTestCase
class GenericParametersAcceptorResolverTest extends PHPStanTestCase
{

/**
Expand Down Expand Up @@ -427,6 +427,7 @@ public function dataResolve(): array
*/
public function testResolve(array $argTypes, ParametersAcceptor $parametersAcceptor, ParametersAcceptor $expectedResult): void
{
self::getContainer(); // to initialize bleeding edge
$result = GenericParametersAcceptorResolver::resolve(
$argTypes,
$parametersAcceptor,
Expand Down Expand Up @@ -462,4 +463,11 @@ public function testResolve(array $argTypes, ParametersAcceptor $parametersAccep
}
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
];
}

}

0 comments on commit bbe8522

Please sign in to comment.