Skip to content

feat(checker): add runtime fatal-error prevention check#1320

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:fix/1273-runtime-fatal-error-prevention
Open

feat(checker): add runtime fatal-error prevention check#1320
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:fix/1273-runtime-fatal-error-prevention

Conversation

@faisalahammad
Copy link
Copy Markdown
Contributor

@faisalahammad faisalahammad commented May 25, 2026

Summary

Adds a new static check Runtime_Fatal_Error_Prevention_Check and corresponding PHPCS sniff RuntimeFatalErrorPreventionSniff to analyze plugins for high-risk coding patterns that commonly trigger runtime fatal errors. These include unguarded imports, direct/unguarded class instantiations or function calls of optional components, dynamic callback definitions that might be missing, and incorrect class method hook attachments.

Fixes #1273

Changes

phpcs-rulesets/plugin-check.ruleset.xml

Before:

	<!-- Prohibit the use of the `goto` PHP language construct. -->
	<rule ref="Generic.PHP.DiscourageGoto.Found">

After:

	<!-- Prevent runtime fatal errors. -->
	<rule ref="PluginCheck.CodeAnalysis.RuntimeFatalErrorPrevention"/>

	<!-- Prohibit the use of the `goto` PHP language construct. -->
	<rule ref="Generic.PHP.DiscourageGoto.Found">

Why: Registers the new custom PHPCS sniff under the core plugin-check ruleset to run as part of the PHPCS static checks.

includes/Checker/Default_Check_Repository.php

Before:

				\'wp_functions_compatibility\' => new Checks\\Plugin_Repo\\WP_Functions_Compatibility_Check(),
			)
		);

After:

				\'wp_functions_compatibility\'     => new Checks\\Plugin_Repo\\WP_Functions_Compatibility_Check(),
				\'runtime_fatal_error_prevention\' => new Checks\\Plugin_Repo\\Runtime_Fatal_Error_Prevention_Check(),
			)
		);

Why: Registers the new Runtime_Fatal_Error_Prevention_Check static check class into the default check repository so it gets run automatically during plugin checks.

Testing

Test 1: Run Sniff Unit Tests

  1. Run PHPCS unit tests on the custom sniff:
    ./vendor/bin/phpunit phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/RuntimeFatalErrorPreventionUnitTest.php

Result: Works as expected (All tests pass)

Test 2: Run Plugin Check PHPUnit Integration Tests

  1. Start the test stack: npm run wp-env:start:tests
  2. Run checker unit tests:
    npm run test-php -- --filter=Runtime_Fatal_Error_Prevention_Check_Tests

Result: Works as expected (All tests pass)

- Implement RuntimeFatalErrorPreventionSniff to parse and analyze common runtime fatal patterns.
- Register check in Default_Check_Repository and ruleset.
- Add comprehensive PHPUnit and sniff unit tests.

Fixes WordPress#1273
@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: faisalahammad <faisalahammad@git.wordpress.org>
Co-authored-by: davidperezgar <davidperez@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

Add check: runtime fatal-error prevention in PHP plugin code

1 participant