Skip to content

Commit e11a6ee

Browse files
committed
Enhance BlockScopeShadowingTest to validate outer scope restoration after exiting bare blocks
1 parent 925957a commit e11a6ee

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ private function wrapClone(Node\Expr\Clone_ $node): ?Node\Expr\FuncCall
931931
private function isSoftScopeBoundary(Node $node): bool
932932
{
933933
return $node instanceof Node\Expr\ArrowFunction
934+
|| $node instanceof Node\Stmt\Block
934935
|| $node instanceof Node\Stmt\If_
935936
|| $node instanceof Node\Stmt\Else_
936937
|| $node instanceof Node\Stmt\ElseIf_

‎tests/TypeChecking/Boundaries/BlockScopeShadowingTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@
3434
$x = -50;
3535
})->toThrow(TypeError::class, 'Variable $x must be of type positive-int');
3636
});
37+
38+
test('restores outer scope variable contract after exiting a bare block', function () {
39+
/** @var positive-int $a */
40+
$a = 10;
41+
{
42+
/** @var non-empty-string $a */
43+
$a = 'inner';
44+
expect($a)->toBe('inner');
45+
}
46+
47+
// Outside bare block, $a reverts back to outer contract: positive-int!
48+
expect(function () use (&$a) {
49+
$a = -1;
50+
})->toThrow(TypeError::class, 'Variable $a must be of type positive-int');
51+
});
3752
});

0 commit comments

Comments
 (0)