Skip to content

Commit 610bfcd

Browse files
authored
Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows (#144)
* Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows 89 catch blocks across the transformer, docblock parser and runtime checkers discard the exception to stay fail-open. When one of them fires, a check is skipped or a file is left uninstrumented with no trace of why. Route every one of them through DebugLog::swallowed(), a no-op unless the TYPEPHP_DEBUG environment variable is truthy. When set, the exception class, message, throw site and catch site go to the PHP error log. Default behaviour is unchanged. Catch blocks that rethrow (CallableWrapper) or already report the exception (RunCommand) are left as they are. * Exclude DebugLog::swallowed() call sites from line coverage The catch blocks only run when TypePHP hits an internal error, so most of them are never reached by the test suite. Mark each call with @codeCoverageIgnore, as requested in review.
1 parent c12f8e8 commit 610bfcd

23 files changed

Lines changed: 277 additions & 40 deletions

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
2626
use PHPStan\PhpDocParser\Parser\TokenIterator;
2727
use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator;
28+
use TypePHP\Internal\Diagnostic\DebugLog;
2829
use TypePHP\Internal\Docblock\DocblockExtractor;
2930
use TypePHP\Internal\Docblock\DocblockNormalizer;
3031
use TypePHP\Internal\Docblock\DocblockParser;
@@ -540,7 +541,8 @@ private function collectUnprovenClassConstantChecks(Node\Stmt\Class_ $node, stri
540541
$checkStmt->setAttribute('typephp_injected', true);
541542
$injectedStmts[] = $checkStmt;
542543
}
543-
} catch (\Throwable) {
544+
} catch (\Throwable $e) {
545+
DebugLog::swallowed($e); // @codeCoverageIgnore
544546
}
545547
}
546548

@@ -603,7 +605,8 @@ private function collectUnprovenGlobalConstantChecks(Node\Stmt\Const_ $node): ar
603605
$checkStmt->setAttribute('typephp_injected', true);
604606
$injectedStmts[] = $checkStmt;
605607
}
606-
} catch (\Throwable) {
608+
} catch (\Throwable $e) {
609+
DebugLog::swallowed($e); // @codeCoverageIgnore
607610
}
608611

609612
return $injectedStmts;
@@ -901,7 +904,8 @@ private function processClassPropertyDefaults(Node\Stmt\Class_ $node): void
901904
if ($classTemplates !== [] && DocblockParser::typeReferencesTemplate($propTypeNode, $classTemplates)) {
902905
continue;
903906
}
904-
} catch (\Throwable) {
907+
} catch (\Throwable $e) {
908+
DebugLog::swallowed($e); // @codeCoverageIgnore
905909
}
906910
}
907911

@@ -984,7 +988,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
984988
try {
985989
$phpDocNode = DocblockExtractor::parseDocString($nodeDoc->getText());
986990
$classTemplates = DocblockExtractor::extractTemplates($phpDocNode);
987-
} catch (\Throwable) {
991+
} catch (\Throwable $e) {
992+
DebugLog::swallowed($e); // @codeCoverageIgnore
988993
}
989994
}
990995

@@ -994,7 +999,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
994999
try {
9951000
$loadedTemplates = DocblockParser::parseClassTemplates($className);
9961001
$classTemplates = [...$classTemplates, ...$loadedTemplates];
997-
} catch (\Throwable) {
1002+
} catch (\Throwable $e) {
1003+
DebugLog::swallowed($e); // @codeCoverageIgnore
9981004
}
9991005
}
10001006
}
@@ -1006,7 +1012,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
10061012
try {
10071013
$parentTemplates = DocblockParser::parseClassTemplates($parentClass);
10081014
$classTemplates = [...$classTemplates, ...$parentTemplates];
1009-
} catch (\Throwable) {
1015+
} catch (\Throwable $e) {
1016+
DebugLog::swallowed($e); // @codeCoverageIgnore
10101017
}
10111018
} else {
10121019
$parentFile = self::findClassFileWithoutAutoload($parentClass);
@@ -1022,7 +1029,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
10221029
$classTemplates = [...$classTemplates, ...DocblockExtractor::extractTemplates($phpDocNode)];
10231030
}
10241031
}
1025-
} catch (\Throwable) {
1032+
} catch (\Throwable $e) {
1033+
DebugLog::swallowed($e); // @codeCoverageIgnore
10261034
}
10271035
}
10281036
}
@@ -1104,7 +1112,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
11041112
try {
11051113
$phpDocNode = DocblockExtractor::parseDocString($doc->getText());
11061114
$templates = DocblockExtractor::extractTemplates($phpDocNode);
1107-
} catch (\Throwable) {
1115+
} catch (\Throwable $e) {
1116+
DebugLog::swallowed($e); // @codeCoverageIgnore
11081117
}
11091118
}
11101119
$hasTemplates = $templates !== [];
@@ -1194,7 +1203,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
11941203
try {
11951204
$methodPhpDoc = DocblockExtractor::parseDocString($methodDoc->getText());
11961205
$methodParamTags = DocblockExtractor::getParamTags($methodPhpDoc);
1197-
} catch (\Throwable) {
1206+
} catch (\Throwable $e) {
1207+
DebugLog::swallowed($e); // @codeCoverageIgnore
11981208
}
11991209
}
12001210

@@ -1261,7 +1271,8 @@ private function extractClassLikeAliases(
12611271
try {
12621272
$phpDocNode = DocblockExtractor::parseDocString($doc->getText());
12631273
$this->collectAliasesFromPhpDocInFile($phpDocNode, $aliases, $this->currentFile, $this->currentNamespace, $visited);
1264-
} catch (\Throwable) {
1274+
} catch (\Throwable $e) {
1275+
DebugLog::swallowed($e); // @codeCoverageIgnore
12651276
}
12661277
}
12671278

@@ -1412,7 +1423,9 @@ class_exists($fqcn, false)
14121423
self::collectRawAliasesFromAstDoc($phpDocNode, $aliases, '', '', [], $localClassAliases, $visited);
14131424

14141425
return self::$astClassAliasesCache[$fqcn] = $aliases;
1415-
} catch (\Throwable) {
1426+
} catch (\Throwable $e) {
1427+
DebugLog::swallowed($e); // @codeCoverageIgnore
1428+
14161429
return self::$astClassAliasesCache[$fqcn] = [];
14171430
}
14181431
}
@@ -1443,7 +1456,8 @@ class_exists($fqcn, false)
14431456
}
14441457

14451458
$aliases = self::extractTargetClassAliasesFromStmts($stmts, $fqcn, $filePath, $localClassAliases, $visited);
1446-
} catch (\Throwable) {
1459+
} catch (\Throwable $e) {
1460+
DebugLog::swallowed($e); // @codeCoverageIgnore
14471461
$aliases = [];
14481462
}
14491463

@@ -1545,7 +1559,8 @@ private static function scanClassLikesForAliases(
15451559
try {
15461560
$phpDocNode = DocblockExtractor::parseDocString($doc->getText());
15471561
self::collectRawAliasesFromAstDoc($phpDocNode, $aliases, $filePath, $namespace, $imports, $localClassAliases, $visited);
1548-
} catch (\Throwable) {
1562+
} catch (\Throwable $e) {
1563+
DebugLog::swallowed($e); // @codeCoverageIgnore
15491564
}
15501565
}
15511566

@@ -1893,6 +1908,7 @@ private function propagateShapeTypesToDestructuring(Node\Expr\List_|Node\Expr\Ar
18931908
}
18941909
}
18951910
} catch (\Throwable $e) {
1911+
DebugLog::swallowed($e); // @codeCoverageIgnore
18961912
}
18971913
}
18981914

@@ -1940,7 +1956,8 @@ private function handleAssign(Node\Expr\Assign $node): ?Node
19401956
[$typeParser, $lexer] = DocblockExtractor::getTypeParserComponents();
19411957
$tokens = new TokenIterator($lexer->tokenize(DocblockNormalizer::normalize($typeString)));
19421958
$parsedType = $typeParser->parse($tokens);
1943-
} catch (\Throwable) {
1959+
} catch (\Throwable $e) {
1960+
DebugLog::swallowed($e); // @codeCoverageIgnore
19441961
}
19451962

19461963
if ($expr instanceof Node\Expr\New_ && $parsedType !== null) {
@@ -2758,7 +2775,8 @@ private function registerParamInferredTypes(array $params, ?string $docText = nu
27582775
foreach (DocblockExtractor::getParamTags($phpDocNode) as $pName => $pTag) {
27592776
$docParamTypes[$pName] = (string) $pTag->type;
27602777
}
2761-
} catch (\Throwable) {
2778+
} catch (\Throwable $e) {
2779+
DebugLog::swallowed($e); // @codeCoverageIgnore
27622780
}
27632781
}
27642782

@@ -3286,7 +3304,8 @@ private function parseUnsetTypeCached(string $typeString): ?TypeNode
32863304
self::$parsedUnsetTypeCache[$typeString] = $parsed;
32873305

32883306
return $parsed;
3289-
} catch (\Throwable) {
3307+
} catch (\Throwable $e) {
3308+
DebugLog::swallowed($e); // @codeCoverageIgnore
32903309
self::$parsedUnsetTypeCache[$typeString] = false;
32913310

32923311
return null;

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use TypePHP\Internal\Ast\Elimination\Evaluator\GeneratorEvaluator;
2828
use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator;
2929
use TypePHP\Internal\Checker\ConditionalChecker;
30+
use TypePHP\Internal\Diagnostic\DebugLog;
3031
use TypePHP\Internal\Docblock\DocblockExtractor;
3132
use TypePHP\Internal\Docblock\DocblockNormalizer;
3233
use TypePHP\Internal\Docblock\DocblockParser;
@@ -119,7 +120,8 @@ public static function inject(
119120
$paramTags = DocblockExtractor::getParamTags($phpDocNode);
120121
$paramOutTags = DocblockExtractor::getParamOutTags($phpDocNode);
121122
$selfOutType = DocblockExtractor::getSelfOutTag($phpDocNode);
122-
} catch (\Throwable) {
123+
} catch (\Throwable $e) {
124+
DebugLog::swallowed($e); // @codeCoverageIgnore
123125
}
124126
}
125127

@@ -479,7 +481,8 @@ private static function collectTrustedParamTypes(
479481
try {
480482
$phpDocNode = DocblockExtractor::parseDocString($doc->getText());
481483
$paramTags = DocblockExtractor::getParamTags($phpDocNode);
482-
} catch (\Throwable) {
484+
} catch (\Throwable $e) {
485+
DebugLog::swallowed($e); // @codeCoverageIgnore
483486
}
484487
}
485488

@@ -1767,7 +1770,9 @@ public static function inlineVarSubsumesReturn(
17671770
$targetReturnType,
17681771
GenericTypeNode::VARIANCE_COVARIANT
17691772
);
1770-
} catch (\Throwable) {
1773+
} catch (\Throwable $e) {
1774+
DebugLog::swallowed($e); // @codeCoverageIgnore
1775+
17711776
return false;
17721777
}
17731778
}

‎src/Internal/Ast/ScopeManager.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
8+
use TypePHP\Internal\Diagnostic\DebugLog;
89
use TypePHP\Internal\Docblock\DocblockExtractor;
910

1011
/**
@@ -211,6 +212,7 @@ public function extractVarDocblock(string $docText, ?Node\Expr $expr = null): vo
211212
}
212213
}
213214
} catch (\Throwable $e) {
215+
DebugLog::swallowed($e); // @codeCoverageIgnore
214216
// Silently ignore malformed docblocks
215217
}
216218
}

‎src/Internal/Ast/TypeDeducer.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
1818
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1919
use PHPStan\PhpDocParser\Parser\TokenIterator;
20+
use TypePHP\Internal\Diagnostic\DebugLog;
2021
use TypePHP\Internal\Docblock\DocblockExtractor;
2122
use TypePHP\Internal\Docblock\DocblockNormalizer;
2223
use TypePHP\Internal\Docblock\DocblockParser;
@@ -313,7 +314,9 @@ private static function parseTypeString(string $typeString): ?TypeNode
313314
$tokens = new TokenIterator($lexer->tokenize($normalized));
314315

315316
return self::$parsedTypeCache[$typeString] = $typeParser->parse($tokens);
316-
} catch (\Throwable) {
317+
} catch (\Throwable $e) {
318+
DebugLog::swallowed($e); // @codeCoverageIgnore
319+
317320
return null;
318321
}
319322
}

‎src/Internal/Checker/ConditionalChecker.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ReflectionClass;
2525
use Throwable;
2626
use Traversable;
27+
use TypePHP\Internal\Diagnostic\DebugLog;
2728
use TypePHP\Internal\Docblock\DocblockParser;
2829
use TypePHP\Internal\Generics\TemplateManager;
2930
use TypePHP\Internal\Resolver\HierarchyResolver;
@@ -479,6 +480,7 @@ private static function resolveRenamedParamValue(string $function, string $param
479480
}
480481
}
481482
} catch (Throwable $e) {
483+
DebugLog::swallowed($e); // @codeCoverageIgnore
482484
// Silently ignore reflection errors
483485
}
484486

‎src/Internal/Checker/InlineChecker.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPStan\PhpDocParser\Parser\TokenIterator;
2525
use PHPStan\PhpDocParser\Parser\TypeParser;
2626
use PHPStan\PhpDocParser\ParserConfig;
27+
use TypePHP\Internal\Diagnostic\DebugLog;
2728
use TypePHP\Internal\Diagnostic\ErrorFactory;
2829
use TypePHP\Internal\Diagnostic\ErrorMessage;
2930
use TypePHP\Internal\Docblock\DocblockNormalizer;
@@ -266,6 +267,7 @@ public static function checkVariable(
266267
$value = self::wrapCollectionCallables($typeNode, $value, $context, $registry);
267268
}
268269
} catch (\Throwable $e) {
270+
DebugLog::swallowed($e); // @codeCoverageIgnore
269271
// Silently ignore unexpected execution exceptions
270272
}
271273

@@ -373,6 +375,7 @@ public static function validateCollectionElementMutation(
373375
return $registry->validate($value, $typeNode->type, $context);
374376
}
375377
} catch (\Throwable $e) {
378+
DebugLog::swallowed($e); // @codeCoverageIgnore
376379
// Silently fall back to prospective whole-structure validation
377380
}
378381

@@ -572,6 +575,7 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
572575
TemplateManager::markFallbackReturnBindings($value, $typeNode, $fallbackMeta[1], $fallbackMeta[0], $preExistingBindings);
573576
}
574577
} catch (\Throwable $e) {
578+
DebugLog::swallowed($e); // @codeCoverageIgnore
575579
// Silently ignore unexpected execution exceptions
576580
}
577581

@@ -625,6 +629,7 @@ private static function classHasSelfOut(string $className): bool
625629
}
626630
}
627631
} catch (\Throwable $e) {
632+
DebugLog::swallowed($e); // @codeCoverageIgnore
628633
// Silently ignore reflection errors
629634
}
630635

@@ -780,6 +785,7 @@ private static function resolveFunctionContextWithFallback(TypeNode $typeNode, s
780785
$typeNode = SpecialTypeResolver::resolve($typeNode, $refFunc);
781786
}
782787
} catch (\ReflectionException $e) {
788+
DebugLog::swallowed($e); // @codeCoverageIgnore
783789
// Silently continue if reflection fails
784790
}
785791

@@ -859,6 +865,7 @@ private static function resolveClassContextWithFallback(
859865
$typeNode = SpecialTypeResolver::resolve($typeNode, $refClass);
860866
}
861867
} catch (\ReflectionException $e) {
868+
DebugLog::swallowed($e); // @codeCoverageIgnore
862869
// Silently continue if reflection fails
863870
}
864871

@@ -916,6 +923,7 @@ private static function substitutePropertyGenericsWithFallback(TypeNode $typeNod
916923
$refClass = new \ReflectionClass($className);
917924
$typeNode = SpecialTypeResolver::resolve($subNode, $refClass);
918925
} catch (\ReflectionException $e) {
926+
DebugLog::swallowed($e); // @codeCoverageIgnore
919927
$typeNode = $subNode;
920928
}
921929
} else {
@@ -973,6 +981,7 @@ private static function substituteStaticPropertyGenerics(TypeNode $typeNode, str
973981
$refClass = new \ReflectionClass($className);
974982
$typeNode = SpecialTypeResolver::resolve($typeNode, $refClass);
975983
} catch (\ReflectionException $e) {
984+
DebugLog::swallowed($e); // @codeCoverageIgnore
976985
// Silently continue if reflection fails
977986
}
978987
}

‎src/Internal/Checker/MemberAssignChecker.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ReflectionNamedType;
1616
use ReflectionProperty;
1717
use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator;
18+
use TypePHP\Internal\Diagnostic\DebugLog;
1819
use TypePHP\Internal\Diagnostic\ErrorFactory;
1920
use TypePHP\Internal\Diagnostic\ErrorMessage;
2021
use TypePHP\Internal\Docblock\DocblockExtractor;
@@ -740,7 +741,8 @@ private static function validatePropertyElementMutationWithContract(
740741
return $registry->validate($value, $contract['valueType'], $context);
741742
}
742743
}
743-
} catch (\Throwable) {
744+
} catch (\Throwable $e) {
745+
DebugLog::swallowed($e); // @codeCoverageIgnore
744746
}
745747

746748
return null;
@@ -1193,7 +1195,8 @@ private static function resolveArrayAccessPlan(string $className): ?array
11931195
}
11941196
}
11951197
}
1196-
} catch (\Throwable) {
1198+
} catch (\Throwable $e) {
1199+
DebugLog::swallowed($e); // @codeCoverageIgnore
11971200
}
11981201

11991202
return self::$arrayAccessPlanCache[$className] = null;
@@ -1281,7 +1284,8 @@ private static function readPropertyValue(mixed $objectOrClass, string $classNam
12811284
// @phpstan-ignore property.dynamicName
12821285
return $objectOrClass->$propName ?? [];
12831286
}
1284-
} catch (\Throwable) {
1287+
} catch (\Throwable $e) {
1288+
DebugLog::swallowed($e); // @codeCoverageIgnore
12851289
}
12861290

12871291
return [];

0 commit comments

Comments
 (0)