Skip to content

Commit a0a3d9b

Browse files
authored
Minor Bug Fixes regarding discovered edge cases (#138)
* Add comprehensive tests for unset mutation enforcement and line number preservation * Add tests for Priority 2 Bugs: Cache Isolation, AST Printer & Class Constants * Enhance type validation and error handling in ConstValidator and RuntimeTypeChecker; add new tests for alias constant resolution and inherited class-level aliases * Refactor code style and add new tests for Priority 3 Bugs: Cross-Namespace Static Elimination, Aliased Callables, and Opcode Fast-Paths * Enhance type handling and validation for ArrayAccess and callable types * Refactor ContractVisitor and FunctionContractInjector for improved alias resolution and make sure autoloading is disabled during ast transformation time; enhance Config reset logic to include ContractVisitor reset; streamline StructureMutator's unset logic * Add PHP 8.6 to the testing matrix in CI configuration * Update vimeo/psalm dependency to version 7.0.0 in composer.json * Enhance ContractVisitor and FunctionContractInjector for improved scope management; add tests for Priority 4 Bugs related to scope isolation and docblock heuristics
1 parent 65f358b commit a0a3d9b

34 files changed

Lines changed: 5492 additions & 355 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
fail-fast: false
8383
matrix:
8484
os: [ubuntu-latest, windows-latest, macos-latest]
85-
php: ['8.1', '8.2', '8.3', '8.4', '8.5']
85+
php: ['8.1', '8.2', '8.3', '8.4', '8.5', '8.6']
8686

8787
steps:
8888
- name: Checkout Code

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"phpstan/phpstan": "^2.1",
3636
"phpstan/phpstan-strict-rules": "^2.0",
3737
"phpstan/extension-installer": "^1.4",
38-
"vimeo/psalm": "^6.16",
38+
"vimeo/psalm": "^7.0.0",
3939
"carthage-software/mago": "^1.47",
4040
"rector/rector": "^2.6",
4141
"friendsofphp/php-cs-fixer": "^3.95"

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 897 additions & 27 deletions
Large diffs are not rendered by default.

‎src/Internal/Ast/Elimination/Evaluator/ArrayLiteralEvaluator.php‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public static function satisfies(
5858
Array_ $arrayExpr,
5959
TypeNode $typeNode,
6060
string $currentFile = '',
61-
array $trustedVariableTypes = []
61+
array $trustedVariableTypes = [],
62+
?string $currentNamespace = null
6263
): bool {
6364
$count = \count($arrayExpr->items);
6465

@@ -71,15 +72,15 @@ public static function satisfies(
7172
}
7273

7374
if ($typeNode instanceof ArrayTypeNode) {
74-
return self::satisfiesList($arrayExpr, $typeNode->type, false, $currentFile, $trustedVariableTypes);
75+
return self::satisfiesList($arrayExpr, $typeNode->type, false, $currentFile, $trustedVariableTypes, $currentNamespace);
7576
}
7677

7778
if ($typeNode instanceof GenericTypeNode) {
78-
return self::satisfiesGenericArray($arrayExpr, $typeNode, $currentFile, $trustedVariableTypes);
79+
return self::satisfiesGenericArray($arrayExpr, $typeNode, $currentFile, $trustedVariableTypes, $currentNamespace);
7980
}
8081

8182
if ($typeNode instanceof ArrayShapeNode) {
82-
return self::satisfiesArrayShape($arrayExpr, $typeNode, $currentFile, $trustedVariableTypes);
83+
return self::satisfiesArrayShape($arrayExpr, $typeNode, $currentFile, $trustedVariableTypes, $currentNamespace);
8384
}
8485

8586
return false;
@@ -138,33 +139,34 @@ private static function satisfiesGenericArray(
138139
Array_ $arrayExpr,
139140
GenericTypeNode $node,
140141
string $currentFile,
141-
array $trustedVariableTypes = []
142+
array $trustedVariableTypes = [],
143+
?string $currentNamespace = null
142144
): bool {
143145
$base = strtolower((string) $node->type);
144146
$genericCount = \count($node->genericTypes);
145147

146148
if ($base === 'list' && $genericCount >= 1) {
147-
return self::satisfiesList($arrayExpr, $node->genericTypes[0], false, $currentFile, $trustedVariableTypes);
149+
return self::satisfiesList($arrayExpr, $node->genericTypes[0], false, $currentFile, $trustedVariableTypes, $currentNamespace);
148150
}
149151

150152
if ($base === 'non-empty-list' && $genericCount >= 1) {
151-
return self::satisfiesList($arrayExpr, $node->genericTypes[0], true, $currentFile, $trustedVariableTypes);
153+
return self::satisfiesList($arrayExpr, $node->genericTypes[0], true, $currentFile, $trustedVariableTypes, $currentNamespace);
152154
}
153155

154156
if (($base === 'array' || $base === 'iterable') && $genericCount === 1) {
155-
return self::satisfiesList($arrayExpr, $node->genericTypes[0], false, $currentFile, $trustedVariableTypes);
157+
return self::satisfiesList($arrayExpr, $node->genericTypes[0], false, $currentFile, $trustedVariableTypes, $currentNamespace);
156158
}
157159

158160
if (($base === 'array' || $base === 'iterable') && $genericCount >= 2) {
159-
return self::satisfiesKeyValueArray($arrayExpr, $node->genericTypes[0], $node->genericTypes[1], false, $currentFile, $trustedVariableTypes);
161+
return self::satisfiesKeyValueArray($arrayExpr, $node->genericTypes[0], $node->genericTypes[1], false, $currentFile, $trustedVariableTypes, $currentNamespace);
160162
}
161163

162164
if ($base === 'non-empty-array' && $genericCount === 1) {
163-
return self::satisfiesList($arrayExpr, $node->genericTypes[0], true, $currentFile, $trustedVariableTypes);
165+
return self::satisfiesList($arrayExpr, $node->genericTypes[0], true, $currentFile, $trustedVariableTypes, $currentNamespace);
164166
}
165167

166168
if ($base === 'non-empty-array' && $genericCount >= 2) {
167-
return self::satisfiesKeyValueArray($arrayExpr, $node->genericTypes[0], $node->genericTypes[1], true, $currentFile, $trustedVariableTypes);
169+
return self::satisfiesKeyValueArray($arrayExpr, $node->genericTypes[0], $node->genericTypes[1], true, $currentFile, $trustedVariableTypes, $currentNamespace);
168170
}
169171

170172
return false;
@@ -178,7 +180,8 @@ private static function satisfiesList(
178180
TypeNode $itemType,
179181
bool $requireNonEmpty,
180182
string $currentFile,
181-
array $trustedVariableTypes = []
183+
array $trustedVariableTypes = [],
184+
?string $currentNamespace = null
182185
): bool {
183186
$count = \count($arrayExpr->items);
184187
if ($requireNonEmpty && $count === 0) {
@@ -198,7 +201,7 @@ private static function satisfiesList(
198201
}
199202
$expectedIdx++;
200203

201-
if (! StaticTypeEvaluator::satisfies($item->value, $itemType, $currentFile, $trustedVariableTypes)) {
204+
if (! StaticTypeEvaluator::satisfies($item->value, $itemType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
202205
return false;
203206
}
204207
}
@@ -215,7 +218,8 @@ private static function satisfiesKeyValueArray(
215218
TypeNode $valueType,
216219
bool $requireNonEmpty,
217220
string $currentFile,
218-
array $trustedVariableTypes = []
221+
array $trustedVariableTypes = [],
222+
?string $currentNamespace = null
219223
): bool {
220224
$count = \count($arrayExpr->items);
221225
if ($requireNonEmpty && $count === 0) {
@@ -240,11 +244,11 @@ private static function satisfiesKeyValueArray(
240244
return false;
241245
}
242246

243-
if (! StaticTypeEvaluator::satisfies($keyExpr, $keyType, $currentFile, $trustedVariableTypes)) {
247+
if (! StaticTypeEvaluator::satisfies($keyExpr, $keyType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
244248
return false;
245249
}
246250

247-
if (! StaticTypeEvaluator::satisfies($item->value, $valueType, $currentFile, $trustedVariableTypes)) {
251+
if (! StaticTypeEvaluator::satisfies($item->value, $valueType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
248252
return false;
249253
}
250254
}
@@ -259,7 +263,8 @@ private static function satisfiesArrayShape(
259263
Array_ $arrayExpr,
260264
ArrayShapeNode $shapeNode,
261265
string $currentFile,
262-
array $trustedVariableTypes = []
266+
array $trustedVariableTypes = [],
267+
?string $currentNamespace = null
263268
): bool {
264269
if ($shapeNode->kind === ArrayShapeNode::KIND_LIST && ! self::isSequentialList($arrayExpr)) {
265270
return false;
@@ -314,7 +319,7 @@ private static function satisfiesArrayShape(
314319
continue;
315320
}
316321

317-
if (! StaticTypeEvaluator::satisfies($actualItems[$expectedKey], $shapeItem->valueType, $currentFile, $trustedVariableTypes)) {
322+
if (! StaticTypeEvaluator::satisfies($actualItems[$expectedKey], $shapeItem->valueType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
318323
return false;
319324
}
320325
}
@@ -331,12 +336,12 @@ private static function satisfiesArrayShape(
331336
if ($shapeNode->unsealedType !== null) {
332337
if ($shapeNode->unsealedType->keyType !== null) {
333338
$keyExpr = \is_int($actualKey) ? new LNumber($actualKey) : new String_($actualKey);
334-
if (! StaticTypeEvaluator::satisfies($keyExpr, $shapeNode->unsealedType->keyType, $currentFile, $trustedVariableTypes)) {
339+
if (! StaticTypeEvaluator::satisfies($keyExpr, $shapeNode->unsealedType->keyType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
335340
return false;
336341
}
337342
}
338343

339-
if (! StaticTypeEvaluator::satisfies($actualValueExpr, $shapeNode->unsealedType->valueType, $currentFile, $trustedVariableTypes)) {
344+
if (! StaticTypeEvaluator::satisfies($actualValueExpr, $shapeNode->unsealedType->valueType, $currentFile, $trustedVariableTypes, $currentNamespace)) {
340345
return false;
341346
}
342347
}

‎src/Internal/Ast/Elimination/Evaluator/ConstantAndEnumEvaluator.php‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,22 @@ final class ConstantAndEnumEvaluator
4747
* 2. Evaluates bitwise OR expressions (A | B) against `int-mask-of` wildcards.
4848
* 3. Evaluates backed enum property reads (Enum::Case->value) against `value-of<Enum>`.
4949
*/
50-
public static function satisfies(Expr $expr, TypeNode $typeNode, string $currentFile = ''): bool
51-
{
50+
public static function satisfies(
51+
Expr $expr,
52+
TypeNode $typeNode,
53+
string $currentFile = '',
54+
?string $currentNamespace = null
55+
): bool {
5256
if ($expr instanceof ClassConstFetch) {
53-
return self::satisfiesClassConst($expr, $typeNode, $currentFile);
57+
return self::satisfiesClassConst($expr, $typeNode, $currentFile, $currentNamespace);
5458
}
5559

5660
if ($expr instanceof BitwiseOr) {
57-
return self::satisfiesBitwiseOr($expr, $typeNode, $currentFile);
61+
return self::satisfiesBitwiseOr($expr, $typeNode, $currentFile, $currentNamespace);
5862
}
5963

6064
if ($expr instanceof PropertyFetch) {
61-
return self::satisfiesEnumBackingValue($expr, $typeNode, $currentFile);
65+
return self::satisfiesEnumBackingValue($expr, $typeNode, $currentFile, $currentNamespace);
6266
}
6367

6468
return false;
@@ -67,8 +71,12 @@ public static function satisfies(Expr $expr, TypeNode $typeNode, string $current
6771
/**
6872
* Evaluates a class constant fetch against ConstTypeNode, IdentifierTypeNode, or int-mask-of.
6973
*/
70-
private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $typeNode, string $currentFile): bool
71-
{
74+
private static function satisfiesClassConst(
75+
ClassConstFetch $fetch,
76+
TypeNode $typeNode,
77+
string $currentFile,
78+
?string $currentNamespace = null
79+
): bool {
7280
if (! ($fetch->class instanceof Name) || ! ($fetch->name instanceof Identifier)) {
7381
return false;
7482
}
@@ -81,7 +89,7 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
8189
$targetClassName = $fetch->class->toString();
8290

8391
if ($typeNode instanceof ConstTypeNode && $typeNode->constExpr instanceof ConstFetchNode) {
84-
if (! self::classesMatch($targetClassName, $typeNode->constExpr->className, $currentFile)) {
92+
if (! self::classesMatch($targetClassName, $typeNode->constExpr->className, $currentFile, $currentNamespace)) {
8593
return false;
8694
}
8795

@@ -95,11 +103,11 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
95103
}
96104

97105
if ($typeNode instanceof IdentifierTypeNode) {
98-
if (! self::classesMatch($targetClassName, $typeNode->name, $currentFile)) {
106+
if (! self::classesMatch($targetClassName, $typeNode->name, $currentFile, $currentNamespace)) {
99107
return false;
100108
}
101109

102-
$resolvedEnum = self::resolveClassForReflection($targetClassName, $currentFile);
110+
$resolvedEnum = self::resolveClassForReflection($targetClassName, $currentFile, $currentNamespace);
103111
if ($resolvedEnum === '' || ! enum_exists($resolvedEnum, false)) {
104112
return false;
105113
}
@@ -110,7 +118,7 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
110118
if ($typeNode instanceof GenericTypeNode && strtolower((string) $typeNode->type) === 'int-mask-of') {
111119
$inner = $typeNode->genericTypes[0] ?? null;
112120
if ($inner instanceof ConstTypeNode && $inner->constExpr instanceof ConstFetchNode) {
113-
if (! self::classesMatch($targetClassName, $inner->constExpr->className, $currentFile)) {
121+
if (! self::classesMatch($targetClassName, $inner->constExpr->className, $currentFile, $currentNamespace)) {
114122
return false;
115123
}
116124

@@ -129,21 +137,29 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
129137
/**
130138
* Recursively verifies that both operands of a bitwise OR expression satisfy an `int-mask-of` wildcard.
131139
*/
132-
private static function satisfiesBitwiseOr(BitwiseOr $expr, TypeNode $typeNode, string $currentFile): bool
133-
{
140+
private static function satisfiesBitwiseOr(
141+
BitwiseOr $expr,
142+
TypeNode $typeNode,
143+
string $currentFile,
144+
?string $currentNamespace = null
145+
): bool {
134146
if (! ($typeNode instanceof GenericTypeNode) || strtolower((string) $typeNode->type) !== 'int-mask-of') {
135147
return false;
136148
}
137149

138-
return self::satisfies($expr->left, $typeNode, $currentFile)
139-
&& self::satisfies($expr->right, $typeNode, $currentFile);
150+
return self::satisfies($expr->left, $typeNode, $currentFile, $currentNamespace)
151+
&& self::satisfies($expr->right, $typeNode, $currentFile, $currentNamespace);
140152
}
141153

142154
/**
143155
* Verifies whether a BackedEnum::Case->value property access satisfies `value-of<Enum>`.
144156
*/
145-
private static function satisfiesEnumBackingValue(PropertyFetch $fetch, TypeNode $typeNode, string $currentFile): bool
146-
{
157+
private static function satisfiesEnumBackingValue(
158+
PropertyFetch $fetch,
159+
TypeNode $typeNode,
160+
string $currentFile,
161+
?string $currentNamespace = null
162+
): bool {
147163
if (! ($fetch->name instanceof Identifier) || $fetch->name->name !== 'value') {
148164
return false;
149165
}
@@ -168,11 +184,11 @@ private static function satisfiesEnumBackingValue(PropertyFetch $fetch, TypeNode
168184
return false;
169185
}
170186

171-
if (! self::classesMatch($enumClassName, $enumTarget->name, $currentFile)) {
187+
if (! self::classesMatch($enumClassName, $enumTarget->name, $currentFile, $currentNamespace)) {
172188
return false;
173189
}
174190

175-
$resolvedEnum = self::resolveClassForReflection($enumClassName, $currentFile);
191+
$resolvedEnum = self::resolveClassForReflection($enumClassName, $currentFile, $currentNamespace);
176192
if ($resolvedEnum === '' || ! enum_exists($resolvedEnum, false) || ! is_subclass_of($resolvedEnum, BackedEnum::class)) {
177193
return false;
178194
}
@@ -183,7 +199,7 @@ private static function satisfiesEnumBackingValue(PropertyFetch $fetch, TypeNode
183199
/**
184200
* Verifies whether two class references deterministically denote the same class/enum.
185201
*/
186-
private static function classesMatch(string $fetchClass, string $docClass, string $currentFile): bool
202+
private static function classesMatch(string $fetchClass, string $docClass, string $currentFile, ?string $currentNamespace = null): bool
187203
{
188204
if ($docClass === '') {
189205
return false;
@@ -205,8 +221,8 @@ private static function classesMatch(string $fetchClass, string $docClass, strin
205221
}
206222

207223
if ($currentFile !== '') {
208-
$resolvedFetchClass = SpecialTypeResolver::resolveFqcnForFile($fetchClass, $currentFile, false);
209-
$resolvedTypeClass = SpecialTypeResolver::resolveFqcnForFile($docClass, $currentFile, false);
224+
$resolvedFetchClass = SpecialTypeResolver::resolveFqcnForFile($fetchClass, $currentFile, false, $currentNamespace);
225+
$resolvedTypeClass = SpecialTypeResolver::resolveFqcnForFile($docClass, $currentFile, false, $currentNamespace);
210226

211227
return $resolvedFetchClass !== '' && $resolvedFetchClass === $resolvedTypeClass;
212228
}
@@ -217,10 +233,10 @@ private static function classesMatch(string $fetchClass, string $docClass, strin
217233
/**
218234
* Resolves a class name without invoking autoloading.
219235
*/
220-
private static function resolveClassForReflection(string $rawClass, string $currentFile): string
236+
private static function resolveClassForReflection(string $rawClass, string $currentFile, ?string $currentNamespace = null): string
221237
{
222238
if ($currentFile !== '') {
223-
return SpecialTypeResolver::resolveFqcnForFile($rawClass, $currentFile, false);
239+
return SpecialTypeResolver::resolveFqcnForFile($rawClass, $currentFile, false, $currentNamespace);
224240
}
225241

226242
return ltrim($rawClass, '\\');

‎src/Internal/Ast/Elimination/Evaluator/GeneratorEvaluator.php‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,23 @@ public static function canEliminateYield(
125125
?TypeNode $keyType,
126126
?TypeNode $valueType,
127127
string $currentFile = '',
128-
?Expr $precedingValueExpr = null
128+
?Expr $precedingValueExpr = null,
129+
?string $currentNamespace = null
129130
): bool {
130131
if (! Config::isEliminateStaticallyProvenChecksEnabled()) {
131132
return false;
132133
}
133134

134135
if ($keyType !== null && ! self::isMixedType($keyType)) {
135136
$keyExpr = $yieldNode->key ?? new LNumber(0);
136-
if (! StaticTypeEvaluator::satisfies($keyExpr, $keyType, $currentFile)) {
137+
if (! StaticTypeEvaluator::satisfies($keyExpr, $keyType, $currentFile, [], $currentNamespace)) {
137138
return false;
138139
}
139140
}
140141

141142
if ($valueType !== null && ! self::isMixedType($valueType)) {
142143
$valExpr = $precedingValueExpr ?? $yieldNode->value ?? new ConstFetch(new Name('null'));
143-
if (! StaticTypeEvaluator::satisfies($valExpr, $valueType, $currentFile)) {
144+
if (! StaticTypeEvaluator::satisfies($valExpr, $valueType, $currentFile, [], $currentNamespace)) {
144145
return false;
145146
}
146147
}
@@ -168,7 +169,8 @@ public static function canEliminateYieldFrom(
168169
YieldFrom $yieldFromNode,
169170
?TypeNode $keyType,
170171
?TypeNode $valueType,
171-
string $currentFile = ''
172+
string $currentFile = '',
173+
?string $currentNamespace = null
172174
): bool {
173175
if (! Config::isEliminateStaticallyProvenChecksEnabled()) {
174176
return false;
@@ -186,13 +188,13 @@ public static function canEliminateYieldFrom(
186188
/** @var TypeNode $valueType */
187189
$targetType = new GenericTypeNode(new IdentifierTypeNode('iterable'), [$valueType]);
188190

189-
return ArrayLiteralEvaluator::satisfies($yieldFromNode->expr, $targetType, $currentFile);
191+
return ArrayLiteralEvaluator::satisfies($yieldFromNode->expr, $targetType, $currentFile, [], $currentNamespace);
190192
}
191193

192194
$effectiveValueType = $valueType ?? new IdentifierTypeNode('mixed');
193195
$targetType = new GenericTypeNode(new IdentifierTypeNode('iterable'), [$keyType, $effectiveValueType]);
194196

195-
return ArrayLiteralEvaluator::satisfies($yieldFromNode->expr, $targetType, $currentFile);
197+
return ArrayLiteralEvaluator::satisfies($yieldFromNode->expr, $targetType, $currentFile, [], $currentNamespace);
196198
}
197199

198200
/**
@@ -204,7 +206,8 @@ public static function canEliminateGeneratorReturn(
204206
?Expr $returnExpr,
205207
?TypeNode $returnType,
206208
string $currentFile = '',
207-
array $trustedVariableTypes = []
209+
array $trustedVariableTypes = [],
210+
?string $currentNamespace = null
208211
): bool {
209212
if (! Config::isEliminateStaticallyProvenChecksEnabled()) {
210213
return false;
@@ -216,7 +219,7 @@ public static function canEliminateGeneratorReturn(
216219

217220
$effectiveExpr = $returnExpr ?? new ConstFetch(new Name('null'));
218221

219-
return StaticTypeEvaluator::satisfies($effectiveExpr, $returnType, $currentFile, $trustedVariableTypes);
222+
return StaticTypeEvaluator::satisfies($effectiveExpr, $returnType, $currentFile, $trustedVariableTypes, $currentNamespace);
220223
}
221224

222225
/**

0 commit comments

Comments
 (0)