Skip to content

Commit d3aa0c4

Browse files
Copilotswissspidy
andcommitted
Refactor: Extract option parsing logic into helper method
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent aaace25 commit d3aa0c4

1 file changed

Lines changed: 40 additions & 73 deletions

File tree

src/Config_Command.php

Lines changed: 40 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -673,31 +673,7 @@ public function set( $args, $assoc_args ) {
673673
*/
674674
$type = Utils\get_flag_value( $assoc_args, 'type' );
675675

676-
$options = [];
677-
678-
$option_flags = [
679-
'raw' => false,
680-
'add' => true,
681-
'anchor' => null,
682-
'placement' => null,
683-
'separator' => null,
684-
];
685-
686-
foreach ( $option_flags as $option => $default ) {
687-
/**
688-
* @var string|null $option_value
689-
*/
690-
$option_value = Utils\get_flag_value( $assoc_args, $option, $default );
691-
if ( null !== $option_value ) {
692-
/**
693-
* @var array<string, string> $options
694-
*/
695-
$options[ $option ] = $option_value;
696-
if ( 'separator' === $option ) {
697-
$options['separator'] = $this->parse_separator( $options['separator'] );
698-
}
699-
}
700-
}
676+
$options = $this->parse_config_transformer_options( $assoc_args, [ 'add' => true ] );
701677

702678
$adding = false;
703679
try {
@@ -811,30 +787,7 @@ public function add( $args, $assoc_args ) {
811787
*/
812788
$type = Utils\get_flag_value( $assoc_args, 'type', 'constant' );
813789

814-
$options = [];
815-
816-
$option_flags = [
817-
'raw' => false,
818-
'anchor' => null,
819-
'placement' => null,
820-
'separator' => null,
821-
];
822-
823-
foreach ( $option_flags as $option => $default ) {
824-
/**
825-
* @var string|null $option_value
826-
*/
827-
$option_value = Utils\get_flag_value( $assoc_args, $option, $default );
828-
if ( null !== $option_value ) {
829-
/**
830-
* @var array<string, string> $options
831-
*/
832-
$options[ $option ] = $option_value;
833-
if ( 'separator' === $option ) {
834-
$options['separator'] = $this->parse_separator( $options['separator'] );
835-
}
836-
}
837-
}
790+
$options = $this->parse_config_transformer_options( $assoc_args );
838791

839792
// add command always adds, so we set the 'add' option to true
840793
$options['add'] = true;
@@ -926,30 +879,7 @@ public function update( $args, $assoc_args ) {
926879
*/
927880
$type = Utils\get_flag_value( $assoc_args, 'type', 'all' );
928881

929-
$options = [];
930-
931-
$option_flags = [
932-
'raw' => false,
933-
'anchor' => null,
934-
'placement' => null,
935-
'separator' => null,
936-
];
937-
938-
foreach ( $option_flags as $option => $default ) {
939-
/**
940-
* @var string|null $option_value
941-
*/
942-
$option_value = Utils\get_flag_value( $assoc_args, $option, $default );
943-
if ( null !== $option_value ) {
944-
/**
945-
* @var array<string, string> $options
946-
*/
947-
$options[ $option ] = $option_value;
948-
if ( 'separator' === $option ) {
949-
$options['separator'] = $this->parse_separator( $options['separator'] );
950-
}
951-
}
952-
}
882+
$options = $this->parse_config_transformer_options( $assoc_args );
953883

954884
// update command always adds if not exists, so we set the 'add' option to true
955885
$options['add'] = true;
@@ -1428,6 +1358,43 @@ private function parse_separator( $separator ) {
14281358
return $separator;
14291359
}
14301360

1361+
/**
1362+
* Parses and returns options for config transformer operations.
1363+
*
1364+
* Extracts common flags (raw, anchor, placement, separator) from assoc_args
1365+
* and builds an options array for use with WPConfigTransformer.
1366+
*
1367+
* @param array $assoc_args Associative arguments from the command.
1368+
* @param array $defaults Default values for the options.
1369+
*
1370+
* @return array<string, string|bool> Parsed options array.
1371+
*/
1372+
private function parse_config_transformer_options( $assoc_args, $defaults = [] ) {
1373+
$options = [];
1374+
1375+
$option_flags = array_merge(
1376+
[
1377+
'raw' => false,
1378+
'anchor' => null,
1379+
'placement' => null,
1380+
'separator' => null,
1381+
],
1382+
$defaults
1383+
);
1384+
1385+
foreach ( $option_flags as $option => $default ) {
1386+
$option_value = Utils\get_flag_value( $assoc_args, $option, $default );
1387+
if ( null !== $option_value ) {
1388+
$options[ $option ] = $option_value;
1389+
if ( 'separator' === $option ) {
1390+
$options['separator'] = $this->parse_separator( $options['separator'] );
1391+
}
1392+
}
1393+
}
1394+
1395+
return $options;
1396+
}
1397+
14311398
/**
14321399
* Gets the value of a specific constant or variable defined in wp-config.php file.
14331400
*

0 commit comments

Comments
 (0)