Skip to content

Build/Test Tools: Check prepared SQL with PHPStan instead of PHPCS - #15856

Draft
johnbillion wants to merge 2 commits into
WordPress:trunkfrom
johnbillion:phpstan-wpdb-rules
Draft

johnbillion wants to merge 2 commits into
WordPress:trunkfrom
johnbillion:phpstan-wpdb-rules

Conversation

@johnbillion

Copy link
Copy Markdown
Member

Ports two WordPress.DB sniff checks to PHPStan rules that report under the same codes:

  • WordPress.DB.PreparedSQL.InterpolatedNotPrepared
  • WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare

The rules work on the syntax tree, recognize the database object by type as well as by the name $wpdb, and handle a query passed by name. The two checks are disabled in phpcs.xml.dist, the phpcs:ignore comments written for them are removed, and the errors those comments covered move into the PHPStan baselines.

Depends on #15850. This branch is stacked on it, so the first commit here is that PR's. Only the last commit belongs to this PR.

Trac ticket:

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Writing the rules and tests, and preparing this PR.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Adds a PHPUnit suite for the custom PHPStan extensions in `tests/phpstan`, built on PHPStan's test harness. The suite runs with `composer phpstan:test` or `npm run typecheck:php:test`, and runs in CI ahead of the static analysis.

See #66634.
Ports the `InterpolatedNotPrepared` check of the `WordPress.DB.PreparedSQL` sniff and the `UnfinishedPrepare` check of the `WordPress.DB.PreparedSQLPlaceholders` sniff to PHPStan rules, which report under the same codes. The rules recognize the database object by its type as well as by the name `$wpdb`, and a query passed by name.

The two checks are disabled in `phpcs.xml.dist`, the `phpcs:ignore` comments written for them are removed, and the errors they covered are added to the PHPStan baselines.
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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