diff --git a/src/Internal/Ast/ContractVisitor.php b/src/Internal/Ast/ContractVisitor.php index 2a75f216..0d73a257 100644 --- a/src/Internal/Ast/ContractVisitor.php +++ b/src/Internal/Ast/ContractVisitor.php @@ -15,6 +15,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Parser\TokenIterator; use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockExtractor; use TypePHP\Internal\Docblock\DocblockNormalizer; use TypePHP\Internal\Docblock\DocblockParser; @@ -362,7 +363,8 @@ private function collectUnprovenClassConstantChecks(Node\Stmt\Class_ $node, stri $checkStmt->setAttribute('typephp_injected', true); $injectedStmts[] = $checkStmt; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -425,7 +427,8 @@ private function collectUnprovenGlobalConstantChecks(Node\Stmt\Const_ $node): ar $checkStmt->setAttribute('typephp_injected', true); $injectedStmts[] = $checkStmt; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return $injectedStmts; @@ -721,7 +724,8 @@ private function processClassPropertyDefaults(Node\Stmt\Class_ $node): void if ($classTemplates !== [] && DocblockParser::typeReferencesTemplate($propTypeNode, $classTemplates)) { continue; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -804,7 +808,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array try { $phpDocNode = DocblockExtractor::parseDocString($nodeDoc->getText()); $classTemplates = DocblockExtractor::extractTemplates($phpDocNode); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -814,7 +819,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array try { $loadedTemplates = DocblockParser::parseClassTemplates($className); $classTemplates = [...$classTemplates, ...$loadedTemplates]; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } } @@ -825,7 +831,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array try { $parentTemplates = DocblockParser::parseClassTemplates($parentClass); $classTemplates = [...$classTemplates, ...$parentTemplates]; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -903,7 +910,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\ try { $phpDocNode = DocblockExtractor::parseDocString($doc->getText()); $templates = DocblockExtractor::extractTemplates($phpDocNode); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } $hasTemplates = $templates !== []; @@ -981,7 +989,8 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\ try { $methodPhpDoc = DocblockExtractor::parseDocString($methodDoc->getText()); $methodParamTags = DocblockExtractor::getParamTags($methodPhpDoc); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1168,6 +1177,7 @@ private function propagateShapeTypesToDestructuring(Node\Expr\List_|Node\Expr\Ar } } } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1231,7 +1241,8 @@ private function handleAssign(Node\Expr\Assign $node): void if (StaticTypeEvaluator::satisfies($expr, $parsedType, $this->currentFile)) { return; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } $node->expr = $this->wrapVariableCheck($expr, $typeString, $varName, $node->var->getStartLine()); diff --git a/src/Internal/Ast/FunctionContractInjector.php b/src/Internal/Ast/FunctionContractInjector.php index 22f06ce9..a85d60dc 100644 --- a/src/Internal/Ast/FunctionContractInjector.php +++ b/src/Internal/Ast/FunctionContractInjector.php @@ -12,6 +12,7 @@ use TypePHP\Internal\Ast\Elimination\EliminationSafetyPolicy; use TypePHP\Internal\Ast\Elimination\Evaluator\GeneratorEvaluator; use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockExtractor; use TypePHP\Internal\Util\StubManager; @@ -152,7 +153,8 @@ public static function inject( $returnTypeNode = $returnTag->type; } $hasMethodTemplates = DocblockExtractor::extractTemplates($phpDocNode) !== []; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } diff --git a/src/Internal/Ast/ScopeManager.php b/src/Internal/Ast/ScopeManager.php index 170dda7a..6c509db4 100644 --- a/src/Internal/Ast/ScopeManager.php +++ b/src/Internal/Ast/ScopeManager.php @@ -5,6 +5,7 @@ namespace TypePHP\Internal\Ast; use PhpParser\Node; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockExtractor; /** @@ -133,6 +134,7 @@ public function extractVarDocblock(string $docText, ?Node\Expr $expr = null): vo } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed docblocks } } diff --git a/src/Internal/Ast/TypeDeducer.php b/src/Internal/Ast/TypeDeducer.php index ff41ed8d..514025dc 100644 --- a/src/Internal/Ast/TypeDeducer.php +++ b/src/Internal/Ast/TypeDeducer.php @@ -17,6 +17,7 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Parser\TokenIterator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockExtractor; use TypePHP\Internal\Docblock\DocblockNormalizer; use TypePHP\Internal\Docblock\DocblockParser; @@ -313,7 +314,9 @@ private static function parseTypeString(string $typeString): ?TypeNode $tokens = new TokenIterator($lexer->tokenize($normalized)); return self::$parsedTypeCache[$typeString] = $typeParser->parse($tokens); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } } diff --git a/src/Internal/Checker/ConditionalChecker.php b/src/Internal/Checker/ConditionalChecker.php index ebc776e3..e9aba797 100644 --- a/src/Internal/Checker/ConditionalChecker.php +++ b/src/Internal/Checker/ConditionalChecker.php @@ -23,6 +23,7 @@ use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use ReflectionClass; use Throwable; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockParser; use TypePHP\Internal\Generics\TemplateManager; use TypePHP\Internal\Resolver\HierarchyResolver; @@ -378,6 +379,7 @@ private static function resolveRenamedParamValue(string $function, string $param } } } catch (Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } diff --git a/src/Internal/Checker/InlineChecker.php b/src/Internal/Checker/InlineChecker.php index ae67929d..b7da0736 100644 --- a/src/Internal/Checker/InlineChecker.php +++ b/src/Internal/Checker/InlineChecker.php @@ -24,6 +24,7 @@ use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\PhpDocParser\ParserConfig; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockNormalizer; @@ -221,6 +222,7 @@ public static function checkVariable( $value = self::wrapCollectionCallables($typeNode, $value, $context, $registry); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore unexpected execution exceptions } @@ -302,6 +304,7 @@ public static function validateCollectionElementMutation( return $registry->validate($value, $typeNode->type, $context); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently fall back to prospective whole-structure validation } @@ -475,6 +478,7 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string return $err; } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore unexpected execution exceptions } @@ -528,6 +532,7 @@ private static function classHasSelfOut(string $className): bool } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } diff --git a/src/Internal/Checker/MemberAssignChecker.php b/src/Internal/Checker/MemberAssignChecker.php index cd017b3a..4f83b24c 100644 --- a/src/Internal/Checker/MemberAssignChecker.php +++ b/src/Internal/Checker/MemberAssignChecker.php @@ -7,6 +7,7 @@ use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; use ReflectionProperty; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockParser; @@ -242,7 +243,8 @@ private static function validatePropertyElementMutation( return $registry->validate($value, $typeNode->type, $context); } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return null; @@ -268,7 +270,8 @@ private static function readPropertyValue(mixed $objectOrClass, string $classNam // @phpstan-ignore property.dynamicName return $objectOrClass->$propName ?? []; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return []; diff --git a/src/Internal/Checker/ParamChecker.php b/src/Internal/Checker/ParamChecker.php index 50b1cb7d..850fd3dc 100644 --- a/src/Internal/Checker/ParamChecker.php +++ b/src/Internal/Checker/ParamChecker.php @@ -15,6 +15,7 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Diagnostic\TypeFormatter; @@ -624,6 +625,7 @@ private static function inferTemplatesFromClosures( } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } } @@ -1169,6 +1171,7 @@ private static function inferGenericObjectNode( $targetTemplateNames = array_keys(DocblockExtractor::extractTemplates($targetPhpDocNode)); } } catch (\Throwable $e) { + DebugLog::swallowed($e); } } self::$classDeclaredTemplateNamesCache[$targetClassFqcn] = $targetTemplateNames; diff --git a/src/Internal/Diagnostic/DebugLog.php b/src/Internal/Diagnostic/DebugLog.php new file mode 100644 index 00000000..b12e094c --- /dev/null +++ b/src/Internal/Diagnostic/DebugLog.php @@ -0,0 +1,37 @@ +getMessage(), + $e->getFile(), + $e->getLine(), + $catchSite['file'] ?? 'unknown', + $catchSite['line'] ?? 0 + )); + } +} diff --git a/src/Internal/Docblock/DocblockExtractor.php b/src/Internal/Docblock/DocblockExtractor.php index 9c42229c..2464846b 100644 --- a/src/Internal/Docblock/DocblockExtractor.php +++ b/src/Internal/Docblock/DocblockExtractor.php @@ -22,6 +22,7 @@ use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\PhpDocParser\ParserConfig; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Resolver\SpecialTypeResolver; use TypePHP\Internal\Util\ClassNameValidator; use TypePHP\Internal\Util\StubManager; @@ -171,6 +172,8 @@ public static function extractMagicPropertyContract(string $doc, string $propNam 'writeType' => $writeType, ]; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -291,6 +294,7 @@ public static function getSelfOutTag(PhpDocNode $node): ?TypeNode return $typeParser->parse($tokens); } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed tags } } @@ -516,6 +520,7 @@ public static function extractTypeFromPropertyDoc(string $doc, string $propName) } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed property docblocks } @@ -539,6 +544,7 @@ public static function extractVarTagFromDoc(string $doc): ?array return [$typeString, $varName]; } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed docblocks } @@ -664,6 +670,8 @@ public static function resolveImportedTypeAlias(string $fqcn, string $importedAl return null; } } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -697,6 +705,7 @@ public static function extractTypeFromClassPropertyDoc(string $doc, string $prop } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed class docblocks } @@ -716,6 +725,7 @@ public static function extractMagicMethodContract(string $doc, string $methodNam } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed class docblocks } diff --git a/src/Internal/Docblock/DocblockParser.php b/src/Internal/Docblock/DocblockParser.php index e42800b9..07661c70 100644 --- a/src/Internal/Docblock/DocblockParser.php +++ b/src/Internal/Docblock/DocblockParser.php @@ -24,6 +24,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use TypePHP\Internal\Checker\InlineChecker; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Resolver\HierarchyResolver; use TypePHP\Internal\Resolver\SpecialTypeResolver; use TypePHP\Internal\Util\Config; @@ -473,6 +474,8 @@ public static function parseClassTemplates(string $className): array return $classTemplates; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return []; } } @@ -557,6 +560,8 @@ public static function parseProperty(string $className, string $propertyName): ? return self::$propertyCache[$className][$propertyName] = $resolvedNode; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return self::$propertyCache[$className][$propertyName] = null; } } @@ -619,6 +624,8 @@ public static function parseMagicPropertyContract(string $className, string $pro return self::$magicPropertyContractCache[$className][$propertyName] = null; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return self::$magicPropertyContractCache[$className][$propertyName] = null; } } @@ -791,6 +798,8 @@ public static function parseMagicMethod(string $className, string $methodName): 'templates' => $classTemplates, ]; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return self::$magicMethodCache[$className][$methodName] = null; } } @@ -931,6 +940,8 @@ public static function parseClassAliases(string $className): array return $aliases; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return []; } } diff --git a/src/Internal/Generics/TemplateManager.php b/src/Internal/Generics/TemplateManager.php index 80507ec4..a07c0862 100644 --- a/src/Internal/Generics/TemplateManager.php +++ b/src/Internal/Generics/TemplateManager.php @@ -25,6 +25,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\PhpDocParser\Parser\TokenIterator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockExtractor; @@ -509,6 +510,7 @@ public static function getDeclaredClassTemplateNames(string $className): array return self::$declaredClassTemplateNamesCache[$className] = array_keys($templates); } } catch (\Throwable $e) { + DebugLog::swallowed($e); } return self::$declaredClassTemplateNamesCache[$className] = []; @@ -648,6 +650,7 @@ public static function getClassStaticBindings(string $className): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -738,6 +741,7 @@ public static function getBoundTemplatesForInstance(object $instance): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -764,6 +768,7 @@ public static function getTemplateVariances(object $instance): array return DocblockExtractor::extractTemplateVariances($classPhpDocNode); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -970,6 +975,7 @@ public static function bindInstanceFromNode(object $instance, GenericTypeNode $t } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection or parsing errors } @@ -1279,6 +1285,7 @@ private static function computeClassInheritedBindings(string $actualClassName): } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection or parsing errors } @@ -1385,6 +1392,7 @@ private static function collectInheritedGenericTagBindings( } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } } @@ -1562,6 +1570,7 @@ private static function checkIdentifierAgainstGenericVariance( [, $classVariances] = self::collectHierarchyTemplatesAndVariances($ref); $declaredVariances = array_values($classVariances); } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1742,6 +1751,7 @@ private static function checkNestedGenericVariance(GenericTypeNode $existing, Ge [, $classVariances] = self::collectHierarchyTemplatesAndVariances($ref); $declaredVariances = array_values($classVariances); } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1815,6 +1825,7 @@ public static function bindInstance(object $instance, string $typeString, string self::bindInstanceFromNode($instance, $typeNode, '', true); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed docblock strings } diff --git a/src/Internal/Io/CacheManager.php b/src/Internal/Io/CacheManager.php index 7cc7dc9d..777f6529 100644 --- a/src/Internal/Io/CacheManager.php +++ b/src/Internal/Io/CacheManager.php @@ -4,6 +4,7 @@ namespace TypePHP\Internal\Io; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Util\Config; use TypePHP\Internal\Util\FileFilter; @@ -310,6 +311,7 @@ private static function findFilesToWarm(string $baseDir): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently handle filesystem read errors } diff --git a/src/Internal/Io/StreamWrapper.php b/src/Internal/Io/StreamWrapper.php index 54a24f38..72c9d6e5 100644 --- a/src/Internal/Io/StreamWrapper.php +++ b/src/Internal/Io/StreamWrapper.php @@ -12,6 +12,7 @@ use PhpParser\ParserFactory; use TypePHP\Internal\Ast\ContractVisitor; use TypePHP\Internal\Ast\TypePHPPrinter; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Resolver\SpecialTypeResolver; use TypePHP\Internal\Util\Config; use TypePHP\Internal\Util\IgnoreManager; @@ -273,6 +274,8 @@ public static function transformSource(string $source, string $filePath = ''): s return $source; } } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $source; } @@ -365,6 +368,8 @@ private static function neutralizeTrailingLineComments(string $code): string return $result; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $code; } } diff --git a/src/Internal/Reporting/ViolationCollector.php b/src/Internal/Reporting/ViolationCollector.php index ca4d77ff..6f480366 100644 --- a/src/Internal/Reporting/ViolationCollector.php +++ b/src/Internal/Reporting/ViolationCollector.php @@ -7,6 +7,7 @@ use ReflectionClass; use ReflectionFunction; use Throwable; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockParser; use TypePHP\Internal\Io\StreamWrapper; @@ -462,7 +463,9 @@ private static function resolveDeclaredIn( } return self::resolveFunctionDeclaration($function); - } catch (Throwable) { + } catch (Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -492,7 +495,9 @@ private static function resolveGenericBoundDeclaration(string $message): ?string $startLine = $refClass->getStartLine(); return self::normalizeRelativePath($file) . ':' . ($startLine !== false ? $startLine : 1); - } catch (Throwable) { + } catch (Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -564,7 +569,9 @@ private static function findPropertyDeclarationLine(string $file, string $propNa return self::$propertyLineCache[$file][$propName] = $token->line; } } - } catch (Throwable) { + } catch (Throwable $e) { + DebugLog::swallowed($e); + return self::$propertyLineCache[$file][$propName] = null; } diff --git a/src/Internal/Resolver/CallerBoundaryResolver.php b/src/Internal/Resolver/CallerBoundaryResolver.php index a5caa999..b44a28bf 100644 --- a/src/Internal/Resolver/CallerBoundaryResolver.php +++ b/src/Internal/Resolver/CallerBoundaryResolver.php @@ -8,6 +8,7 @@ use ReflectionClass; use ReflectionFunction; use Throwable; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Util\Config; use TypePHP\Internal\Util\PathMatcher; @@ -68,6 +69,7 @@ public static function shouldBypassCallback(mixed $callable, string $prefix = '' return PathMatcher::isVendorPath($normalized); } } catch (Throwable $e) { + DebugLog::swallowed($e); } } @@ -98,6 +100,7 @@ public static function isCalleeVendor(string $function): bool $fileName = $ref->getFileName(); } } catch (Throwable $e) { + DebugLog::swallowed($e); } if ($fileName === null || $fileName === false) { diff --git a/src/Internal/Resolver/HierarchyResolver.php b/src/Internal/Resolver/HierarchyResolver.php index 8f16766d..eba1f6d8 100644 --- a/src/Internal/Resolver/HierarchyResolver.php +++ b/src/Internal/Resolver/HierarchyResolver.php @@ -6,6 +6,7 @@ use ReflectionClass; use ReflectionMethod; +use TypePHP\Internal\Diagnostic\DebugLog; /** * @internal Resolves class, interface, trait, and method inheritance hierarchies from child to root. @@ -118,6 +119,7 @@ public static function getTraitAliases(string $className): array return self::$traitAliasCache[$className] = $aliases; } catch (\Throwable $e) { + DebugLog::swallowed($e); self::$hasTraitAliasesCache[$className] = false; return self::$traitAliasCache[$className] = []; diff --git a/src/Internal/Resolver/SpecialTypeResolver.php b/src/Internal/Resolver/SpecialTypeResolver.php index 80172316..2c6d6c43 100644 --- a/src/Internal/Resolver/SpecialTypeResolver.php +++ b/src/Internal/Resolver/SpecialTypeResolver.php @@ -26,6 +26,7 @@ use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Diagnostic\TypeFormatter; @@ -1099,6 +1100,7 @@ public static function getClassTraitUseDocs(string $className): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -1563,6 +1565,7 @@ public static function parseFileMetadata(string $fileName, string $source): void self::$fileUseImports[$normalizedFile] = $imports; } catch (\Throwable $e) { + DebugLog::swallowed($e); self::$fileUseImports[$normalizedFile] = []; } } diff --git a/src/Internal/RuntimeTypeChecker.php b/src/Internal/RuntimeTypeChecker.php index 30c22550..e6e6a4b0 100644 --- a/src/Internal/RuntimeTypeChecker.php +++ b/src/Internal/RuntimeTypeChecker.php @@ -15,6 +15,7 @@ use TypePHP\Internal\Checker\ParamOutChecker; use TypePHP\Internal\Checker\ReturnChecker; use TypePHP\Internal\Checker\SelfOutChecker; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockExtractor; @@ -111,6 +112,7 @@ public static function checkConstant( return ViolationCollector::handle($err, 'constant', $value, $file, $line, $constantName, $target, $declaredIn); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore unexpected parsing errors } @@ -155,6 +157,7 @@ public static function checkConstantClosureParam( return ViolationCollector::handle($err, 'parameter', $value, null, null, $caller, '$' . $paramName, $declaredIn); } } catch (\Throwable $e) { + DebugLog::swallowed($e); } return $value; @@ -197,6 +200,7 @@ public static function checkConstantClosureReturn( return ViolationCollector::handle($err, 'return', $value, $file, $line, $caller, 'return', $declaredIn); } } catch (\Throwable $e) { + DebugLog::swallowed($e); } return $value; diff --git a/src/Internal/Util/Config.php b/src/Internal/Util/Config.php index a80dd9f1..45f07716 100644 --- a/src/Internal/Util/Config.php +++ b/src/Internal/Util/Config.php @@ -8,6 +8,7 @@ use TypePHP\Internal\Checker\MemberAssignChecker; use TypePHP\Internal\Checker\ParamChecker; use TypePHP\Internal\Checker\ReturnChecker; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockParser; use TypePHP\Internal\Generics\TemplateManager; use TypePHP\Internal\Io\CacheManager; @@ -259,6 +260,16 @@ public static function isRedactValuesEnabled(): bool return self::$redactValues; } + /** + * Whether swallowed internal exceptions are written to the PHP error log (TYPEPHP_DEBUG env only). + */ + public static function isDebugEnabled(): bool + { + $envDebug = self::getEnvValue('TYPEPHP_DEBUG'); + + return $envDebug !== null && filter_var($envDebug, FILTER_VALIDATE_BOOLEAN); + } + public static function isReportMode(): bool { return self::getOnViolation() === 'report'; @@ -521,7 +532,8 @@ public static function getProjectRoot(?string $startingDir = null): string return self::$projectRoot = $normPath; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } diff --git a/src/Internal/Util/StubManager.php b/src/Internal/Util/StubManager.php index 2b0fab07..d3acc668 100644 --- a/src/Internal/Util/StubManager.php +++ b/src/Internal/Util/StubManager.php @@ -10,6 +10,7 @@ use RecursiveIteratorIterator; use SplFileInfo; use Throwable; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Io\StreamWrapper; /** @@ -154,6 +155,7 @@ private static function loadStubFiles(array $globs): void self::extractStubsFromAst($stmts); } } catch (Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed stub files } } diff --git a/tests/Internal/Diagnostic/DebugLogTest.php b/tests/Internal/Diagnostic/DebugLogTest.php new file mode 100644 index 00000000..d1d18509 --- /dev/null +++ b/tests/Internal/Diagnostic/DebugLogTest.php @@ -0,0 +1,61 @@ + DebugLog::swallowed(new RuntimeException('boom'))))->toBe(''); + }); + + test('logs the swallowed exception with throw and catch sites', function () use ($captureErrorLog) { + putenv('TYPEPHP_DEBUG=1'); + + expect($captureErrorLog(static fn () => DebugLog::swallowed(new RuntimeException('boom')))) + ->toContain('[TypePHP Debug] Swallowed RuntimeException: boom') + ->toContain('thrown at ' . __FILE__) + ->toContain('swallowed at ' . __FILE__) + ; + }); + + test('reports a source file that is left uninstrumented because it does not parse', function () use ($captureErrorLog) { + putenv('TYPEPHP_DEBUG=1'); + $source = 'toBe($source) + ->and($log)->toContain('Swallowed PhpParser\\Error') + ->toContain('StreamWrapper.php') + ; + }); +});