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 00135b2 commit 87cc16c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5044,14 +5044,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

0 comments on commit 87cc16c

Please sign in to comment.