Skip to content

PHPStan: Add rule for WP_CLI::add_hook() usage - #345

Open
swissspidy wants to merge 2 commits into
mainfrom
add/add-hook-extension
Open

PHPStan: Add rule for WP_CLI::add_hook() usage#345
swissspidy wants to merge 2 commits into
mainfrom
add/add-hook-extension

Conversation

@swissspidy

@swissspidy swissspidy commented Jul 30, 2026

Copy link
Copy Markdown
Member

Complements #344

Summary by CodeRabbit

  • New Features

    • Added static analysis for WP_CLI::add_hook() callbacks.
    • Detects callbacks that require more parameters than the corresponding hook provides.
    • Reports invalid or non-callable callback definitions.
  • Tests

    • Added coverage for valid callback signatures, excessive parameters, and invalid callback names.

@swissspidy swissspidy added this to the 5.1.12 milestone Jul 30, 2026
@swissspidy
swissspidy requested a review from a team as a code owner July 30, 2026 09:46
@swissspidy swissspidy added scope:documentation Related to documentation scope:testing Related to testing labels Jul 30, 2026
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@swissspidy, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 54a5ccdc-1daa-4334-a1ed-09d644ef825a

📥 Commits

Reviewing files that changed from the base of the PR and between ee89fcf and 881bfc8.

📒 Files selected for processing (1)
  • src/PHPStan/WPCliAddHookCallbackRule.php
📝 Walkthrough

Walkthrough

Adds and registers a PHPStan rule that validates WP_CLI::add_hook() callback callability and required parameter counts for recognized hooks, with fixtures and PHPUnit assertions covering valid and invalid registrations.

Changes

Hook callback validation

Layer / File(s) Summary
Implement and register callback validation
src/PHPStan/WPCliAddHookCallbackRule.php, extension.neon
Adds the PHPStan rule and registers it in the extension configuration.
Define valid and invalid hook registrations
tests/data/add_hook_rule.php
Adds fixtures for recognized hooks, optional parameters, excessive required parameters, and invalid callable names.
Assert PHPStan diagnostics
tests/tests/PHPStan/TestWPCliAddHookCallbackRule.php
Analyzes the fixture and asserts three expected diagnostics with line numbers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PHPStan
  participant WPCliAddHookCallbackRule
  participant WP_CLI
  PHPStan->>WPCliAddHookCallbackRule: process StaticCall node
  WPCliAddHookCallbackRule->>WP_CLI: inspect add_hook callback and hook name
  WPCliAddHookCallbackRule-->>PHPStan: return callable or argument-count error
Loading

Suggested reviewers: brianhenryie, ernilambar, janw-me

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: a new PHPStan rule for WP_CLI::add_hook() usage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Fix failing CI checks
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add/add-hook-extension

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.24590% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/PHPStan/WPCliAddHookCallbackRule.php 85.24% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/data/add_hook_rule.php (1)

17-17: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove redundant callback-body comments.

The surrounding scenario labels already describe each case; these // valid and // invalid comments add no test intent.

As per coding guidelines, “Write only high-value inline comments and avoid addressing users through code comments.”

Also applies to: 25-25, 33-33, 41-41, 49-49

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/data/add_hook_rule.php` at line 17, Remove the redundant `// valid` and
`// invalid` comments from the callback bodies in the affected test cases,
including the locations referenced by the review. Keep the surrounding scenario
labels and test behavior unchanged.

Source: Coding guidelines

tests/tests/PHPStan/TestWPCliAddHookCallbackRule.php (1)

14-14: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Align the PHPStan rule test with the required WP_CLI\Tests\TestCase contract.

TestWPCliAddHookCallbackRule directly extends PHPStan\Testing\RuleTestCase; PHPStan tests cannot inherit both PHPStan’s rule base and the required WP-CLI test base, so either document this as an intentional phpstan-extension exception or refit the test through an approved bridge/base class.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/tests/PHPStan/TestWPCliAddHookCallbackRule.php` at line 14, Update
TestWPCliAddHookCallbackRule to satisfy the required WP_CLI\Tests\TestCase
contract: use the approved bridge/base class if one exists, otherwise document
this direct RuleTestCase inheritance as an intentional phpstan-extension
exception. Preserve the test’s PHPStan rule-testing behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/PHPStan/WPCliAddHookCallbackRule.php`:
- Around line 69-76: Update both RuleErrorBuilder::message() sites in
WPCliAddHookCallbackRule::processNode() to assign stable identifier() values
before build(): one identifier for invalid callbacks at lines 69-76 and a
distinct identifier for insufficient required parameters at lines 107-118.

---

Nitpick comments:
In `@tests/data/add_hook_rule.php`:
- Line 17: Remove the redundant `// valid` and `// invalid` comments from the
callback bodies in the affected test cases, including the locations referenced
by the review. Keep the surrounding scenario labels and test behavior unchanged.

In `@tests/tests/PHPStan/TestWPCliAddHookCallbackRule.php`:
- Line 14: Update TestWPCliAddHookCallbackRule to satisfy the required
WP_CLI\Tests\TestCase contract: use the approved bridge/base class if one
exists, otherwise document this direct RuleTestCase inheritance as an
intentional phpstan-extension exception. Preserve the test’s PHPStan
rule-testing behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8507a998-a180-4c45-af4a-a0b09e122a87

📥 Commits

Reviewing files that changed from the base of the PR and between d339d2d and ee89fcf.

📒 Files selected for processing (4)
  • extension.neon
  • src/PHPStan/WPCliAddHookCallbackRule.php
  • tests/data/add_hook_rule.php
  • tests/tests/PHPStan/TestWPCliAddHookCallbackRule.php

Comment thread src/PHPStan/WPCliAddHookCallbackRule.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:documentation Related to documentation scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant