Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/Internal/Ast/ContractVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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 !== [];
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -1168,6 +1177,7 @@ private function propagateShapeTypesToDestructuring(Node\Expr\List_|Node\Expr\Ar
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
}
}

Expand Down Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Ast/FunctionContractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -152,7 +153,8 @@ public static function inject(
$returnTypeNode = $returnTag->type;
}
$hasMethodTemplates = DocblockExtractor::extractTemplates($phpDocNode) !== [];
} catch (\Throwable) {
} catch (\Throwable $e) {
DebugLog::swallowed($e);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Ast/ScopeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TypePHP\Internal\Ast;

use PhpParser\Node;
use TypePHP\Internal\Diagnostic\DebugLog;
use TypePHP\Internal\Docblock\DocblockExtractor;

/**
Expand Down Expand Up @@ -133,6 +134,7 @@ public function extractVarDocblock(string $docText, ?Node\Expr $expr = null): vo
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore malformed docblocks
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Internal/Ast/TypeDeducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Checker/ConditionalChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -378,6 +379,7 @@ private static function resolveRenamedParamValue(string $function, string $param
}
}
} catch (Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore reflection errors
}

Expand Down
5 changes: 5 additions & 0 deletions src/Internal/Checker/InlineChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -528,6 +532,7 @@ private static function classHasSelfOut(string $className): bool
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore reflection errors
}

Expand Down
7 changes: 5 additions & 2 deletions src/Internal/Checker/MemberAssignChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 [];
Expand Down
3 changes: 3 additions & 0 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -624,6 +625,7 @@ private static function inferTemplatesFromClosures(
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore reflection errors
}
}
Expand Down Expand Up @@ -1169,6 +1171,7 @@ private static function inferGenericObjectNode(
$targetTemplateNames = array_keys(DocblockExtractor::extractTemplates($targetPhpDocNode));
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
}
}
self::$classDeclaredTemplateNamesCache[$targetClassFqcn] = $targetTemplateNames;
Expand Down
37 changes: 37 additions & 0 deletions src/Internal/Diagnostic/DebugLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace TypePHP\Internal\Diagnostic;

use TypePHP\Internal\Util\Config;

/**
* Opt-in visibility for the exceptions TypePHP swallows in order to stay fail-open.
*
* @internal
*/
final class DebugLog
{
/**
* Writes a swallowed exception to the PHP error log when TYPEPHP_DEBUG is set; a no-op otherwise.
*/
public static function swallowed(\Throwable $e): void
{
if (! Config::isDebugEnabled()) {
return;
}

$catchSite = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];

error_log(\sprintf(
'[TypePHP Debug] Swallowed %s: %s (thrown at %s:%d, swallowed at %s:%d)',
$e::class,
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$catchSite['file'] ?? 'unknown',
$catchSite['line'] ?? 0
));
}
}
10 changes: 10 additions & 0 deletions src/Internal/Docblock/DocblockExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,6 +172,8 @@ public static function extractMagicPropertyContract(string $doc, string $propNam
'writeType' => $writeType,
];
} catch (\Throwable $e) {
DebugLog::swallowed($e);

return null;
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -516,6 +520,7 @@ public static function extractTypeFromPropertyDoc(string $doc, string $propName)
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore malformed property docblocks
}

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

Expand Down Expand Up @@ -664,6 +670,8 @@ public static function resolveImportedTypeAlias(string $fqcn, string $importedAl
return null;
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);

return null;
}
}
Expand Down Expand Up @@ -697,6 +705,7 @@ public static function extractTypeFromClassPropertyDoc(string $doc, string $prop
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore malformed class docblocks
}

Expand All @@ -716,6 +725,7 @@ public static function extractMagicMethodContract(string $doc, string $methodNam
}
}
} catch (\Throwable $e) {
DebugLog::swallowed($e);
// Silently ignore malformed class docblocks
}

Expand Down
Loading
Loading