Skip to content

Commit

Permalink
Use short array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Mar 25, 2021
1 parent 46de1e4 commit 82a4da3
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions src/Scaffold_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function post_type( $args, $assoc_args ) {
WP_CLI::error( 'Post type slugs cannot exceed 20 characters in length.' );
}

$defaults = array(
$defaults = [
'textdomain' => '',
'dashicon' => 'admin-post',
);
];

$templates = array(
$templates = [
'post_type.mustache',
'post_type_extended.mustache',
);
];

$this->scaffold( $args[0], $assoc_args, $defaults, '/post-types/', $templates );
}
Expand Down Expand Up @@ -124,32 +124,32 @@ public function post_type( $args, $assoc_args ) {
* @alias tax
*/
public function taxonomy( $args, $assoc_args ) {
$defaults = array(
$defaults = [
'textdomain' => '',
'post_types' => "'post'",
);
];

if ( isset( $assoc_args['post_types'] ) ) {
$assoc_args['post_types'] = $this->quote_comma_list_elements( $assoc_args['post_types'] );
}

$templates = array(
$templates = [
'taxonomy.mustache',
'taxonomy_extended.mustache',
);
];

$this->scaffold( $args[0], $assoc_args, $defaults, '/taxonomies/', $templates );
}

private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates ) {
$wp_filesystem = $this->init_wp_filesystem();

$control_defaults = array(
$control_defaults = [
'label' => preg_replace( '/_|-/', ' ', strtolower( $slug ) ),
'theme' => false,
'plugin' => false,
'raw' => false,
);
];
$control_args = $this->extract_args( $assoc_args, $control_defaults );

$vars = $this->extract_args( $assoc_args, $defaults );
Expand Down Expand Up @@ -190,7 +190,7 @@ private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates )
$filename = "{$path}{$slug}.php";

$force = Utils\get_flag_value( $assoc_args, 'force' );
$files_written = $this->create_files( array( $filename => $final_output ), $force );
$files_written = $this->create_files( [ $filename => $final_output ], $force );
$skip_message = "Skipped creating '{$filename}'.";
$success_message = "Created '{$filename}'.";
$this->log_whether_files_written( $files_written, $skip_message, $success_message );
Expand Down Expand Up @@ -269,10 +269,10 @@ public function block( $args, $assoc_args ) {
WP_CLI::error( 'Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.' );
}

$defaults = array(
$defaults = [
'title' => str_replace( '-', ' ', $slug ),
'category' => 'widgets',
);
];
$data = $this->extract_args( $assoc_args, $defaults );

$data['slug'] = $slug;
Expand All @@ -284,11 +284,11 @@ public function block( $args, $assoc_args ) {
$data['dashicon'] = $dashicon;
}

$control_defaults = array(
$control_defaults = [
'force' => false,
'plugin' => false,
'theme' => false,
);
];
$control_args = $this->extract_args( $assoc_args, $control_defaults );

if ( isset( $control_args['plugin'] ) ) {
Expand All @@ -307,12 +307,12 @@ public function block( $args, $assoc_args ) {
WP_CLI::error( 'No plugin or theme selected.' );
}

$files_to_create = array(
$files_to_create = [
"{$block_dir}/{$slug}.php" => self::mustache_render( 'block-php.mustache', $data ),
"{$block_dir}/{$slug}/index.js" => self::mustache_render( 'block-index-js.mustache', $data ),
"{$block_dir}/{$slug}/editor.css" => self::mustache_render( 'block-editor-css.mustache', $data ),
"{$block_dir}/{$slug}/style.css" => self::mustache_render( 'block-style-css.mustache', $data ),
);
];
$files_written = $this->create_files( $files_to_create, $control_args['force'] );
$skip_message = 'All block files were skipped.';
$success_message = "Created block '{$data['title_ucfirst']}'.";
Expand Down Expand Up @@ -372,11 +372,11 @@ public function underscores( $args, $assoc_args ) {
WP_CLI::error( 'Invalid theme slug specified. Theme slugs can only contain letters, numbers, underscores and hyphens, and can only start with a letter or underscore.' );
}

$defaults = array(
$defaults = [
'theme_name' => ucfirst( $theme_slug ),
'author' => 'Me',
'author_uri' => '',
);
];
$data = wp_parse_args( $assoc_args, $defaults );

$_s_theme_path = "$theme_path/$data[theme_name]";
Expand All @@ -395,7 +395,7 @@ public function underscores( $args, $assoc_args ) {

$theme_description = "Custom theme: {$data['theme_name']}, developed by {$data['author']}";

$body = array();
$body = [];
$body['underscoresme_name'] = $data['theme_name'];
$body['underscoresme_slug'] = $theme_slug;
$body['underscoresme_author'] = $data['author'];
Expand All @@ -412,12 +412,12 @@ public function underscores( $args, $assoc_args ) {
}

$tmpfname = wp_tempnam( $url );
$post_args = array(
$post_args = [
'timeout' => $timeout,
'body' => $body,
'stream' => true,
'filename' => $tmpfname,
);
];

$response = wp_remote_post( $url, $post_args );

Expand Down Expand Up @@ -449,19 +449,19 @@ public function underscores( $args, $assoc_args ) {
unlink( $tmpfname );

if ( true === $unzip_result ) {
$files_to_create = array(
$files_to_create = [
"{$theme_path}/{$theme_slug}/.editorconfig" => file_get_contents( self::get_template_path( '.editorconfig' ) ),
);
];
$this->create_files( $files_to_create, false );
WP_CLI::success( "Created theme '{$data['theme_name']}'." );
} else {
WP_CLI::error( "Could not decompress your theme files ('{$tmpfname}') at '{$theme_path}': {$unzip_result->get_error_message()}" );
}

if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
WP_CLI::run_command( array( 'theme', 'activate', $theme_slug ) );
WP_CLI::run_command( [ 'theme', 'activate', $theme_slug ] );
} elseif ( Utils\get_flag_value( $assoc_args, 'enable-network' ) ) {
WP_CLI::run_command( array( 'theme', 'enable', $theme_slug ), array( 'network' => true ) );
WP_CLI::run_command( [ 'theme', 'enable', $theme_slug ], [ 'network' => true ] );
}
}

Expand Down Expand Up @@ -510,20 +510,20 @@ public function underscores( $args, $assoc_args ) {
public function child_theme( $args, $assoc_args ) {
$theme_slug = $args[0];

if ( in_array( $theme_slug, array( '.', '..' ), true ) ) {
if ( in_array( $theme_slug, [ '.', '..' ], true ) ) {
WP_CLI::error( "Invalid theme slug specified. The slug cannot be '.' or '..'." );
}

$defaults = array(
$defaults = [
'theme_name' => ucfirst( $theme_slug ),
'author' => 'Me',
'author_uri' => '',
'theme_uri' => '',
);
];

$data = wp_parse_args( $assoc_args, $defaults );
$data['slug'] = $theme_slug;
$data['parent_theme_function_safe'] = str_replace( array( ' ', '-' ), '_', $data['parent_theme'] );
$data['parent_theme_function_safe'] = str_replace( [ ' ', '-' ], '_', $data['parent_theme'] );
$data['description'] = ucfirst( $data['parent_theme'] ) . ' child theme.';

$theme_dir = WP_CONTENT_DIR . "/themes/{$theme_slug}";
Expand All @@ -538,21 +538,21 @@ public function child_theme( $args, $assoc_args ) {

$this->maybe_create_themes_dir();

$files_to_create = array(
$files_to_create = [
$theme_style_path => self::mustache_render( 'child_theme.mustache', $data ),
$theme_functions_path => self::mustache_render( 'child_theme_functions.mustache', $data ),
"{$theme_dir}/.editorconfig" => file_get_contents( self::get_template_path( '.editorconfig' ) ),
);
];
$force = Utils\get_flag_value( $assoc_args, 'force' );
$files_written = $this->create_files( $files_to_create, $force );
$skip_message = 'All theme files were skipped.';
$success_message = "Created '{$theme_dir}'.";
$this->log_whether_files_written( $files_written, $skip_message, $success_message );

if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
WP_CLI::run_command( array( 'theme', 'activate', $theme_slug ) );
WP_CLI::run_command( [ 'theme', 'activate', $theme_slug ] );
} elseif ( Utils\get_flag_value( $assoc_args, 'enable-network' ) ) {
WP_CLI::run_command( array( 'theme', 'enable', $theme_slug ), array( 'network' => true ) );
WP_CLI::run_command( [ 'theme', 'enable', $theme_slug ], [ 'network' => true ] );
}
}

Expand Down Expand Up @@ -660,11 +660,11 @@ public function plugin( $args, $assoc_args ) {
$plugin_name = ucwords( str_replace( '-', ' ', $plugin_slug ) );
$plugin_package = str_replace( ' ', '_', $plugin_name );

if ( in_array( $plugin_slug, array( '.', '..' ), true ) ) {
if ( in_array( $plugin_slug, [ '.', '..' ], true ) ) {
WP_CLI::error( "Invalid plugin slug specified. The slug cannot be '.' or '..'." );
}

$defaults = array(
$defaults = [
'plugin_slug' => $plugin_slug,
'plugin_name' => $plugin_name,
'plugin_package' => $plugin_package,
Expand All @@ -673,7 +673,7 @@ public function plugin( $args, $assoc_args ) {
'plugin_author_uri' => 'YOUR SITE HERE',
'plugin_uri' => 'PLUGIN SITE HERE',
'plugin_tested_up_to' => get_bloginfo( 'version' ),
);
];
$data = wp_parse_args( $assoc_args, $defaults );

$data['textdomain'] = $plugin_slug;
Expand All @@ -696,15 +696,15 @@ public function plugin( $args, $assoc_args ) {
$plugin_path = "{$plugin_dir}/{$plugin_slug}.php";
$plugin_readme_path = "{$plugin_dir}/readme.txt";

$files_to_create = array(
$files_to_create = [
$plugin_path => self::mustache_render( 'plugin.mustache', $data ),
$plugin_readme_path => self::mustache_render( 'plugin-readme.mustache', $data ),
"{$plugin_dir}/package.json" => self::mustache_render( 'plugin-packages.mustache', $data ),
"{$plugin_dir}/Gruntfile.js" => self::mustache_render( 'plugin-gruntfile.mustache', $data ),
"{$plugin_dir}/.gitignore" => self::mustache_render( 'plugin-gitignore.mustache', $data ),
"{$plugin_dir}/.distignore" => self::mustache_render( 'plugin-distignore.mustache', $data ),
"{$plugin_dir}/.editorconfig" => file_get_contents( self::get_template_path( '.editorconfig' ) ),
);
];
$force = Utils\get_flag_value( $assoc_args, 'force' );
$files_written = $this->create_files( $files_to_create, $force );

Expand All @@ -713,18 +713,18 @@ public function plugin( $args, $assoc_args ) {
$this->log_whether_files_written( $files_written, $skip_message, $success_message );

if ( ! Utils\get_flag_value( $assoc_args, 'skip-tests' ) ) {
$command_args = array(
$command_args = [
'dir' => $plugin_dir,
'ci' => empty( $assoc_args['ci'] ) ? '' : $assoc_args['ci'],
'force' => $force,
);
WP_CLI::run_command( array( 'scaffold', 'plugin-tests', $plugin_slug ), $command_args );
];
WP_CLI::run_command( [ 'scaffold', 'plugin-tests', $plugin_slug ], $command_args );
}

if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
WP_CLI::run_command( array( 'plugin', 'activate', $plugin_slug ) );
WP_CLI::run_command( [ 'plugin', 'activate', $plugin_slug ] );
} elseif ( Utils\get_flag_value( $assoc_args, 'activate-network' ) ) {
WP_CLI::run_command( array( 'plugin', 'activate', $plugin_slug ), array( 'network' => true ) );
WP_CLI::run_command( [ 'plugin', 'activate', $plugin_slug ], [ 'network' => true ] );
}
}

Expand Down Expand Up @@ -839,7 +839,7 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {

if ( ! empty( $args[0] ) ) {
$slug = $args[0];
if ( in_array( $slug, array( '.', '..' ), true ) ) {
if ( in_array( $slug, [ '.', '..' ], true ) ) {
WP_CLI::error( "Invalid {$type} slug specified. The slug cannot be '.' or '..'." );
}
if ( 'theme' === $type ) {
Expand Down Expand Up @@ -885,7 +885,7 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
$wp_filesystem->mkdir( $tests_dir );
$wp_filesystem->mkdir( $bin_dir );

$wp_versions_to_test = array();
$wp_versions_to_test = [];
// Parse plugin readme.txt
if ( file_exists( "{$target_dir}/readme.txt" ) ) {
$readme_content = file_get_contents( "{$target_dir}/readme.txt" );
Expand All @@ -898,16 +898,16 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
$wp_versions_to_test[] = 'latest';
$wp_versions_to_test[] = 'trunk';

$template_data = array(
$template_data = [
"{$type}_slug" => $slug,
"{$type}_package" => $package,
);
];

$force = Utils\get_flag_value( $assoc_args, 'force' );
$files_to_create = array(
$files_to_create = [
"{$tests_dir}/bootstrap.php" => self::mustache_render( "{$type}-bootstrap.mustache", $template_data ),
"{$tests_dir}/test-sample.php" => self::mustache_render( "{$type}-test-sample.mustache", $template_data ),
);
];
if ( 'travis' === $assoc_args['ci'] ) {
$files_to_create[ "{$target_dir}/.travis.yml" ] = self::mustache_render( 'plugin-travis.mustache', compact( 'wp_versions_to_test' ) );
} elseif ( 'circle' === $assoc_args['ci'] ) {
Expand All @@ -920,11 +920,11 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {

$files_written = $this->create_files( $files_to_create, $force );

$to_copy = array(
$to_copy = [
'install-wp-tests.sh' => $bin_dir,
'phpunit.xml.dist' => $target_dir,
'.phpcs.xml.dist' => $target_dir,
);
];

foreach ( $to_copy as $file => $dir ) {
$file_name = "{$dir}/{$file}";
Expand Down Expand Up @@ -973,7 +973,7 @@ private function check_target_directory( $type, $target_dir ) {

protected function create_files( $files_and_contents, $force ) {
$wp_filesystem = $this->init_wp_filesystem();
$wrote_files = array();
$wrote_files = [];

foreach ( $files_and_contents as $filename => $contents ) {
$should_write_file = $this->prompt_if_files_will_be_overwritten( $filename, $force );
Expand Down Expand Up @@ -1007,7 +1007,7 @@ protected function prompt_if_files_will_be_overwritten( $filename, $force ) {
$default = false,
$marker = '[s/r]: '
);
} while ( ! in_array( $answer, array( 's', 'r' ), true ) );
} while ( ! in_array( $answer, [ 's', 'r' ], true ) );
$should_write_file = 'r' === $answer;
}

Expand Down Expand Up @@ -1081,7 +1081,7 @@ private function pluralize( $word ) {
}

protected function extract_args( $assoc_args, $defaults ) {
$out = array();
$out = [];

foreach ( $defaults as $key => $value ) {
$out[ $key ] = Utils\get_flag_value( $assoc_args, $key, $value );
Expand Down Expand Up @@ -1130,7 +1130,7 @@ protected function init_wp_filesystem() {
/**
* Localizes the template path.
*/
private static function mustache_render( $template, $data = array() ) {
private static function mustache_render( $template, $data = [] ) {
return Utils\mustache_render( dirname( dirname( __FILE__ ) ) . "/templates/{$template}", $data );
}

Expand Down Expand Up @@ -1167,7 +1167,7 @@ private static function canonicalize_path( $path ) {
$path .= '/';
}

$output = array();
$output = [];

foreach ( explode( '/', $path ) as $segment ) {
if ( '..' === $segment ) {
Expand Down

0 comments on commit 82a4da3

Please sign in to comment.