Skip to content

Commit 7920dd0

Browse files
committed
Build/Test Tools: Fix typing and logic issues in MockAction.
* Add type information. * Fix erroneous fallback case for `MockAction::current_filter()` in how it gets the last key of `$wp_actions`. * Fix `MockAction::get_call_count()` to handle getting counts of a provided filter in addition to a provided action. Developed in WordPress#12618. See #64898, #64894. git-svn-id: https://develop.svn.wordpress.org/trunk@62808 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a8b8710 commit 7920dd0

1 file changed

Lines changed: 76 additions & 12 deletions

File tree

tests/phpunit/includes/utils.php

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,43 +58,73 @@ function strip_ws( $txt ) {
5858
* add_action( 'foo', array( &$ma, 'action' ) );
5959
*
6060
* @since UT (3.7.0)
61+
*
62+
* @phpstan-type Hook_Event array{
63+
* action?: non-empty-string,
64+
* filter?: non-empty-string,
65+
* hook_name: string|false,
66+
* tag: string|false,
67+
* args: list<mixed>,
68+
* }
6169
*/
6270
class MockAction {
71+
72+
/**
73+
* @var list<Hook_Event>
74+
*/
6375
public $events;
76+
77+
/**
78+
* @var bool
79+
*/
6480
public $debug;
6581

6682
/**
6783
* PHP5 constructor.
6884
*
6985
* @since UT (3.7.0)
86+
*
87+
* @param bool|int $debug
7088
*/
7189
public function __construct( $debug = 0 ) {
7290
$this->reset();
73-
$this->debug = $debug;
91+
$this->debug = (bool) $debug;
7492
}
7593

7694
/**
7795
* @since UT (3.7.0)
7896
*/
79-
public function reset() {
97+
public function reset(): void {
8098
$this->events = array();
8199
}
82100

83101
/**
84102
* @since UT (3.7.0)
103+
*
104+
* @global array<non-empty-string, non-negative-int> $wp_actions
105+
* @return string|false
85106
*/
86107
public function current_filter() {
87108
global $wp_actions;
88109

89110
if ( is_callable( 'current_filter' ) ) {
90-
return current_filter();
111+
$current_filter = current_filter();
112+
} else {
113+
$current_filter = array_key_last( $wp_actions );
114+
if ( null === $current_filter ) {
115+
$current_filter = false;
116+
}
91117
}
92118

93-
return end( $wp_actions );
119+
return $current_filter;
94120
}
95121

96122
/**
97123
* @since UT (3.7.0)
124+
*
125+
* @template T
126+
* @param T $arg
127+
* @return T
98128
*/
99129
public function action( $arg ) {
100130
$current_filter = $this->current_filter();
@@ -115,6 +145,10 @@ public function action( $arg ) {
115145

116146
/**
117147
* @since UT (3.7.0)
148+
*
149+
* @template T
150+
* @param T $arg
151+
* @return T
118152
*/
119153
public function action2( $arg ) {
120154
$current_filter = $this->current_filter();
@@ -135,6 +169,10 @@ public function action2( $arg ) {
135169

136170
/**
137171
* @since UT (3.7.0)
172+
*
173+
* @template T
174+
* @param T $arg
175+
* @return T
138176
*/
139177
public function filter( $arg ) {
140178
$current_filter = $this->current_filter();
@@ -155,6 +193,10 @@ public function filter( $arg ) {
155193

156194
/**
157195
* @since UT (3.7.0)
196+
*
197+
* @template T
198+
* @param T $arg
199+
* @return T
158200
*/
159201
public function filter2( $arg ) {
160202
$current_filter = $this->current_filter();
@@ -175,8 +217,12 @@ public function filter2( $arg ) {
175217

176218
/**
177219
* @since UT (3.7.0)
220+
*
221+
* @no-named-arguments
222+
* @param string $arg
223+
* @return non-empty-string
178224
*/
179-
public function filter_append( $arg ) {
225+
public function filter_append( $arg ): string {
180226
$current_filter = $this->current_filter();
181227

182228
if ( $this->debug ) {
@@ -197,8 +243,12 @@ public function filter_append( $arg ) {
197243
* Does not return the result, so it's safe to use with the 'all' filter.
198244
*
199245
* @since UT (3.7.0)
246+
*
247+
* @no-named-arguments
248+
* @param string $hook_name
249+
* @param mixed ...$args
200250
*/
201-
public function filterall( $hook_name, ...$args ) {
251+
public function filterall( string $hook_name, ...$args ): void {
202252
$current_filter = $this->current_filter();
203253

204254
if ( $this->debug ) {
@@ -217,22 +267,30 @@ public function filterall( $hook_name, ...$args ) {
217267
* Returns a list of all the actions, hook names and args.
218268
*
219269
* @since UT (3.7.0)
270+
*
271+
* @return list<Hook_Event>
220272
*/
221-
public function get_events() {
273+
public function get_events(): array {
222274
return $this->events;
223275
}
224276

225277
/**
226278
* Returns a count of the number of times the action was called since the last reset.
227279
*
228280
* @since UT (3.7.0)
281+
*
282+
* @param string $hook_name Hook name.
283+
* @return non-negative-int
229284
*/
230-
public function get_call_count( $hook_name = '' ) {
285+
public function get_call_count( $hook_name = '' ): int {
231286
if ( $hook_name ) {
232287
$count = 0;
233288

234289
foreach ( $this->events as $e ) {
235-
if ( $e['action'] === $hook_name ) {
290+
if (
291+
( isset( $e['action'] ) && $e['action'] === $hook_name ) ||
292+
( isset( $e['filter'] ) && $e['filter'] === $hook_name )
293+
) {
236294
++$count;
237295
}
238296
}
@@ -247,8 +305,10 @@ public function get_call_count( $hook_name = '' ) {
247305
* Returns an array of the hook names that triggered calls to this action.
248306
*
249307
* @since 6.1.0
308+
*
309+
* @return list<string|false>
250310
*/
251-
public function get_hook_names() {
311+
public function get_hook_names(): array {
252312
$out = array();
253313

254314
foreach ( $this->events as $e ) {
@@ -263,17 +323,21 @@ public function get_hook_names() {
263323
*
264324
* @since UT (3.7.0)
265325
* @since 6.1.0 Turned into an alias for ::get_hook_names().
326+
*
327+
* @return list<string|false>
266328
*/
267-
public function get_tags() {
329+
public function get_tags(): array {
268330
return $this->get_hook_names();
269331
}
270332

271333
/**
272334
* Returns an array of args passed in calls to this action.
273335
*
274336
* @since UT (3.7.0)
337+
*
338+
* @return list<list<mixed>>
275339
*/
276-
public function get_args() {
340+
public function get_args(): array {
277341
$out = array();
278342

279343
foreach ( $this->events as $e ) {

0 commit comments

Comments
 (0)