Skip to content

Commit 2e49453

Browse files
Copilotswissspidy
andcommitted
Refactor: Extract helper method and version check callback
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent bfdbe14 commit 2e49453

2 files changed

Lines changed: 25 additions & 36 deletions

File tree

‎entity-command.php‎

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,36 +113,14 @@
113113
WP_CLI::add_command( 'font', 'Font_Namespace' );
114114
}
115115

116-
WP_CLI::add_command(
117-
'font collection',
118-
'Font_Collection_Command',
119-
array(
120-
'before_invoke' => function () {
121-
if ( Utils\wp_version_compare( '6.5', '<' ) ) {
122-
WP_CLI::error( 'Requires WordPress 6.5 or greater.' );
123-
}
124-
},
125-
)
126-
);
127-
WP_CLI::add_command(
128-
'font family',
129-
'Font_Family_Command',
130-
array(
131-
'before_invoke' => function () {
132-
if ( Utils\wp_version_compare( '6.5', '<' ) ) {
133-
WP_CLI::error( 'Requires WordPress 6.5 or greater.' );
134-
}
135-
},
136-
)
137-
);
138-
WP_CLI::add_command(
139-
'font face',
140-
'Font_Face_Command',
141-
array(
142-
'before_invoke' => function () {
143-
if ( Utils\wp_version_compare( '6.5', '<' ) ) {
144-
WP_CLI::error( 'Requires WordPress 6.5 or greater.' );
145-
}
146-
},
147-
)
116+
$wpcli_entity_font_version_check = array(
117+
'before_invoke' => function () {
118+
if ( Utils\wp_version_compare( '6.5', '<' ) ) {
119+
WP_CLI::error( 'Requires WordPress 6.5 or greater.' );
120+
}
121+
},
148122
);
123+
124+
WP_CLI::add_command( 'font collection', 'Font_Collection_Command', $wpcli_entity_font_version_check );
125+
WP_CLI::add_command( 'font family', 'Font_Family_Command', $wpcli_entity_font_version_check );
126+
WP_CLI::add_command( 'font face', 'Font_Face_Command', $wpcli_entity_font_version_check );

‎src/Font_Collection_Command.php‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ class Font_Collection_Command extends WP_CLI_Command {
3838
'description',
3939
);
4040

41+
/**
42+
* Gets a safe value from collection data array.
43+
*
44+
* @param mixed $data Collection data.
45+
* @param string $key Key to retrieve.
46+
* @return string Value from data or empty string.
47+
*/
48+
private function get_safe_value( $data, $key ) {
49+
return is_array( $data ) && isset( $data[ $key ] ) ? $data[ $key ] : '';
50+
}
51+
4152
/**
4253
* Lists registered font collections.
4354
*
@@ -94,8 +105,8 @@ public function list_( $args, $assoc_args ) {
94105
$data = $collection->get_data();
95106
$items[] = array(
96107
'slug' => $collection->slug,
97-
'name' => is_array( $data ) && isset( $data['name'] ) ? $data['name'] : '',
98-
'description' => is_array( $data ) && isset( $data['description'] ) ? $data['description'] : '',
108+
'name' => $this->get_safe_value( $data, 'name' ),
109+
'description' => $this->get_safe_value( $data, 'description' ),
99110
);
100111
}
101112

@@ -164,8 +175,8 @@ public function get( $args, $assoc_args ) {
164175

165176
$data = array(
166177
'slug' => $collection->slug,
167-
'name' => is_array( $collection_data ) && isset( $collection_data['name'] ) ? $collection_data['name'] : '',
168-
'description' => is_array( $collection_data ) && isset( $collection_data['description'] ) ? $collection_data['description'] : '',
178+
'name' => $this->get_safe_value( $collection_data, 'name' ),
179+
'description' => $this->get_safe_value( $collection_data, 'description' ),
169180
);
170181

171182
$formatter = $this->get_formatter( $assoc_args );

0 commit comments

Comments
 (0)