Add PHPStan custom rule for get_subcribed_events() callbacks validation#1019
Add PHPStan custom rule for get_subcribed_events() callbacks validation#1019wordpressfan merged 5 commits intodevelopfrom
get_subcribed_events() callbacks validation#1019Conversation
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
|
@remyperona do u think it'd be better to have this annotation on the method itself not into the subscriber array item? |
|
One method can be on multiple hooks, but the same rules apply: a return for a method on a filter, no return for a method on an action. So in the end, it is the hook that determines what should happen in the method, not the other way around. |
OK, thanks @remyperona |
Description
Port the existing validations for callbacks of
add_filter()andadd_action()to the subscriber system andget_subscribed_events()array returnedéType of change
SubscriberCallbackRule
This rule validates
get_subscribed_events()method implementations in classes that implementSubscriberInterface.Purpose
Ensures that:
@filteror@actionannotationsaccepted_argswhen specifiedUsage
Add single-line comment annotations to each array element in
get_subscribed_events():Supported Array Formats
Simple string method name:
Array with method and priority:
Array with method, priority, and accepted_args:
Nested arrays (multiple callbacks for same hook):
Note: Annotation on parent element applies to all nested callbacks.
Variable assignment:
Filter vs Action Guidelines
Filters (
// @filter)Actions (
// @action)Error Identifiers
wpmedia.subscriber.missingAnnotation- Missing @filter or @action annotationwpmedia.subscriber.filter.missingReturn- Filter callback doesn't return a valuewpmedia.subscriber.action.unexpectedReturn- Action callback returns a valuewpmedia.subscriber.arguments.count- Parameter count mismatch with accepted_argsSkipped Validations
The rule automatically skips validation for:
return ParentClass::get_subscribed_events())Examples
Valid Implementation
Invalid Implementation (Will Show Errors)