diff --git a/src/Internal/Ast/ContractVisitor.php b/src/Internal/Ast/ContractVisitor.php index 15bfac9..bd28a85 100644 --- a/src/Internal/Ast/ContractVisitor.php +++ b/src/Internal/Ast/ContractVisitor.php @@ -25,6 +25,7 @@ use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; 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; @@ -540,7 +541,8 @@ private function collectUnprovenClassConstantChecks(Node\Stmt\Class_ $node, stri $checkStmt->setAttribute('typephp_injected', true); $injectedStmts[] = $checkStmt; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -603,7 +605,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; @@ -901,7 +904,8 @@ private function processClassPropertyDefaults(Node\Stmt\Class_ $node): void if ($classTemplates !== [] && DocblockParser::typeReferencesTemplate($propTypeNode, $classTemplates)) { continue; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -984,7 +988,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); } } @@ -994,7 +999,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); } } } @@ -1006,7 +1012,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); } } else { $parentFile = self::findClassFileWithoutAutoload($parentClass); @@ -1022,7 +1029,8 @@ private function resolveClassTemplatesForDefaults(Node\Stmt\Class_ $node): array $classTemplates = [...$classTemplates, ...DocblockExtractor::extractTemplates($phpDocNode)]; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } } @@ -1104,7 +1112,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 !== []; @@ -1194,7 +1203,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); } } @@ -1261,7 +1271,8 @@ private function extractClassLikeAliases( try { $phpDocNode = DocblockExtractor::parseDocString($doc->getText()); $this->collectAliasesFromPhpDocInFile($phpDocNode, $aliases, $this->currentFile, $this->currentNamespace, $visited); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1412,7 +1423,9 @@ class_exists($fqcn, false) self::collectRawAliasesFromAstDoc($phpDocNode, $aliases, '', '', [], $localClassAliases, $visited); return self::$astClassAliasesCache[$fqcn] = $aliases; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return self::$astClassAliasesCache[$fqcn] = []; } } @@ -1443,7 +1456,8 @@ class_exists($fqcn, false) } $aliases = self::extractTargetClassAliasesFromStmts($stmts, $fqcn, $filePath, $localClassAliases, $visited); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); $aliases = []; } @@ -1545,7 +1559,8 @@ private static function scanClassLikesForAliases( try { $phpDocNode = DocblockExtractor::parseDocString($doc->getText()); self::collectRawAliasesFromAstDoc($phpDocNode, $aliases, $filePath, $namespace, $imports, $localClassAliases, $visited); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1893,6 +1908,7 @@ private function propagateShapeTypesToDestructuring(Node\Expr\List_|Node\Expr\Ar } } } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1940,7 +1956,8 @@ private function handleAssign(Node\Expr\Assign $node): ?Node [$typeParser, $lexer] = DocblockExtractor::getTypeParserComponents(); $tokens = new TokenIterator($lexer->tokenize(DocblockNormalizer::normalize($typeString))); $parsedType = $typeParser->parse($tokens); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } if ($expr instanceof Node\Expr\New_ && $parsedType !== null) { @@ -2758,7 +2775,8 @@ private function registerParamInferredTypes(array $params, ?string $docText = nu foreach (DocblockExtractor::getParamTags($phpDocNode) as $pName => $pTag) { $docParamTypes[$pName] = (string) $pTag->type; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -3286,7 +3304,8 @@ private function parseUnsetTypeCached(string $typeString): ?TypeNode self::$parsedUnsetTypeCache[$typeString] = $parsed; return $parsed; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); self::$parsedUnsetTypeCache[$typeString] = false; return null; diff --git a/src/Internal/Ast/FunctionContractInjector.php b/src/Internal/Ast/FunctionContractInjector.php index b22ab90..4d93bc6 100644 --- a/src/Internal/Ast/FunctionContractInjector.php +++ b/src/Internal/Ast/FunctionContractInjector.php @@ -27,6 +27,7 @@ use TypePHP\Internal\Ast\Elimination\Evaluator\GeneratorEvaluator; use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator; use TypePHP\Internal\Checker\ConditionalChecker; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockExtractor; use TypePHP\Internal\Docblock\DocblockNormalizer; use TypePHP\Internal\Docblock\DocblockParser; @@ -119,7 +120,8 @@ public static function inject( $paramTags = DocblockExtractor::getParamTags($phpDocNode); $paramOutTags = DocblockExtractor::getParamOutTags($phpDocNode); $selfOutType = DocblockExtractor::getSelfOutTag($phpDocNode); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -479,7 +481,8 @@ private static function collectTrustedParamTypes( try { $phpDocNode = DocblockExtractor::parseDocString($doc->getText()); $paramTags = DocblockExtractor::getParamTags($phpDocNode); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1767,7 +1770,9 @@ public static function inlineVarSubsumesReturn( $targetReturnType, GenericTypeNode::VARIANCE_COVARIANT ); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return false; } } diff --git a/src/Internal/Ast/ScopeManager.php b/src/Internal/Ast/ScopeManager.php index c0b11b1..e798bfc 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; /** @@ -211,6 +212,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 ff41ed8..514025d 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 de728c4..8333162 100644 --- a/src/Internal/Checker/ConditionalChecker.php +++ b/src/Internal/Checker/ConditionalChecker.php @@ -24,6 +24,7 @@ use ReflectionClass; use Throwable; use Traversable; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Docblock\DocblockParser; use TypePHP\Internal\Generics\TemplateManager; use TypePHP\Internal\Resolver\HierarchyResolver; @@ -479,6 +480,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 2b2f7aa..0168eec 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; @@ -266,6 +267,7 @@ public static function checkVariable( $value = self::wrapCollectionCallables($typeNode, $value, $context, $registry); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore unexpected execution exceptions } @@ -373,6 +375,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 } @@ -572,6 +575,7 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string TemplateManager::markFallbackReturnBindings($value, $typeNode, $fallbackMeta[1], $fallbackMeta[0], $preExistingBindings); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore unexpected execution exceptions } @@ -625,6 +629,7 @@ private static function classHasSelfOut(string $className): bool } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -780,6 +785,7 @@ private static function resolveFunctionContextWithFallback(TypeNode $typeNode, s $typeNode = SpecialTypeResolver::resolve($typeNode, $refFunc); } } catch (\ReflectionException $e) { + DebugLog::swallowed($e); // Silently continue if reflection fails } @@ -859,6 +865,7 @@ private static function resolveClassContextWithFallback( $typeNode = SpecialTypeResolver::resolve($typeNode, $refClass); } } catch (\ReflectionException $e) { + DebugLog::swallowed($e); // Silently continue if reflection fails } @@ -916,6 +923,7 @@ private static function substitutePropertyGenericsWithFallback(TypeNode $typeNod $refClass = new \ReflectionClass($className); $typeNode = SpecialTypeResolver::resolve($subNode, $refClass); } catch (\ReflectionException $e) { + DebugLog::swallowed($e); $typeNode = $subNode; } } else { @@ -973,6 +981,7 @@ private static function substituteStaticPropertyGenerics(TypeNode $typeNode, str $refClass = new \ReflectionClass($className); $typeNode = SpecialTypeResolver::resolve($typeNode, $refClass); } catch (\ReflectionException $e) { + DebugLog::swallowed($e); // Silently continue if reflection fails } } diff --git a/src/Internal/Checker/MemberAssignChecker.php b/src/Internal/Checker/MemberAssignChecker.php index 7b25b3a..8f34ddc 100644 --- a/src/Internal/Checker/MemberAssignChecker.php +++ b/src/Internal/Checker/MemberAssignChecker.php @@ -15,6 +15,7 @@ use ReflectionNamedType; use ReflectionProperty; use TypePHP\Internal\Ast\Elimination\StaticTypeEvaluator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Docblock\DocblockExtractor; @@ -740,7 +741,8 @@ private static function validatePropertyElementMutationWithContract( return $registry->validate($value, $contract['valueType'], $context); } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return null; @@ -1193,7 +1195,8 @@ private static function resolveArrayAccessPlan(string $className): ?array } } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return self::$arrayAccessPlanCache[$className] = null; @@ -1281,7 +1284,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 d18490d..141e851 100644 --- a/src/Internal/Checker/ParamChecker.php +++ b/src/Internal/Checker/ParamChecker.php @@ -16,6 +16,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use TypePHP\Internal\Ast\Elimination\Evaluator\GeneratorEvaluator; +use TypePHP\Internal\Diagnostic\DebugLog; use TypePHP\Internal\Diagnostic\ErrorFactory; use TypePHP\Internal\Diagnostic\ErrorMessage; use TypePHP\Internal\Diagnostic\TypeFormatter; @@ -1015,6 +1016,7 @@ private static function inferTemplatesFromClosures( $classTemplates ); } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } } @@ -2186,6 +2188,7 @@ private static function inferGenericObjectNode( $targetTemplateNames = array_keys(DocblockExtractor::extractTemplates($targetPhpDocNode)); } } catch (\Throwable $e) { + DebugLog::swallowed($e); } } self::$classDeclaredTemplateNamesCache[$targetClassFqcn] = $targetTemplateNames; @@ -2348,7 +2351,8 @@ private static function inferTemplatesFromIterableObject( } } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } elseif ($value instanceof \Traversable) { [$inferredKey, $inferredVal] = self::resolveTraversableItemTypes($value); @@ -2453,7 +2457,8 @@ private static function traversablePlan(object $value): ?array ]; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -2500,7 +2505,8 @@ private static function traversablePlan(object $value): ?array break 2; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } diff --git a/src/Internal/Diagnostic/DebugLog.php b/src/Internal/Diagnostic/DebugLog.php new file mode 100644 index 0000000..8294d4c --- /dev/null +++ b/src/Internal/Diagnostic/DebugLog.php @@ -0,0 +1,32 @@ +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 c564a6a..b09c1bd 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 } @@ -681,6 +687,8 @@ public static function resolveImportedTypeAlias(string $fqcn, string $importedAl return null; } } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -714,6 +722,7 @@ public static function extractTypeFromClassPropertyDoc(string $doc, string $prop } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed class docblocks } @@ -733,6 +742,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 95f250d..e7fa532 100644 --- a/src/Internal/Docblock/DocblockParser.php +++ b/src/Internal/Docblock/DocblockParser.php @@ -25,6 +25,7 @@ use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use TypePHP\Internal\Checker\ConditionalChecker; 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; @@ -495,6 +496,7 @@ public static function parse(string $function): array $contract = self::parseFunction($ref); } } catch (\ReflectionException $e) { + DebugLog::swallowed($e); $contract = [ 'types' => [], 'constrainedTypes' => [], @@ -550,6 +552,8 @@ public static function parseClassTemplates(string $className): array return $classTemplates; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return []; } } @@ -634,6 +638,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; } } @@ -696,6 +702,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; } } @@ -868,6 +876,8 @@ public static function parseMagicMethod(string $className, string $methodName): 'templates' => $classTemplates, ]; } catch (\Throwable $e) { + DebugLog::swallowed($e); + return self::$magicMethodCache[$className][$methodName] = null; } } @@ -1008,6 +1018,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 65cd1da..c2e1b91 100644 --- a/src/Internal/Generics/TemplateManager.php +++ b/src/Internal/Generics/TemplateManager.php @@ -30,6 +30,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; @@ -868,7 +869,8 @@ public static function markFallbackReturnBindings( } } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -1037,6 +1039,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] = []; @@ -1422,6 +1425,7 @@ public static function getTemplateVariances(object $instance): array return self::$templateVariancesCache[$className] = DocblockExtractor::extractTemplateVariances($classPhpDocNode); } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -1777,6 +1781,7 @@ public static function bindInstanceFromNode( } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection or parsing errors } @@ -1924,7 +1929,9 @@ private static function compileGenericBindingPlan(GenericTypeNode $typeNode, str 'allStaticValid' => $allStaticValid, 'items' => $items, ]; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return [ 'isRelative' => false, 'skip' => false, @@ -2020,7 +2027,8 @@ private static function propagationPlan(string $className): array $plan[] = ['prop' => $prop, 'type' => $propType]; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -2177,7 +2185,8 @@ private static function propagateBindingsToInstanceProperties( 'bindings' => $instanceBindings, 'propIds' => $propIds, ]; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } finally { unset(self::$propagationStack[$oid]); } @@ -2269,6 +2278,7 @@ private static function declaredVarianceList(string $className): array [, $classVariances] = self::hierarchyTemplatesForClass($className); $variances = array_values($classVariances); } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -2297,6 +2307,7 @@ private static function classTemplateDefaults(string $className): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -2660,7 +2671,8 @@ private static function propagateGeneratorRefinementToProperties(object $instanc $map[$propVal] = $propBindings; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } } @@ -2902,6 +2914,7 @@ private static function computeClassInheritedBindings(string $actualClassName, a } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection or parsing errors } @@ -3057,6 +3070,7 @@ private static function collectInheritedGenericTagBindings( } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } } @@ -3379,7 +3393,9 @@ private static function resolveRawConstantValue(string $fqcn, string $constName) } return \defined($constName) ? \constant($constName) : null; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -4139,7 +4155,8 @@ private static function verifyShallowCollectionRefinement( $plan[] = ['prop' => $prop, 'type' => $propType]; } } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } self::$templatePropertyPlanCache[$actualClass][$templateName] = $plan; @@ -4243,6 +4260,7 @@ public static function bindInstance(object $instance, string $typeString, string } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore malformed docblock strings } @@ -4317,7 +4335,8 @@ private static function extractSingleGenericTypeString(string $typeString): ?arr return [(string) $node, (string) $node->type]; } - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); } return null; diff --git a/src/Internal/Io/CacheManager.php b/src/Internal/Io/CacheManager.php index 023e754..d82cdbd 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; use TypePHP\Internal\Util\StubManager; @@ -360,6 +361,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 2137a93..e6cca6a 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; @@ -281,6 +282,8 @@ public static function transformSource(string $source, string $filePath = ''): s return $source; } } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $source; } @@ -373,6 +376,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 943994f..3336767 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; @@ -620,7 +621,9 @@ private static function resolveDeclaredIn( } return self::resolveFunctionDeclaration($function); - } catch (Throwable) { + } catch (Throwable $e) { + DebugLog::swallowed($e); + return null; } } @@ -650,7 +653,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; } } @@ -722,7 +727,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 a721cdb..824a61b 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; @@ -97,6 +98,7 @@ public static function shouldBypassCallback(mixed $callable, string $prefix = '' return PathMatcher::isVendorPath($normalized); } } catch (Throwable $e) { + DebugLog::swallowed($e); } } @@ -136,6 +138,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 5ce2cf9..4865c60 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 561a5b6..6479d78 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; @@ -551,6 +552,8 @@ private static function getReflectionContext(\ReflectionClass|\ReflectionFunctio try { return self::$reflectionContextCache[$context] = new \ReflectionMethod($className, $methodName); } catch (\ReflectionException $e) { + DebugLog::swallowed($e); + return self::$reflectionContextCache[$context] = new \ReflectionClass($className); } } @@ -569,6 +572,7 @@ private static function getReflectionContext(\ReflectionClass|\ReflectionFunctio try { return self::$reflectionContextCache[$context] = new \ReflectionFunction($context); } catch (\ReflectionException $e) { + DebugLog::swallowed($e); /** @var class-string $fallbackClass */ $fallbackClass = \stdClass::class; @@ -1109,6 +1113,7 @@ private static function getClassConstant(string $fqcn, string $constName, bool $ $val = $refClass->getConstant($constName); } } catch (\ReflectionException $e) { + DebugLog::swallowed($e); } } self::$classConstantCache[$fqcn][$constName] = $val; @@ -1262,6 +1267,7 @@ public static function getClassTraitUseDocs(string $className): array } } } catch (\Throwable $e) { + DebugLog::swallowed($e); // Silently ignore reflection errors } @@ -1837,6 +1843,7 @@ public static function parseFileMetadata(string $fileName, string $source): void } self::$fileUseImports[$normalizedFile] = $allImports; } catch (\Throwable $e) { + DebugLog::swallowed($e); self::$fileUseImports[$normalizedFile] = []; self::$fileNamespaceUseImports[$normalizedFile] = []; unset(self::$fileNamespaceRanges[$normalizedFile]); diff --git a/src/Internal/RuntimeTypeChecker.php b/src/Internal/RuntimeTypeChecker.php index 66f4f89..094f261 100644 --- a/src/Internal/RuntimeTypeChecker.php +++ b/src/Internal/RuntimeTypeChecker.php @@ -25,6 +25,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; @@ -328,7 +329,9 @@ public static function checkClassConstant( try { $refConst = new \ReflectionClassConstant($className, $constName); $value = $refConst->getValue(); - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + return null; } @@ -352,6 +355,8 @@ public static function checkConstant( try { $typeNode = self::resolveConstantTypeNode($typeString, $constantName, $file); } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $value; } @@ -392,6 +397,8 @@ public static function checkConstantClosureParam( try { $typeNode = self::resolveConstantTypeNode($typeString, $caller, $file); } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $value; } @@ -428,6 +435,8 @@ public static function checkConstantClosureReturn( try { $typeNode = self::resolveConstantTypeNode($typeString, $caller, $file); } catch (\Throwable $e) { + DebugLog::swallowed($e); + return $value; } diff --git a/src/Internal/Util/Config.php b/src/Internal/Util/Config.php index c0ae298..c59e977 100644 --- a/src/Internal/Util/Config.php +++ b/src/Internal/Util/Config.php @@ -9,6 +9,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; @@ -288,6 +289,14 @@ 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 + { + return filter_var(self::getEnvValue('TYPEPHP_DEBUG'), FILTER_VALIDATE_BOOLEAN); + } + public static function isReportMode(): bool { return self::getOnViolation() === 'report'; @@ -550,7 +559,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/StructureMutator.php b/src/Internal/Util/StructureMutator.php index 1e75917..e76fd2e 100644 --- a/src/Internal/Util/StructureMutator.php +++ b/src/Internal/Util/StructureMutator.php @@ -5,6 +5,7 @@ namespace TypePHP\Internal\Util; use stdClass; +use TypePHP\Internal\Diagnostic\DebugLog; /** * @internal Navigates and mutates a data structure along an accessor chain on an isolated copy. @@ -162,7 +163,9 @@ public static function unsetCopy(mixed $root, array $chains): mixed // @phpstan-ignore property.dynamicName $current = &$current->$propName; - } catch (\Throwable) { + } catch (\Throwable $e) { + DebugLog::swallowed($e); + break; } } diff --git a/src/Internal/Util/StubManager.php b/src/Internal/Util/StubManager.php index 33d623b..29ce12e 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; /** @@ -180,6 +181,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 0000000..cfd9a38 --- /dev/null +++ b/tests/Internal/Diagnostic/DebugLogTest.php @@ -0,0 +1,64 @@ + 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') + ; + }); +});