Skip to content

Commit 85ba2ed

Browse files
committed
Enhance EliminationSafetyPolicy with new type checks for object generics; update StaticTypeEvaluator and FunctionContractInjector to utilize new checks; add EliminationBox and ObjectGenericReturnEliminationTest for comprehensive testing of elimination safety rules.
1 parent a805928 commit 85ba2ed

5 files changed

Lines changed: 402 additions & 6 deletions

File tree

‎src/Internal/Ast/Elimination/EliminationSafetyPolicy.php‎

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@
88
use PhpParser\Node\Expr\Closure;
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Function_;
11+
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
1112
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
13+
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
14+
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
15+
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
1216
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
1317
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
18+
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
19+
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
20+
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
21+
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
1422
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
23+
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
1524

1625
/**
1726
* Enforces LSP safety rules and runtime state invariants to determine
@@ -78,11 +87,8 @@ public static function isReturnEliminationSafe(
7887
return false;
7988
}
8089

81-
if ($returnTypeNode instanceof GenericTypeNode) {
82-
$baseName = strtolower((string) $returnTypeNode->type);
83-
if (! isset(self::PERMISSIBLE_NON_OBJECT_GENERICS[$baseName])) {
84-
return false;
85-
}
90+
if (self::containsObjectGeneric($returnTypeNode)) {
91+
return false;
8692
}
8793

8894
if ($classContext === null || $functionNode instanceof Function_ || $functionNode instanceof Closure || $functionNode instanceof ArrowFunction) {
@@ -156,4 +162,93 @@ public static function isGeneratorEliminationSafe(
156162

157163
return false;
158164
}
165+
166+
/**
167+
* Recursively checks whether a TypeNode contains any object GenericTypeNode
168+
* (e.g. Collection<Dog>, ?Collection<Dog>, Collection<Dog>|null, list<Collection<Dog>>,
169+
* array{c: Collection<Dog>}) whose runtime WeakMap template bindings must be preserved.
170+
*/
171+
public static function containsObjectGeneric(TypeNode $typeNode): bool
172+
{
173+
while ($typeNode instanceof NullableTypeNode || $typeNode instanceof ArrayTypeNode) {
174+
$typeNode = $typeNode->type;
175+
}
176+
177+
if ($typeNode instanceof GenericTypeNode) {
178+
$baseName = strtolower((string) $typeNode->type);
179+
if (! isset(self::PERMISSIBLE_NON_OBJECT_GENERICS[$baseName])) {
180+
return true;
181+
}
182+
183+
foreach ($typeNode->genericTypes as $genericArg) {
184+
if (self::containsObjectGeneric($genericArg)) {
185+
return true;
186+
}
187+
}
188+
189+
return false;
190+
}
191+
192+
if ($typeNode instanceof UnionTypeNode || $typeNode instanceof IntersectionTypeNode) {
193+
foreach ($typeNode->types as $member) {
194+
if (self::containsObjectGeneric($member)) {
195+
return true;
196+
}
197+
}
198+
199+
return false;
200+
}
201+
202+
if ($typeNode instanceof ArrayShapeNode) {
203+
foreach ($typeNode->items as $item) {
204+
if (self::containsObjectGeneric($item->valueType)) {
205+
return true;
206+
}
207+
}
208+
209+
if ($typeNode->unsealedType !== null) {
210+
if (self::containsObjectGeneric($typeNode->unsealedType->valueType)) {
211+
return true;
212+
}
213+
214+
if ($typeNode->unsealedType->keyType !== null && self::containsObjectGeneric($typeNode->unsealedType->keyType)) {
215+
return true;
216+
}
217+
}
218+
219+
return false;
220+
}
221+
222+
if ($typeNode instanceof ObjectShapeNode) {
223+
foreach ($typeNode->items as $item) {
224+
if (self::containsObjectGeneric($item->valueType)) {
225+
return true;
226+
}
227+
}
228+
229+
return false;
230+
}
231+
232+
if ($typeNode instanceof CallableTypeNode) {
233+
foreach ($typeNode->parameters as $param) {
234+
if (self::containsObjectGeneric($param->type)) {
235+
return true;
236+
}
237+
}
238+
239+
return self::containsObjectGeneric($typeNode->returnType);
240+
}
241+
242+
if ($typeNode instanceof ConditionalTypeNode || $typeNode instanceof ConditionalTypeForParameterNode) {
243+
return self::containsObjectGeneric($typeNode->if)
244+
|| self::containsObjectGeneric($typeNode->else);
245+
}
246+
247+
if ($typeNode instanceof OffsetAccessTypeNode) {
248+
return self::containsObjectGeneric($typeNode->type)
249+
|| self::containsObjectGeneric($typeNode->offset);
250+
}
251+
252+
return false;
253+
}
159254
}

‎src/Internal/Ast/Elimination/StaticTypeEvaluator.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ public static function satisfies(
7676
}
7777
}
7878

79-
if ($expr instanceof Variable && \is_string($expr->name) && isset($trustedVariableTypes[$expr->name])) {
79+
if (
80+
$expr instanceof Variable
81+
&& \is_string($expr->name)
82+
&& isset($trustedVariableTypes[$expr->name])
83+
&& ! EliminationSafetyPolicy::containsObjectGeneric($typeNode)
84+
) {
8085
$resolvedTarget = $currentFile !== ''
8186
? SpecialTypeResolver::resolveForFile($typeNode, $currentFile, $currentNamespace, false)
8287
: $typeNode;

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ public static function inlineVarSubsumesReturn(
18421842
if (
18431843
! $isEliminationSafe
18441844
|| $targetReturnType === null
1845+
|| EliminationSafetyPolicy::containsObjectGeneric($targetReturnType)
18451846
|| ! Config::isInlineScalarsEnabled()
18461847
|| ! Config::isInlineArraysEnabled()
18471848
|| ! Config::isInlineObjectsEnabled()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Domain;
6+
7+
/**
8+
* @template T
9+
*/
10+
final class EliminationBox
11+
{
12+
/**
13+
* @var T|null
14+
*/
15+
public mixed $item = null;
16+
17+
/**
18+
* @param T $item
19+
*/
20+
public function set(mixed $item): void
21+
{
22+
$this->item = $item;
23+
}
24+
}

0 commit comments

Comments
 (0)