humanmade / phpstan-rules
PHPStan rules for WordPress projects, from Human Made.
Package info
github.com/humanmade/phpstan-rules
Type:phpstan-extension
pkg:composer/humanmade/phpstan-rules
Requires
- php: >=8.2
- phpstan/phpstan: ^2.1
- szepeviktor/phpstan-wordpress: ^2.0
Requires (Dev)
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 13:16:33 UTC
README
PHPStan rules for WordPress projects. They find problems that need type information, or need to look across more than one function or file. PHP_CodeSniffer can't do either, so these rules sit alongside humanmade/coding-standards rather than replacing it.
Install
composer require --dev humanmade/phpstan-rules
With phpstan/extension-installer the rules load automatically. Without it, add both extensions to your phpstan.neon:
includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon - vendor/humanmade/phpstan-rules/extension.neon
The package needs PHPStan 2 and depends on szepeviktor/phpstan-wordpress for the WordPress stubs.
The rules run at every PHPStan level. Rules that join data across files run only when PHPStan analyses the whole project, not when you analyse a single file.
Rules
| Identifier | Finds |
|---|---|
hm.requestHandler.missingCapability |
A request handler that never checks the user's capability |
hm.requestHandler.missingNonce |
A request handler that writes data but never checks a nonce |
hm.restRoute.missingPermissionCallback |
A REST route with no permission_callback |
hm.restRoute.publicWrite |
A REST route that accepts writes but lets anyone call it |
hm.localizeScript.duplicateObjectName |
Two scripts that share a wp_localize_script() object name |
Request handlers must check capabilities
add_action( 'wp_ajax_reindex', [ self::class, 'reindex' ] ); public static function reindex() { check_ajax_referer( 'reindex' ); // Any logged-in user, including a subscriber, gets here. self::rebuild( sanitize_key( $_POST['post_type'] ) ); }
A nonce proves that the request came from your page. It does not prove that the user is allowed to do the action. An AJAX or admin_post handler needs a capability check as well.
The rule checks these handlers:
wp_ajax_{action}andadmin_post_{action}. It skips thenoprivversions, and it skips an action that is also registered asnopriv, because that action is public on purpose.admin_init. It checks this hook only when the handler reads request input ($_POST,$_GET,$_REQUEST,$_FILES,filter_input()orphp://input) and writes data.admin_initalso runs for logged-out requests toadmin-ajax.php, so a handler there without a check can be run by anyone.
You can add save_post, save_post_{post_type} and edit_post to inputWriteHooks, as shown under Configuration. They are off by default because when a post is saved from the editor, WordPress has already checked the nonce and the edit_post capability. The rule still finds meta-box handlers that skip WordPress's guidance to check both.
It also skips a handler that a public hook runs too, for example a function that template_redirect also calls to serve the same export publicly. The public hooks are the nopriv hooks, init, wp_loaded, parse_request, wp and template_redirect.
The handler passes when it calls current_user_can(), or a similar function, itself or in any function it calls. Checks inside closures the handler runs count too, but not closures it only registers as callbacks with add_action() or add_filter(). Checks in a subclass that overrides a method the handler calls also count. So does a check made earlier in the function that registers the hook, such as if ( ! current_user_can( 'manage_options' ) ) { return; } before the add_action() call. The hook, the handler and the check can be in different files. Callbacks can be function names, [ $this, 'method' ], [ Foo::class, 'method' ], 'Foo::method', closures or first-class callables.
Request handlers that write must check a nonce
add_action( 'admin_post_example_save', 'example_save' ); function example_save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die(); } // No nonce, so another site can make an admin's browser send this request. update_option( 'example', sanitize_text_field( $_POST['example'] ) ); }
This rule checks the same handlers as the capability rule, but only when the handler writes data. WPCS's WordPress.Security.NonceVerification sniff also looks for nonces. That sniff only sees one function, so it can't see a nonce checked in a helper. This rule can.
REST routes need a permission callback
register_rest_route( 'example/v1', '/items', [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => __NAMESPACE__ . '\\create_item', 'permission_callback' => '__return_true', ] );
hm.restRoute.missingPermissionCallback reports a route with no permission_callback. WordPress then lets anyone call the route, and it only logs a notice.
hm.restRoute.publicWrite reports a route that accepts POST, PUT, PATCH or DELETE when its permission callback always returns true. That includes '__return_true', fn () => true, and a closure that only returns true. It also includes a callback that calls wp_send_json_error() to stop the request and otherwise returns true. Return false or a WP_Error from the permission callback instead. A public read route is fine.
wp_localize_script() object names must be unique
wp_localize_script( 'theme-main', 'themeVars', [ 'nonce' => wp_create_nonce( 'contact' ) ] ); wp_localize_script( 'theme-counter', 'themeVars', [ 'locale' => get_locale() ] );
Each call prints var themeVars = {...} before its script. The second one replaces the first, so the main script loses its nonce. The rule compares calls in the same function, method or closure. Calls in different functions often run on different pages, such as admin and front end, so it doesn't compare them.
When a rule stays silent
These rules only report what they can prove. They say nothing when:
- the hook name or route is built at runtime, such as
'wp_ajax_' . $action - the callback can't be resolved to one function or method
- the handler is in code that PHPStan isn't analysing
- the handler, or a function it calls directly, calls something PHPStan can't follow, such as a method on a
mixedvalue or a function from a plugin that isn't installed
WordPress core functions, PHP built-ins and code under vendor/ count as known code that makes no checks.
"Reaches a check" is not the same as "enforces a check". A handler passes if it calls current_user_can() anywhere on its path, even if it ignores the result or checks after it writes.
Writes count anywhere on the path too. A handler that only reads, but calls a helper that caches its result with update_option(), counts as writing.
Configuration
Add your own check helpers so the rules can see them. Lists in NEON merge with the defaults:
parameters: hmPhpstan: requestHandlers: capabilityChecks: - 'Example\Auth\require_admin' - 'Example\Auth::require_capability' nonceChecks: - 'Example\Security\verify_request' writeFunctions: - 'Example\Store\save' inputWriteHooks: - save_post - save_post_* - edit_post
| Parameter | Default |
|---|---|
alwaysCapabilityHooks |
wp_ajax_*, admin_post_* |
publicHooks |
wp_ajax_nopriv_*, admin_post_nopriv_*, init, wp_loaded, parse_request, wp, template_redirect |
inputWriteHooks |
admin_init |
capabilityChecks |
current_user_can, user_can, current_user_can_for_site, current_user_can_for_blog, author_can, is_super_admin, WP_User::has_cap |
nonceChecks |
wp_verify_nonce, check_admin_referer, check_ajax_referer |
writeFunctions |
Option, meta, post, term, user and comment writes, file writes, wp_mail() and wp_remote_post(). See rules.neon for the full list. $wpdb->insert(), update(), delete() and replace() always count. $wpdb->query() counts unless its SQL is a known SELECT, SHOW, DESCRIBE or EXPLAIN. |
Turn a rule off with its identifier:
parameters: ignoreErrors: - identifier: hm.requestHandler.missingNonce
To accept one finding, add // @phpstan-ignore hm.restRoute.publicWrite on the line above, with a note that says how the route is protected.
Development
composer install
composer test
composer analyse
License
GPL-2.0-or-later.