PHPStan: Add dynamic return type extension for WP_CLI::do_hook() - #344
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
📝 WalkthroughWalkthroughChangesWP-CLI hook return type analysis
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant PHPStan
participant WPCliDoHookDynamicReturnTypeExtension
participant FileTypeMapper
participant WP_CLI
PHPStan->>WPCliDoHookDynamicReturnTypeExtension: Analyze WP_CLI::do_hook()
WPCliDoHookDynamicReturnTypeExtension->>FileTypeMapper: Resolve PHPDoc parameter types
FileTypeMapper-->>WPCliDoHookDynamicReturnTypeExtension: Return resolved type
WPCliDoHookDynamicReturnTypeExtension-->>PHPStan: Provide inferred return type
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.2.6)PHPStan was skipped because the config uses disallowed Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/PHPStan/WPCliDoHookDynamicReturnTypeExtension.php (1)
48-65: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winFirst
@paramtag is used unconditionally — fragile if the docblock ever has more than one param.The loop returns the type of whichever
@paramtag is encountered first ingetParamTags(). This works for the current test fixtures (each docblock has exactly one@param), but nothing enforces that invariant — a docblock documenting both the hook name and the filtered value (e.g.@param string $hook_name,@param array $value) would silently return the wrong type. Consider matching against the specific parameter representing the filtered value (e.g. by position/name) rather than "whichever is first."🤖 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 `@src/PHPStan/WPCliDoHookDynamicReturnTypeExtension.php` around lines 48 - 65, Update the parameter-type lookup in the resolved PHPDoc handling of the extension so it selects the tag corresponding to the filtered value parameter by its known name or position, rather than returning the first entry from getParamTags(). Preserve returning that matched parameter’s type and define the existing behavior when no matching tag is present.
🤖 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 `@tests/data/do_hook.php`:
- Around line 43-49: Update the assertType call for $formats_result in the
formatter_available_formats hook test to assert array<int, string> instead of
array<string>, matching the integer-keyed string array type documented by the
$formats annotation.
---
Nitpick comments:
In `@src/PHPStan/WPCliDoHookDynamicReturnTypeExtension.php`:
- Around line 48-65: Update the parameter-type lookup in the resolved PHPDoc
handling of the extension so it selects the tag corresponding to the filtered
value parameter by its known name or position, rather than returning the first
entry from getParamTags(). Preserve returning that matched parameter’s type and
define the existing behavior when no matching tag is present.
🪄 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: 0ff2159b-7d31-4bac-9621-fff389be8e38
📒 Files selected for processing (4)
extension.neonsrc/PHPStan/WPCliDoHookDynamicReturnTypeExtension.phptests/data/do_hook.phptests/tests/PHPStan/TestDynamicReturnTypeExtension.php
Inspired by WordPress/wordpress-develop#12022
Summary by CodeRabbit
New Features
WP_CLI::do_hook().Bug Fixes
nullreturn values.Tests