Skip to content

Commit 33d7538

Browse files
Copilotswissspidy
andcommitted
Refactor to use closure pattern for improved readability
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent a929acb commit 33d7538

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/EvalFile_Command.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function __invoke( $args, $assoc_args ) {
5858
WP_CLI::error( '"-" and "--use-include" parameters cannot be used at the same time' );
5959
}
6060

61+
$execute_closure = function () use ( $file, $args, $use_include ) {
62+
self::execute_eval( $file, $args, $use_include );
63+
};
64+
6165
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
6266
$skip_wordpress = Utils\get_flag_value( $assoc_args, 'skip-wordpress' );
6367

@@ -66,20 +70,15 @@ public function __invoke( $args, $assoc_args ) {
6670
}
6771

6872
if ( $hook ) {
69-
WP_CLI::add_wp_hook(
70-
$hook,
71-
function () use ( $file, $args, $use_include ) {
72-
self::execute_eval( $file, $args, $use_include );
73-
}
74-
);
73+
WP_CLI::add_wp_hook( $hook, $execute_closure );
7574
}
7675

7776
if ( null === $skip_wordpress ) {
7877
WP_CLI::get_runner()->load_wordpress();
7978
}
8079

8180
if ( ! $hook ) {
82-
self::execute_eval( $file, $args, $use_include );
81+
$execute_closure();
8382
}
8483
}
8584

src/Eval_Command.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class Eval_Command extends WP_CLI_Command {
3939
*/
4040
public function __invoke( $args, $assoc_args ) {
4141

42+
$execute_closure = function () use ( $args ) {
43+
eval( $args[0] );
44+
};
45+
4246
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
4347
$skip_wordpress = Utils\get_flag_value( $assoc_args, 'skip-wordpress' );
4448

@@ -47,21 +51,15 @@ public function __invoke( $args, $assoc_args ) {
4751
}
4852

4953
if ( $hook ) {
50-
$code = $args[0];
51-
WP_CLI::add_wp_hook(
52-
$hook,
53-
function () use ( $code ) {
54-
eval( $code );
55-
}
56-
);
54+
WP_CLI::add_wp_hook( $hook, $execute_closure );
5755
}
5856

5957
if ( null === $skip_wordpress ) {
6058
WP_CLI::get_runner()->load_wordpress();
6159
}
6260

6361
if ( ! $hook ) {
64-
eval( $args[0] );
62+
$execute_closure();
6563
}
6664
}
6765
}

0 commit comments

Comments
 (0)