@@ -137,17 +137,20 @@ class Command {
137137 public function stage ( $ args , $ assoc_args ) {
138138 global $ wpdb ;
139139
140- $ focus = Utils \get_flag_value ( $ assoc_args , 'all ' , isset ( $ args [0 ] ) ? $ args [0 ] : null );
140+ /** @var array<string, bool|string> $typed_assoc_args */
141+ $ typed_assoc_args = self ::get_typed_assoc_args ( $ assoc_args );
142+ $ focus = Utils \get_flag_value ( $ typed_assoc_args , 'all ' , isset ( $ args [0 ] ) ? $ args [0 ] : null );
141143
142- $ order = Utils \get_flag_value ( $ assoc_args , 'order ' , 'ASC ' );
143- $ orderby = Utils \get_flag_value ( $ assoc_args , 'orderby ' , null );
144+ $ order_val = Utils \get_flag_value ( $ typed_assoc_args , 'order ' , 'ASC ' );
145+ $ order = is_string ( $ order_val ) ? $ order_val : 'ASC ' ;
146+ $ orderby_val = Utils \get_flag_value ( $ typed_assoc_args , 'orderby ' , null );
147+ $ orderby = ( is_string ( $ orderby_val ) || is_null ( $ orderby_val ) ) ? $ orderby_val : null ;
144148
145149 $ valid_stages = array ( 'bootstrap ' , 'main_query ' , 'template ' );
146150 if ( $ focus && ( true !== $ focus && ! in_array ( $ focus , $ valid_stages , true ) ) ) {
147151 WP_CLI ::error ( 'Invalid stage. Must be one of ' . implode ( ', ' , $ valid_stages ) . ', or use --all. ' );
148152 }
149153
150- assert ( is_bool ( $ focus ) || is_string ( $ focus ) || is_null ( $ focus ) );
151154 $ profiler = new Profiler ( 'stage ' , $ focus );
152155 $ profiler ->run ();
153156
@@ -184,9 +187,10 @@ public function stage( $args, $assoc_args ) {
184187 );
185188 }
186189 $ fields = array_merge ( $ base , $ metrics );
187- $ formatter = new Formatter ( $ assoc_args , $ fields );
190+ $ formatter = new Formatter ( $ typed_assoc_args , $ fields );
188191 $ loggers = $ profiler ->get_loggers ();
189- if ( Utils \get_flag_value ( $ assoc_args , 'spotlight ' ) ) {
192+ /** @var array<string, bool|string> $typed_assoc_args */
193+ if ( Utils \get_flag_value ( $ typed_assoc_args , 'spotlight ' ) ) {
190194 $ loggers = self ::shine_spotlight ( $ loggers , $ metrics );
191195 }
192196
@@ -269,10 +273,14 @@ public function stage( $args, $assoc_args ) {
269273 */
270274 public function hook ( $ args , $ assoc_args ) {
271275
272- $ focus = Utils \get_flag_value ( $ assoc_args , 'all ' , isset ( $ args [0 ] ) ? $ args [0 ] : null );
276+ /** @var array<string, bool|string> $typed_assoc_args */
277+ $ typed_assoc_args = self ::get_typed_assoc_args ( $ assoc_args );
278+ $ focus = Utils \get_flag_value ( $ typed_assoc_args , 'all ' , isset ( $ args [0 ] ) ? $ args [0 ] : null );
273279
274- $ order = Utils \get_flag_value ( $ assoc_args , 'order ' , 'ASC ' );
275- $ orderby = Utils \get_flag_value ( $ assoc_args , 'orderby ' , null );
280+ $ order_val = Utils \get_flag_value ( $ typed_assoc_args , 'order ' , 'ASC ' );
281+ $ order = is_string ( $ order_val ) ? $ order_val : 'ASC ' ;
282+ $ orderby_val = Utils \get_flag_value ( $ typed_assoc_args , 'orderby ' , null );
283+ $ orderby = ( is_string ( $ orderby_val ) || is_null ( $ orderby_val ) ) ? $ orderby_val : null ;
276284
277285 $ profiler = new Profiler ( 'hook ' , $ focus );
278286 $ profiler ->run ();
@@ -300,13 +308,16 @@ public function hook( $args, $assoc_args ) {
300308 'request_count ' ,
301309 );
302310 $ fields = array_merge ( $ base , $ metrics );
303- $ formatter = new Formatter ( $ assoc_args , $ fields );
311+ $ formatter = new Formatter ( $ typed_assoc_args , $ fields );
304312 $ loggers = $ profiler ->get_loggers ();
305- if ( Utils \get_flag_value ( $ assoc_args , 'spotlight ' ) ) {
313+ /** @var array<string, bool|string> $typed_assoc_args */
314+ if ( Utils \get_flag_value ( $ typed_assoc_args , 'spotlight ' ) ) {
306315 $ loggers = self ::shine_spotlight ( $ loggers , $ metrics );
307316 }
308- $ search = Utils \get_flag_value ( $ assoc_args , 'search ' , false );
309- if ( false !== $ search && '' !== $ search ) {
317+ /** @var array<string, bool|string> $typed_assoc_args */
318+ $ search_val = Utils \get_flag_value ( $ typed_assoc_args , 'search ' , '' );
319+ $ search = is_string ( $ search_val ) ? $ search_val : '' ;
320+ if ( '' !== $ search ) {
310321 if ( ! $ focus ) {
311322 WP_CLI ::error ( '--search requires --all or a specific hook. ' );
312323 }
@@ -375,11 +386,15 @@ public function hook( $args, $assoc_args ) {
375386 public function eval_ ( $ args , $ assoc_args ) {
376387 $ statement = $ args [0 ];
377388
378- $ order = Utils \get_flag_value ( $ assoc_args , 'order ' , 'ASC ' );
379- $ orderby = Utils \get_flag_value ( $ assoc_args , 'orderby ' , null );
389+ /** @var array<string, bool|string> $typed_assoc_args */
390+ $ typed_assoc_args = self ::get_typed_assoc_args ( $ assoc_args );
391+ $ order_val = Utils \get_flag_value ( $ typed_assoc_args , 'order ' , 'ASC ' );
392+ $ order = is_string ( $ order_val ) ? $ order_val : 'ASC ' ;
393+ $ orderby_val = Utils \get_flag_value ( $ typed_assoc_args , 'orderby ' , null );
394+ $ orderby = ( is_string ( $ orderby_val ) || is_null ( $ orderby_val ) ) ? $ orderby_val : null ;
380395
381396 self ::profile_eval_ish (
382- $ assoc_args ,
397+ $ typed_assoc_args ,
383398 function () use ( $ statement ) {
384399 eval ( $ statement ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- no other way around here
385400 },
@@ -449,15 +464,20 @@ public function eval_file( $args, $assoc_args ) {
449464
450465 $ file = $ args [0 ];
451466
452- $ order = Utils \get_flag_value ( $ assoc_args , 'order ' , 'ASC ' );
453- $ orderby = Utils \get_flag_value ( $ assoc_args , 'orderby ' , null );
467+ /** @var array<string, bool|string> $typed_assoc_args */
468+ /** @var array<string, bool|string> $typed_assoc_args */
469+ $ typed_assoc_args = self ::get_typed_assoc_args ( $ assoc_args );
470+ $ order_val = Utils \get_flag_value ( $ typed_assoc_args , 'order ' , 'ASC ' );
471+ $ order = is_string ( $ order_val ) ? $ order_val : 'ASC ' ;
472+ $ orderby_val = Utils \get_flag_value ( $ typed_assoc_args , 'orderby ' , null );
473+ $ orderby = ( is_string ( $ orderby_val ) || is_null ( $ orderby_val ) ) ? $ orderby_val : null ;
454474
455475 if ( ! file_exists ( $ file ) ) {
456476 WP_CLI ::error ( "' $ file' does not exist. " );
457477 }
458478
459479 self ::profile_eval_ish (
460- $ assoc_args ,
480+ $ typed_assoc_args ,
461481 function () use ( $ file ) {
462482 self ::include_file ( $ file );
463483 },
@@ -476,10 +496,12 @@ function () use ( $file ) {
476496 * @return void
477497 */
478498 private static function profile_eval_ish ( $ assoc_args , $ profile_callback , $ order = 'ASC ' , $ orderby = null ) {
479- $ hook = Utils \get_flag_value ( $ assoc_args , 'hook ' );
480- $ focus = false ;
481- $ type = false ;
482- $ fields = array ();
499+ /** @var array<string, bool|string> $typed_assoc_args */
500+ $ typed_assoc_args = self ::get_typed_assoc_args ( $ assoc_args );
501+ $ hook = Utils \get_flag_value ( $ typed_assoc_args , 'hook ' );
502+ $ focus = false ;
503+ $ type = false ;
504+ $ fields = array ();
483505 if ( $ hook ) {
484506 $ type = 'hook ' ;
485507 if ( true !== $ hook ) {
@@ -515,7 +537,7 @@ private static function profile_eval_ish( $assoc_args, $profile_callback, $order
515537 'request_count ' ,
516538 )
517539 );
518- $ formatter = new Formatter ( $ assoc_args , $ fields );
540+ $ formatter = new Formatter ( $ typed_assoc_args , $ fields );
519541 $ formatter ->display_items ( $ loggers , false , $ order , $ orderby );
520542 }
521543
@@ -586,4 +608,20 @@ function ( $logger ) use ( $pattern ) {
586608 }
587609 );
588610 }
611+
612+ /**
613+ * Get typed assoc args for get_flag_value.
614+ *
615+ * @param array<string, mixed> $assoc_args
616+ * @return array<string, bool|string>
617+ */
618+ private static function get_typed_assoc_args ( $ assoc_args ) {
619+ $ typed = array ();
620+ foreach ( $ assoc_args as $ k => $ v ) {
621+ if ( is_bool ( $ v ) || is_string ( $ v ) ) {
622+ $ typed [ $ k ] = $ v ;
623+ }
624+ }
625+ return $ typed ;
626+ }
589627}
0 commit comments