Skip to content

Commit 5834cf5

Browse files
committed
[BE] Infer explicit mixed when instantiating generic class with unknown template types
1 parent 12879e4 commit 5834cf5

File tree

8 files changed

+13
-51
lines changed

8 files changed

+13
-51
lines changed

changelog-2.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Major new features 🚀
1111
Bleeding edge (TODO move to other sections)
1212
=====================
1313

14-
* Infer explicit mixed when instantiating generic class with unknown template types (https://github.com/phpstan/phpstan-src/commit/089d4c6fb6eb709c44123548d33990113d174b86), #6398
1514
* Report useless `array_filter()` calls ([#1077](https://github.com/phpstan/phpstan-src/pull/1077)), #6840, thanks @leongersen!
1615
* Specify explicit mixed array type via `is_array` ([#1191](https://github.com/phpstan/phpstan-src/pull/1191)), thanks @herndlm!
1716
* Lower memory consumption thanks to breaking up of reference cycles
@@ -140,6 +139,7 @@ Improvements 🔧
140139
* Learn more: [Using RuleErrorBuilder to enrich reported errors in custom rules](https://phpstan.org/blog/using-rule-error-builder)
141140
* Require identifier in custom rules (https://github.com/phpstan/phpstan-src/commit/969e6fa31d5484d42dab902703cfc6820a983cfd)
142141
* New `RuleLevelHelper::accepts()` behaviour (https://github.com/phpstan/phpstan-src/commit/941fc815db49315b8783dc466cf593e0d8a85d23)
142+
* Infer explicit mixed when instantiating generic class with unknown template types (https://github.com/phpstan/phpstan-src/commit/089d4c6fb6eb709c44123548d33990113d174b86), #6398
143143

144144
Bugfixes 🐛
145145
=====================

conf/bleedingEdge.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ parameters:
22
featureToggles:
33
bleedingEdge: true
44
skipCheckGenericClasses!: []
5-
explicitMixedInUnknownGenericNew: true
65
explicitMixedForGlobalVariables: true
76
explicitMixedViaIsArray: true
87
arrayFilter: true

conf/config.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ parameters:
3636
- CachingIterator
3737
- RegexIterator
3838
- ReflectionEnum
39-
explicitMixedInUnknownGenericNew: false
4039
explicitMixedForGlobalVariables: false
4140
explicitMixedViaIsArray: false
4241
arrayFilter: false

conf/parametersSchema.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ parametersSchema:
3131
bleedingEdge: bool(),
3232
disableRuntimeReflectionProvider: bool(),
3333
skipCheckGenericClasses: listOf(string()),
34-
explicitMixedInUnknownGenericNew: bool(),
3534
explicitMixedForGlobalVariables: bool(),
3635
explicitMixedViaIsArray: bool(),
3736
arrayFilter: bool(),

src/Analyser/DirectInternalScopeFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function __construct(
3535
private Parser $parser,
3636
private NodeScopeResolver $nodeScopeResolver,
3737
private PhpVersion $phpVersion,
38-
private bool $explicitMixedInUnknownGenericNew,
3938
private bool $explicitMixedForGlobalVariables,
4039
private ConstantResolver $constantResolver,
4140
)
@@ -103,7 +102,6 @@ public function create(
103102
$afterExtractCall,
104103
$parentScope,
105104
$nativeTypesPromoted,
106-
$this->explicitMixedInUnknownGenericNew,
107105
$this->explicitMixedForGlobalVariables,
108106
);
109107
}

src/Analyser/LazyInternalScopeFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
final class LazyInternalScopeFactory implements InternalScopeFactory
2121
{
2222

23-
private bool $explicitMixedInUnknownGenericNew;
24-
2523
private bool $explicitMixedForGlobalVariables;
2624

2725
/**
@@ -32,7 +30,6 @@ public function __construct(
3230
private Container $container,
3331
)
3432
{
35-
$this->explicitMixedInUnknownGenericNew = $this->container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'];
3633
$this->explicitMixedForGlobalVariables = $this->container->getParameter('featureToggles')['explicitMixedForGlobalVariables'];
3734
}
3835

@@ -97,7 +94,6 @@ public function create(
9794
$afterExtractCall,
9895
$parentScope,
9996
$nativeTypesPromoted,
100-
$this->explicitMixedInUnknownGenericNew,
10197
$this->explicitMixedForGlobalVariables,
10298
);
10399
}

src/Analyser/MutatingScope.php

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ public function __construct(
220220
private bool $afterExtractCall = false,
221221
private ?Scope $parentScope = null,
222222
private bool $nativeTypesPromoted = false,
223-
private bool $explicitMixedInUnknownGenericNew = false,
224223
private bool $explicitMixedForGlobalVariables = false,
225224
)
226225
{
@@ -5549,49 +5548,22 @@ private function exactInstantiation(New_ $node, string $className): ?Type
55495548
$constructorMethod->getNamedArgumentsVariants(),
55505549
);
55515550

5552-
if ($this->explicitMixedInUnknownGenericNew) {
5553-
$resolvedTemplateTypeMap = $parametersAcceptor->getResolvedTemplateTypeMap();
5554-
return TypeTraverser::map(new GenericObjectType(
5555-
$resolvedClassName,
5556-
$classReflection->typeMapToList($classReflection->getTemplateTypeMap()),
5557-
), static function (Type $type, callable $traverse) use ($resolvedTemplateTypeMap): Type {
5558-
if ($type instanceof TemplateType && !$type->isArgument()) {
5559-
$newType = $resolvedTemplateTypeMap->getType($type->getName());
5560-
if ($newType === null || $newType instanceof ErrorType) {
5561-
return $type->getBound();
5562-
}
5563-
5564-
return TemplateTypeHelper::generalizeInferredTemplateType($type, $newType);
5551+
$resolvedTemplateTypeMap = $parametersAcceptor->getResolvedTemplateTypeMap();
5552+
return TypeTraverser::map(new GenericObjectType(
5553+
$resolvedClassName,
5554+
$classReflection->typeMapToList($classReflection->getTemplateTypeMap()),
5555+
), static function (Type $type, callable $traverse) use ($resolvedTemplateTypeMap): Type {
5556+
if ($type instanceof TemplateType && !$type->isArgument()) {
5557+
$newType = $resolvedTemplateTypeMap->getType($type->getName());
5558+
if ($newType === null || $newType instanceof ErrorType) {
5559+
return $type->getBound();
55655560
}
55665561

5567-
return $traverse($type);
5568-
});
5569-
}
5570-
5571-
$resolvedPhpDoc = $classReflection->getResolvedPhpDoc();
5572-
if ($resolvedPhpDoc === null) {
5573-
return $objectType;
5574-
}
5575-
5576-
$list = [];
5577-
$typeMap = $parametersAcceptor->getResolvedTemplateTypeMap();
5578-
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
5579-
$templateType = $typeMap->getType($tag->getName());
5580-
if ($templateType !== null) {
5581-
$list[] = $templateType;
5582-
continue;
5562+
return TemplateTypeHelper::generalizeInferredTemplateType($type, $newType);
55835563
}
5584-
$bound = $tag->getBound();
5585-
if ($bound instanceof MixedType && $bound->isExplicitMixed()) {
5586-
$bound = new MixedType(false);
5587-
}
5588-
$list[] = $bound;
5589-
}
55905564

5591-
return new GenericObjectType(
5592-
$resolvedClassName,
5593-
$list,
5594-
);
5565+
return $traverse($type);
5566+
});
55955567
}
55965568

55975569
private function filterTypeWithMethod(Type $typeWithMethod, string $methodName): ?Type

src/Testing/PHPStanTestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ public static function createScopeFactory(ReflectionProvider $reflectionProvider
188188
self::getParser(),
189189
$container->getByType(NodeScopeResolver::class),
190190
$container->getByType(PhpVersion::class),
191-
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
192191
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
193192
$constantResolver,
194193
),

0 commit comments

Comments
 (0)