Skip to content

Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows - #144

Open
naxvog wants to merge 1 commit into
typephp-php:mainfrom
naxvog:feat/debug-swallowed-exceptions
Open

naxvog wants to merge 1 commit into
typephp-php:mainfrom
naxvog:feat/debug-swallowed-exceptions

Conversation

@naxvog

@naxvog naxvog commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Reimplements #130 on top of current main, as requested there after the internal refactoring.

Problem

TypePHP stays fail-open by catching \Throwable (or \ReflectionException) and discarding it in 89 places across the transformer, docblock parser and runtime checkers. When one of them fires, a check is skipped or a file is left uninstrumented, and nothing says why.

Change

  • New TypePHP\Internal\Diagnostic\DebugLog::swallowed(\Throwable $e). It is a no-op unless the TYPEPHP_DEBUG environment variable is truthy.
  • Every catch block that discards its exception now calls it. Catch blocks that rethrow (CallableWrapper) or already report the exception (RunCommand) are unchanged.
  • Config::isDebugEnabled() reads the variable. There is no config file key; this is a diagnostic switch, not project configuration.

With TYPEPHP_DEBUG=1 the PHP error log gets one line per swallowed exception:

[TypePHP Debug] Swallowed PhpParser\Error: Syntax error, unexpected T_STRING on line 1 (thrown at vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php:403, swallowed at src/Internal/Io/StreamWrapper.php:285)

Default behaviour is unchanged.

Notes

  • Debug mode also logs fallbacks that are expected, such as a ReflectionException for a function that does not exist, so the log is verbose by design.
  • The exception message is logged as is; redact_values is not applied to it.
  • With the variable unset the added cost is one static call and an environment lookup, and only on a path that has already thrown.

Testing

tests/Internal/Diagnostic/DebugLogTest.php covers: silent by default, the logged line with throw and catch site, and a source file that does not parse being reported while still returned untouched. Pint, PHPStan and the full Pest suite pass on PHP 8.5.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant