Check the hook callback parameter count ?
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 25/100
Rechercherichtung
Beginnen Sie mit der Überprüfung des vorhandenen AlwaysReturnInFilter-Sniffs sowie der in den verlinkten WordPress-Referenzen dokumentierten Einstiegspunkte add_filter() und add_action(). Klären Sie, wie Callback-Deklarationen und accepted_args verglichen werden sollten, einschließlich optionaler Parameter und Closures, bevor Sie den Umfang des Sniffs festlegen. Die Aufgabe ist abgeschlossen, wenn das Verhalten für die bereitgestellten Beispiele für Verstöße und Nichtverstöße spezifiziert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
What problem would the enhancement address for VIP?
Prevent potential fatal ArgumentCount errors.
Describe the solution you'd like
Inspired by the AlwaysReturnInFilter sniff, I was thinking that once the abstract hook/callback sniff exists, it wouldn't be that hard to create a sniff which would check synchronicity between the parameters requested from a hook-in and the callback function declaration.
This would apply to both add_filter() as well as add_action().
Such a sniff would also be a candidate for (eventually moving to) WPCS, but I wanted to discuss the general idea here first, largely to see if the potential issue described above actually occurs in real life situations.
What code should be reported as a violation?
// Error: Two parameters expected, one (implicit) requested.
add_filter( 'the_title', function( $title, $id ) {
// Do something.
return $title;
} );
// Warning: One parameter expected, two requested.
add_filter( 'the_title', function( $title ) {
// Do something.
return $title;
}, 10, 2 );
What code should not be reported as a violation?
// OK: Two parameters expected, two requested.
add_filter( 'the_title', function( $title, $id ) {
// Do something.
return $title;
}, 10, 2 );
// OK: Three parameters expected, two requested, but third parameter is optional,
// which can be done by design to allow direct calls to the function to pass it.
function prefix_title_filter( $title, $id, $optional = null ) {
// Do something.
return $title;
}
add_filter( 'the_title', 'prefix_title_filter', 10, 2 );
Additional context
Future scope
- Once a list of WP native hooks + the amount of parameters they pass is known (via a trait in WPCS for instance), an additional check could be added that the
$accepted_argsparameter value is never higher than the number of passed parameters when the hook is called.
function prefix_title_filter( $title, $id, $optional = null ) {
// Do something.
return $title;
}
// The `the_title` filter only passes two arguments max, so the code here is an error.
add_filter( 'the_title', 'prefix_title_filter', 10, 3 );
- For PHP native functions used in callbacks, reflection could be used to see how many (required) parameters these take and match that with the
$accepted_argsparameter. - For WP native functions a list based solution would be needed to do the same.
Relevant links
- Vorherrschende Sprache
- PHP
- Sterne
- 261
- Forks
- 44
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus Automattic/VIP-Coding-Standards
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading itEvtl. vergeben @tomjn hat das vor 6 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its ifEvtl. vergeben @tomjn hat das vor 6 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 76/100
-
Suppress filters in get_posts false positiveEvtl. vergeben @tomjn hat das vor 7 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 48/100
-
Breaking Change Type: Maintenance
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
Automattic/VIP-Coding-Standards#849 · 1 Kommentar ·
Alle Issues in Automattic/VIP-Coding-Standards
Ähnliche Issues
-
extension/Commercial needs-triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
crazy-goat/rabbit-stream#753 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
opensourcepos/opensourcepos#4743 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
OpenConext/OpenConext-engineblock#2129 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 82/100
grokability/snipe-it#19786 ·
Maintainer antworten meist innerhalb von 1 Tag