From f1279bef173dc047bf9aa87f840bbd73c1cf2e6e Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Thu, 18 Apr 2019 12:56:51 +0530
Subject: [PATCH 1/6] PHPCS: Add new ruleset to the project Update .distignore
 and .gitignore with phpcs/phpunit config files Update wp-cli-tests to 2.1

---
 .distignore    |  2 ++
 .gitignore     |  3 +++
 composer.json  |  2 +-
 phpcs.xml.dist | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 phpcs.xml.dist

diff --git a/.distignore b/.distignore
index b964b40c7..95b52fb02 100644
--- a/.distignore
+++ b/.distignore
@@ -6,6 +6,8 @@
 .travis.yml
 behat.yml
 circle.yml
+phpcs.xml.dist
+phpunit.xml.dist
 bin/
 features/
 utils/
diff --git a/.gitignore b/.gitignore
index ff4941991..bcf211b32 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,6 @@ vendor/
 *.tar.gz
 composer.lock
 *.log
+phpunit.xml
+phpcs.xml
+.phpcs.xml
diff --git a/composer.json b/composer.json
index a9bdba1cb..317ed17d9 100644
--- a/composer.json
+++ b/composer.json
@@ -16,7 +16,7 @@
     },
     "require-dev": {
         "wp-cli/entity-command": "^1.3 || ^2",
-        "wp-cli/wp-cli-tests": "^2.0.7"
+        "wp-cli/wp-cli-tests": "^2.1"
     },
     "config": {
         "process-timeout": 7200,
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
new file mode 100644
index 000000000..646cefcd2
--- /dev/null
+++ b/phpcs.xml.dist
@@ -0,0 +1,64 @@
+<?xml version="1.0"?>
+<ruleset name="WP-CLI-media">
+	<description>Custom ruleset for WP-CLI media-command</description>
+
+	<!--
+	#############################################################################
+	COMMAND LINE ARGUMENTS
+	For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
+	For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
+	#############################################################################
+	-->
+
+	<!-- What to scan. -->
+	<file>.</file>
+
+	<!-- Ignoring select files/folders.
+		 https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
+	<exclude-pattern>*/node_modules/*</exclude-pattern>
+	<exclude-pattern>*/vendor/*</exclude-pattern>
+
+	<!-- Show progress. -->
+	<arg value="p"/>
+
+	<!-- Strip the filepaths down to the relevant bit. -->
+	<arg name="basepath" value="./"/>
+
+	<!-- Check up to 8 files simultaneously. -->
+	<arg name="parallel" value="8"/>
+
+	<!--
+	#############################################################################
+	USE THE WPCliCS RULESET
+	#############################################################################
+	-->
+
+	<rule ref="WPCliCS"/>
+
+	<!--
+	#############################################################################
+	PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
+	#############################################################################
+	-->
+
+	<!-- For help understanding the `testVersion` configuration setting:
+		 https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
+	<config name="testVersion" value="5.4-"/>
+
+	<!-- Verify that everything in the global namespace is either namespaced or prefixed.
+		 See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
+	<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
+		<properties>
+			<property name="prefixes" type="array">
+				<element value="WP_CLI\Media"/><!-- Namespaces. -->
+				<element value="wpcli_media"/><!-- Global variables and such. -->
+			</property>
+		</properties>
+	</rule>
+
+	<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
+	<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
+		<exclude-pattern>*/src/Media_Command\.php$</exclude-pattern>
+	</rule>
+
+</ruleset>

From aef39206f4d471471429c148a09669715fe82569 Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Thu, 18 Apr 2019 12:59:55 +0530
Subject: [PATCH 2/6] PHPCS: Fix up errors in media-command.php

---
 media-command.php | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/media-command.php b/media-command.php
index f17d8aae5..7574e794a 100644
--- a/media-command.php
+++ b/media-command.php
@@ -4,16 +4,22 @@
 	return;
 }
 
-$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
-if ( file_exists( $autoload ) ) {
-	require_once $autoload;
+$wpcli_media_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
+if ( file_exists( $wpcli_media_autoloader ) ) {
+	require_once $wpcli_media_autoloader;
 }
 
-WP_CLI::add_command( 'media', 'Media_Command', array(
-	'before_invoke' => function () {
-		if ( !wp_image_editor_supports() ) {
-			WP_CLI::error( 'No support for generating images found. ' .
-				'Please install the Imagick or GD PHP extensions.' );
-		}
-	}
-) );
+WP_CLI::add_command(
+	'media',
+	'Media_Command',
+	array(
+		'before_invoke' => function () {
+			if ( ! wp_image_editor_supports() ) {
+				WP_CLI::error(
+					'No support for generating images found. ' .
+					'Please install the Imagick or GD PHP extensions.'
+				);
+			}
+		},
+	)
+);

From effabc1640974c53e0f21ad55abaeac246da8343 Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Thu, 18 Apr 2019 16:52:47 +0530
Subject: [PATCH 3/6] PHPCS: Fix up errors in src/Media_Command.php

---
 src/Media_Command.php | 278 +++++++++++++++++++++++++-----------------
 1 file changed, 163 insertions(+), 115 deletions(-)

diff --git a/src/Media_Command.php b/src/Media_Command.php
index aca43c28a..ecf84ace4 100644
--- a/src/Media_Command.php
+++ b/src/Media_Command.php
@@ -99,10 +99,13 @@ class Media_Command extends WP_CLI_Command {
 	 *     3/3 Regenerated "large" thumbnail for "Sunburst Over River" (ID 756).
 	 *     Success: Regenerated 3 of 3 images.
 	 */
-	function regenerate( $args, $assoc_args = array() ) {
-		$assoc_args = wp_parse_args( $assoc_args, array(
-			'image_size' => '',
-		) );
+	public function regenerate( $args, $assoc_args = array() ) {
+		$assoc_args = wp_parse_args(
+			$assoc_args,
+			array(
+				'image_size' => '',
+			)
+		);
 
 		$image_size = $assoc_args['image_size'];
 		if ( $image_size && ! in_array( $image_size, get_intermediate_image_sizes(), true ) ) {
@@ -117,7 +120,7 @@ function regenerate( $args, $assoc_args = array() ) {
 			}
 		}
 
-		$skip_delete = \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-delete' );
+		$skip_delete  = \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-delete' );
 		$only_missing = \WP_CLI\Utils\get_flag_value( $assoc_args, 'only-missing' );
 		if ( $only_missing ) {
 			$skip_delete = true;
@@ -128,31 +131,39 @@ function regenerate( $args, $assoc_args = array() ) {
 			$mime_types[] = 'application/pdf';
 		}
 		$query_args = array(
-			'post_type' => 'attachment',
-			'post__in' => $args,
+			'post_type'      => 'attachment',
+			'post__in'       => $args,
 			'post_mime_type' => $mime_types,
-			'post_status' => 'any',
-			'posts_per_page' => -1,
-			'fields' => 'ids'
+			'post_status'    => 'any',
+			'posts_per_page' => - 1,
+			'fields'         => 'ids',
 		);
 
 		$images = new WP_Query( $query_args );
 
 		$count = $images->post_count;
 
-		if ( !$count ) {
+		if ( ! $count ) {
 			WP_CLI::warning( 'No images found.' );
 			return;
 		}
 
-		WP_CLI::log( sprintf( 'Found %1$d %2$s to regenerate.', $count,
-			_n( 'image', 'images', $count ) ) );
+		WP_CLI::log(
+			sprintf(
+				'Found %1$d %2$s to regenerate.',
+				$count,
+				_n( 'image', 'images', $count )
+			)
+		);
 
 		if ( $image_size ) {
 			$image_size_filters = $this->add_image_size_filters( $image_size );
 		}
 
-		$number = $successes = $errors = $skips = 0;
+		$number    = 0;
+		$successes = 0;
+		$errors    = 0;
+		$skips     = 0;
 		foreach ( $images->posts as $id ) {
 			$number++;
 			if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
@@ -236,13 +247,16 @@ function regenerate( $args, $assoc_args = array() ) {
 	 *     $ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --porcelain | xargs -I {} wp post list --post__in={} --field=url --post_type=attachment
 	 *     http://wordpress-develop.dev/wp-header-logo/
 	 */
-	function import( $args, $assoc_args = array() ) {
-		$assoc_args = wp_parse_args( $assoc_args, array(
-			'title' => '',
-			'caption' => '',
-			'alt' => '',
-			'desc' => '',
-		) );
+	public function import( $args, $assoc_args = array() ) {
+		$assoc_args = wp_parse_args(
+			$assoc_args,
+			array(
+				'title'   => '',
+				'caption' => '',
+				'alt'     => '',
+				'desc'    => '',
+			)
+		);
 
 		// Assume the most generic term
 		$noun = 'item';
@@ -256,26 +270,28 @@ function import( $args, $assoc_args = array() ) {
 		}
 
 		if ( isset( $assoc_args['post_id'] ) ) {
-			if ( !get_post( $assoc_args['post_id'] ) ) {
-				WP_CLI::warning( "Invalid --post_id" );
+			if ( ! get_post( $assoc_args['post_id'] ) ) {
+				WP_CLI::warning( 'Invalid --post_id' );
 				$assoc_args['post_id'] = false;
 			}
 		} else {
 			$assoc_args['post_id'] = false;
 		}
 
-		$number = $successes = $errors = 0;
+		$number    = 0;
+		$successes = 0;
+		$errors    = 0;
 		foreach ( $args as $file ) {
 			$number++;
 			if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
 				Utils\wp_clear_object_cache();
 			}
-			$is_file_remote = parse_url( $file, PHP_URL_HOST );
-			$orig_filename = $file;
-			$file_time = '';
+			$is_file_remote = function_exists( 'wp_parse_url' ) ? wp_parse_url( $file, PHP_URL_HOST ) : parse_url( $file, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
+			$orig_filename  = $file;
+			$file_time      = '';
 
 			if ( empty( $is_file_remote ) ) {
-				if ( !file_exists( $file ) ) {
+				if ( ! file_exists( $file ) ) {
 					WP_CLI::warning( "Unable to import file '$file'. Reason: File doesn't exist." );
 					$errors++;
 					continue;
@@ -293,10 +309,13 @@ function import( $args, $assoc_args = array() ) {
 			} else {
 				$tempfile = download_url( $file );
 				if ( is_wp_error( $tempfile ) ) {
-					WP_CLI::warning( sprintf(
-						"Unable to import file '%s'. Reason: %s",
-						$file, implode( ', ', $tempfile->get_error_messages() )
-					) );
+					WP_CLI::warning(
+						sprintf(
+							"Unable to import file '%s'. Reason: %s",
+							$file,
+							implode( ', ', $tempfile->get_error_messages() )
+						)
+					);
 					$errors++;
 					continue;
 				}
@@ -305,19 +324,19 @@ function import( $args, $assoc_args = array() ) {
 
 			$file_array = array(
 				'tmp_name' => $tempfile,
-				'name' => $name,
+				'name'     => $name,
 			);
 
 			$post_array = array(
-				'post_title' => $assoc_args['title'],
+				'post_title'   => $assoc_args['title'],
 				'post_excerpt' => $assoc_args['caption'],
 				'post_content' => $assoc_args['desc'],
 			);
 
 			if ( ! empty( $file_time ) ) {
-				$post_array['post_date'] = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) );
-				$post_array['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $file_time );
-				$post_array['post_modified'] = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) );
+				$post_array['post_date']         = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) );
+				$post_array['post_date_gmt']     = gmdate( 'Y-m-d H:i:s', $file_time );
+				$post_array['post_modified']     = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) );
 				$post_array['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $file_time );
 			}
 
@@ -344,16 +363,19 @@ function import( $args, $assoc_args = array() ) {
 			}
 
 			if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
-				$wp_filetype = wp_check_filetype( $file, null );
+				$wp_filetype                  = wp_check_filetype( $file, null );
 				$post_array['post_mime_type'] = $wp_filetype['type'];
-				$post_array['post_status'] = 'inherit';
+				$post_array['post_status']    = 'inherit';
 
 				$success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] );
 				if ( is_wp_error( $success ) ) {
-					WP_CLI::warning( sprintf(
-						"Unable to insert file '%s'. Reason: %s",
-						$orig_filename, implode( ', ', $success->get_error_messages() )
-					) );
+					WP_CLI::warning(
+						sprintf(
+							"Unable to insert file '%s'. Reason: %s",
+							$orig_filename,
+							implode( ', ', $success->get_error_messages() )
+						)
+					);
 					$errors++;
 					continue;
 				}
@@ -362,10 +384,13 @@ function import( $args, $assoc_args = array() ) {
 				// Deletes the temporary file.
 				$success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array );
 				if ( is_wp_error( $success ) ) {
-					WP_CLI::warning( sprintf(
-						"Unable to import file '%s'. Reason: %s",
-						$orig_filename, implode( ', ', $success->get_error_messages() )
-					) );
+					WP_CLI::warning(
+						sprintf(
+							"Unable to import file '%s'. Reason: %s",
+							$orig_filename,
+							implode( ', ', $success->get_error_messages() )
+						)
+					);
 					$errors++;
 					continue;
 				}
@@ -384,17 +409,22 @@ function import( $args, $assoc_args = array() ) {
 			$attachment_success_text = '';
 			if ( $assoc_args['post_id'] ) {
 				$attachment_success_text = " and attached to post {$assoc_args['post_id']}";
-				if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) )
+				if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
 					$attachment_success_text .= ' as featured image';
+				}
 			}
 
 			if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
 				WP_CLI::line( $success );
 			} else {
-				WP_CLI::log( sprintf(
-					"Imported file '%s' as attachment ID %d%s.",
-					$orig_filename, $success, $attachment_success_text
-				) );
+				WP_CLI::log(
+					sprintf(
+						"Imported file '%s' as attachment ID %d%s.",
+						$orig_filename,
+						$success,
+						$attachment_success_text
+					)
+				);
 			}
 			$successes++;
 		}
@@ -458,74 +488,83 @@ public function image_size( $args, $assoc_args ) {
 
 		$soft_ratio_text = 'N/A';
 
-		$assoc_args = array_merge( array(
-			'fields'      => 'name,width,height,crop,ratio'
-		), $assoc_args );
+		$assoc_args = array_merge(
+			array(
+				'fields' => 'name,width,height,crop,ratio',
+			),
+			$assoc_args
+		);
 
 		$sizes = array(
 			array(
-				'name'      => 'large',
-				'width'     => intval( get_option( 'large_size_w' ) ),
-				'height'    => intval( get_option( 'large_size_h' ) ),
-				'crop'      => false !== get_option( 'large_crop' ) ? 'hard' : 'soft',
-				'ratio'     => false !== get_option( 'large_crop' ) ? $this->get_ratio( intval( get_option( 'large_size_w' ) ), intval( get_option( 'large_size_h' ) ) ) : $soft_ratio_text,
+				'name'   => 'large',
+				'width'  => intval( get_option( 'large_size_w' ) ),
+				'height' => intval( get_option( 'large_size_h' ) ),
+				'crop'   => false !== get_option( 'large_crop' ) ? 'hard' : 'soft',
+				'ratio'  => false !== get_option( 'large_crop' ) ? $this->get_ratio( intval( get_option( 'large_size_w' ) ), intval( get_option( 'large_size_h' ) ) ) : $soft_ratio_text,
 			),
 			array(
-				'name'      => 'medium_large',
-				'width'     => intval( get_option( 'medium_large_size_w' ) ),
-				'height'    => intval( get_option( 'medium_large_size_h' ) ),
-				'crop'      => false !== get_option( 'medium_large_crop' ) ? 'hard' : 'soft',
-				'ratio'     => false !== get_option( 'medium_large_crop' ) ? $this->get_ratio( intval( get_option( 'medium_large_size_w' ) ), intval( get_option( 'medium_large_size_h' ) ) ) : $soft_ratio_text,
+				'name'   => 'medium_large',
+				'width'  => intval( get_option( 'medium_large_size_w' ) ),
+				'height' => intval( get_option( 'medium_large_size_h' ) ),
+				'crop'   => false !== get_option( 'medium_large_crop' ) ? 'hard' : 'soft',
+				'ratio'  => false !== get_option( 'medium_large_crop' ) ? $this->get_ratio( intval( get_option( 'medium_large_size_w' ) ), intval( get_option( 'medium_large_size_h' ) ) ) : $soft_ratio_text,
 			),
 			array(
-				'name'      => 'medium',
-				'width'     => intval( get_option( 'medium_size_w' ) ),
-				'height'    => intval( get_option( 'medium_size_h' ) ),
-				'crop'      => false !== get_option( 'medium_crop' ) ? 'hard' : 'soft',
-				'ratio'     => false !== get_option( 'medium_crop' ) ? $this->get_ratio( intval( get_option( 'medium_size_w' ) ), intval( get_option( 'medium_size_h' ) ) ) : $soft_ratio_text,
+				'name'   => 'medium',
+				'width'  => intval( get_option( 'medium_size_w' ) ),
+				'height' => intval( get_option( 'medium_size_h' ) ),
+				'crop'   => false !== get_option( 'medium_crop' ) ? 'hard' : 'soft',
+				'ratio'  => false !== get_option( 'medium_crop' ) ? $this->get_ratio( intval( get_option( 'medium_size_w' ) ), intval( get_option( 'medium_size_h' ) ) ) : $soft_ratio_text,
 			),
 			array(
-				'name'      => 'thumbnail',
-				'width'     => intval( get_option( 'thumbnail_size_w' ) ),
-				'height'    => intval( get_option( 'thumbnail_size_h' ) ),
-				'crop'      => false !== get_option( 'thumbnail_crop' ) ? 'hard' : 'soft',
-				'ratio'     => false !== get_option( 'thumbnail_crop' ) ? $this->get_ratio( intval( get_option( 'thumbnail_size_w' ) ), intval( get_option( 'thumbnail_size_h' ) ) ) : $soft_ratio_text,
+				'name'   => 'thumbnail',
+				'width'  => intval( get_option( 'thumbnail_size_w' ) ),
+				'height' => intval( get_option( 'thumbnail_size_h' ) ),
+				'crop'   => false !== get_option( 'thumbnail_crop' ) ? 'hard' : 'soft',
+				'ratio'  => false !== get_option( 'thumbnail_crop' ) ? $this->get_ratio( intval( get_option( 'thumbnail_size_w' ) ), intval( get_option( 'thumbnail_size_h' ) ) ) : $soft_ratio_text,
 			),
 		);
 		if ( is_array( $_wp_additional_image_sizes ) ) {
-			foreach( $_wp_additional_image_sizes as $size => $size_args ) {
-				$crop = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
+			foreach ( $_wp_additional_image_sizes as $size => $size_args ) {
+				$crop    = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
 				$sizes[] = array(
-					'name'      => $size,
-					'width'     => $size_args['width'],
-					'height'    => $size_args['height'],
-					'crop'      => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
-					'ratio'     => empty( $crop ) || is_array( $size_args['crop'] ) ? $soft_ratio_text : $this->get_ratio( $size_args['width'], $size_args['height'] ),
+					'name'   => $size,
+					'width'  => $size_args['width'],
+					'height' => $size_args['height'],
+					'crop'   => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
+					'ratio'  => empty( $crop ) || is_array( $size_args['crop'] ) ? $soft_ratio_text : $this->get_ratio( $size_args['width'], $size_args['height'] ),
 				);
 			}
 		}
-		usort( $sizes, function( $a, $b ){
-			if ( $a['width'] == $b['width'] ) {
-				return 0;
+		usort(
+			$sizes,
+			function( $a, $b ) {
+				if ( $a['width'] === $b['width'] ) {
+					return 0;
+				}
+				return ( $a['width'] < $b['width'] ) ? 1 : -1;
 			}
-			return ( $a['width'] < $b['width'] ) ? 1 : -1;
-		});
-		array_unshift( $sizes, array(
-				'name'      => 'full',
-				'width'     => '',
-				'height'    => '',
-				'crop'      => 'N/A',
-				'ratio'     => $soft_ratio_text,
-		) );
+		);
+		array_unshift(
+			$sizes,
+			array(
+				'name'   => 'full',
+				'width'  => '',
+				'height' => '',
+				'crop'   => 'N/A',
+				'ratio'  => $soft_ratio_text,
+			)
+		);
 		WP_CLI\Utils\format_items( $assoc_args['format'], $sizes, explode( ',', $assoc_args['fields'] ) );
 	}
 
 	private function get_ratio( $width, $height ) {
-		if ( $height == 0 ) {
+		if ( 0 === $height ) {
 			return "0:{$width}";
 		}
 
-		if ( $width == 0 ) {
+		if ( 0 === $width ) {
 			return "{$height}:0";
 		}
 
@@ -536,8 +575,8 @@ private function get_ratio( $width, $height ) {
 		return "{$width_ratio}:{$height_ratio}";
 	}
 
-	private function gcd($num1,$num2) {
-		while (0 !== $num2) {
+	private function gcd( $num1, $num2 ) {
+		while ( 0 !== $num2 ) {
 			$t    = $num1 % $num2;
 			$num1 = $num2;
 			$num2 = $t;
@@ -547,14 +586,16 @@ private function gcd($num1,$num2) {
 
 	// wp_tempnam() inexplicably forces a .tmp extension, which spoils MIME type detection
 	private function make_copy( $path ) {
-		$dir = get_temp_dir();
+		$dir      = get_temp_dir();
 		$filename = Utils\basename( $path );
-		if ( empty( $filename ) )
+		if ( empty( $filename ) ) {
 			$filename = time();
+		}
 
 		$filename = $dir . wp_unique_filename( $dir, $filename );
-		if ( !copy( $path, $filename ) )
+		if ( ! copy( $path, $filename ) ) {
 			WP_CLI::error( "Could not create temporary file for $path." );
+		}
 
 		return $filename;
 	}
@@ -577,7 +618,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_
 
 		$fullsizepath = get_attached_file( $id );
 
-		if ( false === $fullsizepath || !file_exists( $fullsizepath ) ) {
+		if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) {
 			WP_CLI::warning( "Can't find $att_desc." );
 			$errors++;
 			return;
@@ -647,11 +688,13 @@ private function remove_old_images( $metadata, $fullsizepath, $image_size ) {
 		foreach ( $metadata['sizes'] as $size_info ) {
 			$intermediate_path = $dir_path . $size_info['file'];
 
-			if ( $intermediate_path === $fullsizepath )
+			if ( $intermediate_path === $fullsizepath ) {
 				continue;
+			}
 
-			if ( file_exists( $intermediate_path ) )
+			if ( file_exists( $intermediate_path ) ) {
 				unlink( $intermediate_path );
+			}
 		}
 	}
 
@@ -661,7 +704,7 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz
 		$skip_it = false;
 
 		// Note: zero-length string returned if no metadata, for instance if PDF or non-standard image (eg an SVG).
-		$metadata = wp_get_attachment_metadata($att_id);
+		$metadata = wp_get_attachment_metadata( $att_id );
 
 		$image_sizes = $this->get_intermediate_image_sizes_for_attachment( $fullsizepath, $is_pdf, $metadata );
 
@@ -715,11 +758,12 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz
 		$dir_path = dirname( $fullsizepath ) . '/';
 
 		// Check that the thumbnail files exist.
-		foreach( $metadata['sizes'] as $size_info ) {
+		foreach ( $metadata['sizes'] as $size_info ) {
 			$intermediate_path = $dir_path . $size_info['file'];
 
-			if ( $intermediate_path === $fullsizepath )
+			if ( $intermediate_path === $fullsizepath ) {
 				continue;
+			}
 
 			if ( ! file_exists( $intermediate_path ) ) {
 				return true;
@@ -752,7 +796,8 @@ private function get_intermediate_image_sizes_for_attachment( $fullsizepath, $is
 		if ( is_wp_error( $editor ) ) {
 			return $editor;
 		}
-		if ( is_wp_error( $result = $editor->load() ) ) {
+		$result = $editor->load();
+		if ( is_wp_error( $result ) ) {
 			unset( $editor );
 			return $result;
 		}
@@ -762,9 +807,13 @@ private function get_intermediate_image_sizes_for_attachment( $fullsizepath, $is
 		$sizes = array();
 		foreach ( $this->get_intermediate_sizes( $is_pdf, $metadata ) as $name => $size ) {
 			// Need to check destination and original width or height differ before calling image_resize_dimensions(), otherwise it will return non-false.
-			if ( ( $width !== $size['width'] || $height !== $size['height'] ) && ( $dims = image_resize_dimensions( $width, $height, $size['width'], $size['height'], $size['crop'] ) ) ) {
+			$dims = image_resize_dimensions( $width, $height, $size['width'], $size['height'], $size['crop'] );
+			if ( ( $width !== $size['width'] || $height !== $size['height'] ) && $dims ) {
 				list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
-				$sizes[ $name ] = array( 'width' => $dst_w, 'height' => $dst_h );
+				$sizes[ $name ] = array(
+					'width'  => $dst_w,
+					'height' => $dst_h,
+				);
 			}
 		}
 		return $sizes;
@@ -774,12 +823,12 @@ private function get_intermediate_image_sizes_for_attachment( $fullsizepath, $is
 	private function get_intermediate_sizes( $is_pdf, $metadata ) {
 		if ( $is_pdf ) {
 			// Copied from wp_generate_attachment_metadata() in "wp-admin/includes/image.php".
-			$fallback_sizes = array(
+			$fallback_sizes           = array(
 				'thumbnail',
 				'medium',
 				'large',
 			);
-			$intermediate_image_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
+			$intermediate_image_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
 		} else {
 			$intermediate_image_sizes = get_intermediate_image_sizes();
 		}
@@ -792,11 +841,10 @@ private function get_intermediate_sizes( $is_pdf, $metadata ) {
 			// For WP < 4.7.0.
 			global $_wp_additional_image_sizes;
 			if ( ! $_wp_additional_image_sizes ) {
-				$_wp_additional_image_sizes = array();
+				$_wp_additional_image_sizes = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Used as a fallback for WordPress version less than 4.7.0 as function wp_get_additional_image_sizes didn't exist then.
 			}
 		}
 
-
 		$sizes = array();
 		foreach ( $intermediate_image_sizes as $s ) {
 			if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
@@ -825,7 +873,7 @@ private function get_intermediate_sizes( $is_pdf, $metadata ) {
 
 		// Check here that not PDF (as filter not applied in core if is) and `$metadata` is array (as may not be and filter only applied in core when is).
 		if ( ! $is_pdf && is_array( $metadata ) ) {
-			$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata );
+			$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
 		}
 
 		return $sizes;

From 4f5fccd68e917c635ab1b57d9ab84783a736da7e Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Fri, 19 Apr 2019 19:39:17 +0530
Subject: [PATCH 4/6] PHPCS: Update ruleset and remove previously excluded
 directories

---
 phpcs.xml.dist | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 646cefcd2..75f665799 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -13,11 +13,6 @@
 	<!-- What to scan. -->
 	<file>.</file>
 
-	<!-- Ignoring select files/folders.
-		 https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
-	<exclude-pattern>*/node_modules/*</exclude-pattern>
-	<exclude-pattern>*/vendor/*</exclude-pattern>
-
 	<!-- Show progress. -->
 	<arg value="p"/>
 
@@ -29,11 +24,11 @@
 
 	<!--
 	#############################################################################
-	USE THE WPCliCS RULESET
+	USE THE WP_CLI_CS RULESET
 	#############################################################################
 	-->
 
-	<rule ref="WPCliCS"/>
+	<rule ref="WP_CLI_CS"/>
 
 	<!--
 	#############################################################################

From a2d940da645f591024e0e9b667387dcbc0935369 Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Fri, 19 Apr 2019 22:32:20 +0530
Subject: [PATCH 5/6] PHPCS: Move whitelist comment to preceding lines Update
 namespace function calls and array syntax

---
 media-command.php     | 25 +++++++++++++++----------
 src/Media_Command.php | 35 +++++++++++++++++++----------------
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/media-command.php b/media-command.php
index 7574e794a..d089f556b 100644
--- a/media-command.php
+++ b/media-command.php
@@ -9,17 +9,22 @@
 	require_once $wpcli_media_autoloader;
 }
 
+/**
+ * Check for Imagick and GD extensions availability.
+ *
+ * @throws \WP_CLI\ExitException
+ */
+function wpcli_media_assert_image_editor_support() {
+	if ( ! wp_image_editor_supports() ) {
+		WP_CLI::error(
+			'No support for generating images found. '
+			. 'Please install the Imagick or GD PHP extensions.'
+		);
+	}
+}
+
 WP_CLI::add_command(
 	'media',
 	'Media_Command',
-	array(
-		'before_invoke' => function () {
-			if ( ! wp_image_editor_supports() ) {
-				WP_CLI::error(
-					'No support for generating images found. ' .
-					'Please install the Imagick or GD PHP extensions.'
-				);
-			}
-		},
-	)
+	[ 'before_invoke' => 'wpcli_media_assert_image_editor_support' ]
 );
diff --git a/src/Media_Command.php b/src/Media_Command.php
index ecf84ace4..f48f59575 100644
--- a/src/Media_Command.php
+++ b/src/Media_Command.php
@@ -102,9 +102,7 @@ class Media_Command extends WP_CLI_Command {
 	public function regenerate( $args, $assoc_args = array() ) {
 		$assoc_args = wp_parse_args(
 			$assoc_args,
-			array(
-				'image_size' => '',
-			)
+			[ 'image_size' => '' ]
 		);
 
 		$image_size = $assoc_args['image_size'];
@@ -120,8 +118,8 @@ public function regenerate( $args, $assoc_args = array() ) {
 			}
 		}
 
-		$skip_delete  = \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-delete' );
-		$only_missing = \WP_CLI\Utils\get_flag_value( $assoc_args, 'only-missing' );
+		$skip_delete  = Utils\get_flag_value( $assoc_args, 'skip-delete' );
+		$only_missing = Utils\get_flag_value( $assoc_args, 'only-missing' );
 		if ( $only_missing ) {
 			$skip_delete = true;
 		}
@@ -286,7 +284,9 @@ public function import( $args, $assoc_args = array() ) {
 			if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
 				Utils\wp_clear_object_cache();
 			}
-			$is_file_remote = function_exists( 'wp_parse_url' ) ? wp_parse_url( $file, PHP_URL_HOST ) : parse_url( $file, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
+
+			// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
+			$is_file_remote = function_exists( 'wp_parse_url' ) ? wp_parse_url( $file, PHP_URL_HOST ) : parse_url( $file, PHP_URL_HOST );
 			$orig_filename  = $file;
 			$file_time      = '';
 
@@ -296,14 +296,14 @@ public function import( $args, $assoc_args = array() ) {
 					$errors++;
 					continue;
 				}
-				if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
+				if ( Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
 					$tempfile = $file;
 				} else {
 					$tempfile = $this->make_copy( $file );
 				}
 				$name = Utils\basename( $file );
 
-				if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'preserve-filetime' ) ) {
+				if ( Utils\get_flag_value( $assoc_args, 'preserve-filetime' ) ) {
 					$file_time = @filemtime( $file );
 				}
 			} else {
@@ -362,7 +362,7 @@ public function import( $args, $assoc_args = array() ) {
 				$post_array['post_title'] = preg_replace( '/\.[^.]+$/', '', Utils\basename( $file ) );
 			}
 
-			if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
+			if ( Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
 				$wp_filetype                  = wp_check_filetype( $file, null );
 				$post_array['post_mime_type'] = $wp_filetype['type'];
 				$post_array['post_status']    = 'inherit';
@@ -402,19 +402,19 @@ public function import( $args, $assoc_args = array() ) {
 			}
 
 			// Set as featured image, if --post_id and --featured_image are set
-			if ( $assoc_args['post_id'] && \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
+			if ( $assoc_args['post_id'] && Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
 				update_post_meta( $assoc_args['post_id'], '_thumbnail_id', $success );
 			}
 
 			$attachment_success_text = '';
 			if ( $assoc_args['post_id'] ) {
 				$attachment_success_text = " and attached to post {$assoc_args['post_id']}";
-				if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
+				if ( Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
 					$attachment_success_text .= ' as featured image';
 				}
 			}
 
-			if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
+			if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
 				WP_CLI::line( $success );
 			} else {
 				WP_CLI::log(
@@ -823,12 +823,13 @@ private function get_intermediate_image_sizes_for_attachment( $fullsizepath, $is
 	private function get_intermediate_sizes( $is_pdf, $metadata ) {
 		if ( $is_pdf ) {
 			// Copied from wp_generate_attachment_metadata() in "wp-admin/includes/image.php".
-			$fallback_sizes           = array(
+			$fallback_sizes = array(
 				'thumbnail',
 				'medium',
 				'large',
 			);
-			$intermediate_image_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
+			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
+			$intermediate_image_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
 		} else {
 			$intermediate_image_sizes = get_intermediate_image_sizes();
 		}
@@ -841,7 +842,8 @@ private function get_intermediate_sizes( $is_pdf, $metadata ) {
 			// For WP < 4.7.0.
 			global $_wp_additional_image_sizes;
 			if ( ! $_wp_additional_image_sizes ) {
-				$_wp_additional_image_sizes = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Used as a fallback for WordPress version less than 4.7.0 as function wp_get_additional_image_sizes didn't exist then.
+				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Used as a fallback for WordPress version less than 4.7.0 as function wp_get_additional_image_sizes didn't exist then.
+				$_wp_additional_image_sizes = array();
 			}
 		}
 
@@ -873,7 +875,8 @@ private function get_intermediate_sizes( $is_pdf, $metadata ) {
 
 		// Check here that not PDF (as filter not applied in core if is) and `$metadata` is array (as may not be and filter only applied in core when is).
 		if ( ! $is_pdf && is_array( $metadata ) ) {
-			$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
+			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
+			$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata );
 		}
 
 		return $sizes;

From ff6ce68025f0cc1082e6d123fb1f9473a1cd8dbb Mon Sep 17 00:00:00 2001
From: Thrijith Thankachan <thrijith13@gmail.com>
Date: Sat, 20 Apr 2019 00:25:01 +0530
Subject: [PATCH 6/6] Convert function passed to before_invoke into closure

---
 media-command.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/media-command.php b/media-command.php
index d089f556b..f5c4e74da 100644
--- a/media-command.php
+++ b/media-command.php
@@ -14,17 +14,17 @@
  *
  * @throws \WP_CLI\ExitException
  */
-function wpcli_media_assert_image_editor_support() {
+$wpcli_media_assert_image_editor_support = function () {
 	if ( ! wp_image_editor_supports() ) {
 		WP_CLI::error(
 			'No support for generating images found. '
 			. 'Please install the Imagick or GD PHP extensions.'
 		);
 	}
-}
+};
 
 WP_CLI::add_command(
 	'media',
 	'Media_Command',
-	[ 'before_invoke' => 'wpcli_media_assert_image_editor_support' ]
+	[ 'before_invoke' => $wpcli_media_assert_image_editor_support ]
 );