Skip to content

Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows - #144

Merged
rcalicdan merged 2 commits into
typephp-php:mainfrom
naxvog:feat/debug-swallowed-exceptions
Dec 3, 2026
Merged

rcalicdan merged 2 commits 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.

@rcalicdan rcalicdan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, Can you you add // @codeCoverageIgnore comment on the line where the new Code is written like

  DebugLog::swallowed($e); // @codeCoverageIgnore

to supress line coverage on that specific line..

The catch blocks only run when TypePHP hits an internal error, so most of
them are never reached by the test suite. Mark each call with
@codeCoverageIgnore, as requested in review.
@naxvog

naxvog commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Done in 8d85787: all 89 DebugLog::swallowed($e); calls now carry // @codeCoverageIgnore. Pint, PHPStan and the test suite pass locally on PHP 8.5.

@rcalicdan rcalicdan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, will be merging now.

@rcalicdan
rcalicdan merged commit 610bfcd into typephp-php:main Dec 3, 2026
21 checks passed
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.

2 participants