Skip to content

Commit 914a244

Browse files
committed
Functions/StripTags: add support for handling PHP 8.1 first class callables
First class callables are basically syntax sugar for closures. So: ```php add_action('my_action', strip_tags(...)); ``` ... could also be written as: ```php add_action('my_action', function(...$args) { return strip_tags(...$args); }); ``` Example: https://3v4l.org/cra8s#veol As the code would be flagged when used in a closure, it is my opinion, it should also be flagged when used as a first class callable. This commit implements this in a WPCS cross-version compatible manner. Prior to WPCS 3.2.0, first class callables would be send to the `process_no_parameters()` method. As of WPCS 3.2.0, they will be send to the dedicated `process_first_class_callable()` method. See WordPress/WordPress-Coding-Standards#2544
1 parent 1318d56 commit 914a244

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

WordPressVIPMinimum/Sniffs/Functions/StripTagsSniff.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public function process_no_parameters( $stackPtr, $group_name, $matched_content
7474
$this->add_warning( $stackPtr );
7575
}
7676

77+
/**
78+
* Process the function if it is used as a first class callable.
79+
*
80+
* @param int $stackPtr The position of the current token in the stack.
81+
* @param string $group_name The name of the group which was matched.
82+
* @param string $matched_content The token content (function name) which was matched
83+
* in lowercase.
84+
*
85+
* @return void
86+
*/
87+
public function process_first_class_callable( $stackPtr, $group_name, $matched_content ) {
88+
$this->add_warning( $stackPtr );
89+
}
90+
7791
/**
7892
* Add a warning if the function is used at all.
7993
*

WordPressVIPMinimum/Tests/Functions/StripTagsUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ strip_tags(allowed_tags: $allowed); // Warning. Invalid function call, but that'
4444

4545
\strip_tags(string: $html, allowed_tag: $allowed); // Warning (mind: deliberate typo in param name).
4646
strip_tags(html: $html); // Warning (mind: deliberately using incorrect param name).
47+
48+
add_action('my_action', strip_tags(...)); // PHP 8.1 first class callable.

WordPressVIPMinimum/Tests/Functions/StripTagsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function getWarningList() {
4444
43 => 1,
4545
45 => 1,
4646
46 => 1,
47+
48 => 1,
4748
];
4849
}
4950
}

0 commit comments

Comments
 (0)