Skip to content

Commit 3d35b6a

Browse files
naxvogclaude
andcommitted
Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows
60 catch (\Throwable) 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. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 77920b2 commit 3d35b6a

22 files changed

Lines changed: 218 additions & 17 deletions

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1616
use PHPStan\PhpDocParser\Parser\TokenIterator;
1717
use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator;
18+
use TypePHP\Internal\Diagnostic\DebugLog;
1819
use TypePHP\Internal\Docblock\DocblockExtractor;
1920
use TypePHP\Internal\Docblock\DocblockNormalizer;
2021
use TypePHP\Internal\Docblock\DocblockParser;
@@ -362,7 +363,8 @@ private function collectUnprovenClassConstantChecks(Node\Stmt\Class_ $node, stri
362363
$checkStmt->setAttribute('typephp_injected', true);
363364
$injectedStmts[] = $checkStmt;
364365
}
365-
} catch (\Throwable) {
366+
} catch (\Throwable $e) {
367+
DebugLog::swallowed($e);
366368
}
367369
}
368370

@@ -425,7 +427,8 @@ private function collectUnprovenGlobalConstantChecks(Node\Stmt\Const_ $node): ar
425427
$checkStmt->setAttribute('typephp_injected', true);
426428
$injectedStmts[] = $checkStmt;
427429
}
428-
} catch (\Throwable) {
430+
} catch (\Throwable $e) {
431+
DebugLog::swallowed($e);
429432
}
430433

431434
return $injectedStmts;
@@ -721,7 +724,8 @@ private function processClassPropertyDefaults(Node\Stmt\Class_ $node): void
721724
if ($classTemplates !== [] && DocblockParser::typeReferencesTemplate($propTypeNode, $classTemplates)) {
722725
continue;
723726
}
724-
} catch (\Throwable) {
727+
} catch (\Throwable $e) {
728+
DebugLog::swallowed($e);
725729
}
726730
}
727731

@@ -804,7 +808,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
804808
try {
805809
$phpDocNode = DocblockExtractor::parseDocString($nodeDoc->getText());
806810
$classTemplates = DocblockExtractor::extractTemplates($phpDocNode);
807-
} catch (\Throwable) {
811+
} catch (\Throwable $e) {
812+
DebugLog::swallowed($e);
808813
}
809814
}
810815

@@ -814,7 +819,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
814819
try {
815820
$loadedTemplates = DocblockParser::parseClassTemplates($className);
816821
$classTemplates = [...$classTemplates, ...$loadedTemplates];
817-
} catch (\Throwable) {
822+
} catch (\Throwable $e) {
823+
DebugLog::swallowed($e);
818824
}
819825
}
820826
}
@@ -825,7 +831,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array
825831
try {
826832
$parentTemplates = DocblockParser::parseClassTemplates($parentClass);
827833
$classTemplates = [...$classTemplates, ...$parentTemplates];
828-
} catch (\Throwable) {
834+
} catch (\Throwable $e) {
835+
DebugLog::swallowed($e);
829836
}
830837
}
831838

@@ -903,7 +910,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
903910
try {
904911
$phpDocNode = DocblockExtractor::parseDocString($doc->getText());
905912
$templates = DocblockExtractor::extractTemplates($phpDocNode);
906-
} catch (\Throwable) {
913+
} catch (\Throwable $e) {
914+
DebugLog::swallowed($e);
907915
}
908916
}
909917
$hasTemplates = $templates !== [];
@@ -981,7 +989,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
981989
try {
982990
$methodPhpDoc = DocblockExtractor::parseDocString($methodDoc->getText());
983991
$methodParamTags = DocblockExtractor::getParamTags($methodPhpDoc);
984-
} catch (\Throwable) {
992+
} catch (\Throwable $e) {
993+
DebugLog::swallowed($e);
985994
}
986995
}
987996

@@ -1168,6 +1177,7 @@ private function propagateShapeTypesToDestructuring(Node\Expr\List_|Node\Expr\Ar
11681177
}
11691178
}
11701179
} catch (\Throwable $e) {
1180+
DebugLog::swallowed($e);
11711181
}
11721182
}
11731183

@@ -1231,7 +1241,8 @@ private function handleAssign(Node\Expr\Assign $node): void
12311241
if (StaticTypeEvaluator::satisfies($expr, $parsedType, $this->currentFile)) {
12321242
return;
12331243
}
1234-
} catch (\Throwable) {
1244+
} catch (\Throwable $e) {
1245+
DebugLog::swallowed($e);
12351246
}
12361247

12371248
$node->expr = $this->wrapVariableCheck($expr, $typeString, $varName, $node->var->getStartLine());

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use TypePHP\Internal\Ast\Elimination\EliminationSafetyPolicy;
1313
use TypePHP\Internal\Ast\Elimination\Evaluator\GeneratorEvaluator;
1414
use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator;
15+
use TypePHP\Internal\Diagnostic\DebugLog;
1516
use TypePHP\Internal\Docblock\DocblockExtractor;
1617
use TypePHP\Internal\Util\StubManager;
1718

@@ -152,7 +153,8 @@ public static function inject(
152153
$returnTypeNode = $returnTag->type;
153154
}
154155
$hasMethodTemplates = DocblockExtractor::extractTemplates($phpDocNode) !== [];
155-
} catch (\Throwable) {
156+
} catch (\Throwable $e) {
157+
DebugLog::swallowed($e);
156158
}
157159
}
158160

‎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
/**
@@ -133,6 +134,7 @@ public function extractVarDocblock(string $docText, ?Node\Expr $expr = null): vo
133134
}
134135
}
135136
} catch (\Throwable $e) {
137+
DebugLog::swallowed($e);
136138
// Silently ignore malformed docblocks
137139
}
138140
}

‎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);
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
@@ -23,6 +23,7 @@
2323
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
2424
use ReflectionClass;
2525
use Throwable;
26+
use TypePHP\Internal\Diagnostic\DebugLog;
2627
use TypePHP\Internal\Docblock\DocblockParser;
2728
use TypePHP\Internal\Generics\TemplateManager;
2829
use TypePHP\Internal\Resolver\HierarchyResolver;
@@ -378,6 +379,7 @@ private static function resolveRenamedParamValue(string $function, string $param
378379
}
379380
}
380381
} catch (Throwable $e) {
382+
DebugLog::swallowed($e);
381383
// Silently ignore reflection errors
382384
}
383385

‎src/Internal/Checker/InlineChecker.php‎

Lines changed: 5 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;
@@ -221,6 +222,7 @@ public static function checkVariable(
221222
$value = self::wrapCollectionCallables($typeNode, $value, $context, $registry);
222223
}
223224
} catch (\Throwable $e) {
225+
DebugLog::swallowed($e);
224226
// Silently ignore unexpected execution exceptions
225227
}
226228

@@ -302,6 +304,7 @@ public static function validateCollectionElementMutation(
302304
return $registry->validate($value, $typeNode->type, $context);
303305
}
304306
} catch (\Throwable $e) {
307+
DebugLog::swallowed($e);
305308
// Silently fall back to prospective whole-structure validation
306309
}
307310

@@ -475,6 +478,7 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
475478
return $err;
476479
}
477480
} catch (\Throwable $e) {
481+
DebugLog::swallowed($e);
478482
// Silently ignore unexpected execution exceptions
479483
}
480484

@@ -528,6 +532,7 @@ private static function classHasSelfOut(string $className): bool
528532
}
529533
}
530534
} catch (\Throwable $e) {
535+
DebugLog::swallowed($e);
531536
// Silently ignore reflection errors
532537
}
533538

‎src/Internal/Checker/MemberAssignChecker.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
88
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
99
use ReflectionProperty;
10+
use TypePHP\Internal\Diagnostic\DebugLog;
1011
use TypePHP\Internal\Diagnostic\ErrorFactory;
1112
use TypePHP\Internal\Diagnostic\ErrorMessage;
1213
use TypePHP\Internal\Docblock\DocblockParser;
@@ -242,7 +243,8 @@ private static function validatePropertyElementMutation(
242243

243244
return $registry->validate($value, $typeNode->type, $context);
244245
}
245-
} catch (\Throwable) {
246+
} catch (\Throwable $e) {
247+
DebugLog::swallowed($e);
246248
}
247249

248250
return null;
@@ -268,7 +270,8 @@ private static function readPropertyValue(mixed $objectOrClass, string $classNam
268270
// @phpstan-ignore property.dynamicName
269271
return $objectOrClass->$propName ?? [];
270272
}
271-
} catch (\Throwable) {
273+
} catch (\Throwable $e) {
274+
DebugLog::swallowed($e);
272275
}
273276

274277
return [];

‎src/Internal/Checker/ParamChecker.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
1616
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1717
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
18+
use TypePHP\Internal\Diagnostic\DebugLog;
1819
use TypePHP\Internal\Diagnostic\ErrorFactory;
1920
use TypePHP\Internal\Diagnostic\ErrorMessage;
2021
use TypePHP\Internal\Diagnostic\TypeFormatter;
@@ -624,6 +625,7 @@ private static function inferTemplatesFromClosures(
624625
}
625626
}
626627
} catch (\Throwable $e) {
628+
DebugLog::swallowed($e);
627629
// Silently ignore reflection errors
628630
}
629631
}
@@ -1169,6 +1171,7 @@ private static function inferGenericObjectNode(
11691171
$targetTemplateNames = array_keys(DocblockExtractor::extractTemplates($targetPhpDocNode));
11701172
}
11711173
} catch (\Throwable $e) {
1174+
DebugLog::swallowed($e);
11721175
}
11731176
}
11741177
self::$classDeclaredTemplateNamesCache[$targetClassFqcn] = $targetTemplateNames;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Internal\Diagnostic;
6+
7+
use TypePHP\Internal\Util\Config;
8+
9+
/**
10+
* Opt-in visibility for the exceptions TypePHP swallows in order to stay fail-open.
11+
*
12+
* @internal
13+
*/
14+
final class DebugLog
15+
{
16+
/**
17+
* Writes a swallowed exception to the PHP error log when TYPEPHP_DEBUG is set; a no-op otherwise.
18+
*/
19+
public static function swallowed(\Throwable $e): void
20+
{
21+
if (! Config::isDebugEnabled()) {
22+
return;
23+
}
24+
25+
$catchSite = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
26+
27+
error_log(\sprintf(
28+
'[TypePHP Debug] Swallowed %s: %s (thrown at %s:%d, swallowed at %s:%d)',
29+
$e::class,
30+
$e->getMessage(),
31+
$e->getFile(),
32+
$e->getLine(),
33+
$catchSite['file'] ?? 'unknown',
34+
$catchSite['line'] ?? 0
35+
));
36+
}
37+
}

‎src/Internal/Docblock/DocblockExtractor.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPStan\PhpDocParser\Parser\TokenIterator;
2323
use PHPStan\PhpDocParser\Parser\TypeParser;
2424
use PHPStan\PhpDocParser\ParserConfig;
25+
use TypePHP\Internal\Diagnostic\DebugLog;
2526
use TypePHP\Internal\Resolver\SpecialTypeResolver;
2627
use TypePHP\Internal\Util\ClassNameValidator;
2728
use TypePHP\Internal\Util\StubManager;
@@ -171,6 +172,8 @@ public static function extractMagicPropertyContract(string $doc, string $propNam
171172
'writeType' => $writeType,
172173
];
173174
} catch (\Throwable $e) {
175+
DebugLog::swallowed($e);
176+
174177
return null;
175178
}
176179
}
@@ -291,6 +294,7 @@ public static function getSelfOutTag(PhpDocNode $node): ?TypeNode
291294

292295
return $typeParser->parse($tokens);
293296
} catch (\Throwable $e) {
297+
DebugLog::swallowed($e);
294298
// Silently ignore malformed tags
295299
}
296300
}
@@ -516,6 +520,7 @@ public static function extractTypeFromPropertyDoc(string $doc, string $propName)
516520
}
517521
}
518522
} catch (\Throwable $e) {
523+
DebugLog::swallowed($e);
519524
// Silently ignore malformed property docblocks
520525
}
521526

@@ -539,6 +544,7 @@ public static function extractVarTagFromDoc(string $doc): ?array
539544
return [$typeString, $varName];
540545
}
541546
} catch (\Throwable $e) {
547+
DebugLog::swallowed($e);
542548
// Silently ignore malformed docblocks
543549
}
544550

@@ -664,6 +670,8 @@ public static function resolveImportedTypeAlias(string $fqcn, string $importedAl
664670
return null;
665671
}
666672
} catch (\Throwable $e) {
673+
DebugLog::swallowed($e);
674+
667675
return null;
668676
}
669677
}
@@ -697,6 +705,7 @@ public static function extractTypeFromClassPropertyDoc(string $doc, string $prop
697705
}
698706
}
699707
} catch (\Throwable $e) {
708+
DebugLog::swallowed($e);
700709
// Silently ignore malformed class docblocks
701710
}
702711

@@ -716,6 +725,7 @@ public static function extractMagicMethodContract(string $doc, string $methodNam
716725
}
717726
}
718727
} catch (\Throwable $e) {
728+
DebugLog::swallowed($e);
719729
// Silently ignore malformed class docblocks
720730
}
721731

0 commit comments

Comments
 (0)