From 8cf788b93987b4a5e43787ed3344da14663feed9 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Sun, 3 Apr 2016 10:36:47 +0700 Subject: [PATCH] Code formatting, rip the bandaid off in one go --- ...lass-s3-uploads-changed-files-iterator.php | 8 +- inc/class-s3-uploads-local-stream-wrapper.php | 156 +++++++++--------- inc/class-s3-uploads-stream-wrapper.php | 16 +- inc/class-s3-uploads-uploadsyncbuilder.php | 77 +++++---- inc/class-s3-uploads.php | 8 +- s3-uploads.php | 2 +- 6 files changed, 132 insertions(+), 135 deletions(-) diff --git a/inc/class-s3-uploads-changed-files-iterator.php b/inc/class-s3-uploads-changed-files-iterator.php index 7943b8c1..4485b1ee 100644 --- a/inc/class-s3-uploads-changed-files-iterator.php +++ b/inc/class-s3-uploads-changed-files-iterator.php @@ -11,15 +11,15 @@ public function accept() { if ( $current->getMTime() > $data[1] ) { WP_CLI::line( "(dry-run) Uploading {$current->getPathname()}" ); } - return false; + return false; } - $key = $this->sourceConverter->convert((string) $current); - if (!($data = $this->getTargetData($key))) { + $key = $this->sourceConverter->convert( (string) $current ); + if ( ! ( $data = $this->getTargetData( $key ) ) ) { return true; } // Ensure it hasn't been modified since the mtime return $current->getMTime() > $data[1]; } -} \ No newline at end of file +} diff --git a/inc/class-s3-uploads-local-stream-wrapper.php b/inc/class-s3-uploads-local-stream-wrapper.php index 06bb0774..77b550ad 100644 --- a/inc/class-s3-uploads-local-stream-wrapper.php +++ b/inc/class-s3-uploads-local-stream-wrapper.php @@ -18,7 +18,7 @@ class S3_Uploads_Local_Stream_Wrapper { * * @var resource */ - public $handle = NULL; + public $handle = null; /** * Instance URI (stream). @@ -40,7 +40,7 @@ static function getDirectoryPath() { return $upload_dir['basedir'] . '/s3'; } - function setUri($uri) { + function setUri( $uri ) { $this->uri = $uri; } @@ -64,45 +64,45 @@ function getUri() { * Returns a string representing a location suitable for writing of a file, * or FALSE if unable to write to the file such as with read-only streams. */ - protected function getTarget($uri = NULL) { - if (!isset($uri)) { + protected function getTarget( $uri = null ) { + if ( ! isset( $uri ) ) { $uri = $this->uri; } - list($scheme, $target) = explode('://', $uri, 2); + list( $scheme, $target) = explode( '://', $uri, 2 ); // Remove erroneous leading or trailing, forward-slashes and backslashes. - return trim($target, '\/'); + return trim( $target, '\/' ); } - static function getMimeType($uri, $mapping = NULL) { + static function getMimeType( $uri, $mapping = null ) { $extension = ''; - $file_parts = explode('.', basename($uri)); + $file_parts = explode( '.', basename( $uri ) ); // Remove the first part: a full filename should not match an extension. - array_shift($file_parts); + array_shift( $file_parts ); // Iterate over the file parts, trying to find a match. // For my.awesome.image.jpeg, we try: -// - jpeg -// - image.jpeg, and -// - awesome.image.jpeg - while ($additional_part = array_pop($file_parts)) { - $extension = strtolower($additional_part . ($extension ? '.' . $extension : '')); - if (isset($mapping['extensions'][$extension])) { - return $mapping['mimetypes'][$mapping['extensions'][$extension]]; + // - jpeg + // - image.jpeg, and + // - awesome.image.jpeg + while ( $additional_part = array_pop( $file_parts ) ) { + $extension = strtolower( $additional_part . ( $extension ? '.' . $extension : '' ) ); + if ( isset( $mapping['extensions'][ $extension ] ) ) { + return $mapping['mimetypes'][ $mapping['extensions'][ $extension ] ]; } } return 'application/octet-stream'; } - function chmod($mode) { - $output = @chmod($this->getLocalPath(), $mode); + function chmod( $mode ) { + $output = @chmod( $this->getLocalPath(), $mode ); // We are modifying the underlying file here, so we have to clear the stat // cache so that PHP understands that URI has changed too. - clearstatcache(TRUE, $this->getLocalPath()); + clearstatcache( true, $this->getLocalPath() ); return $output; } @@ -123,17 +123,17 @@ function realpath() { * path, as determined by the realpath() function. If $uri is set but not * valid, returns FALSE. */ - protected function getLocalPath($uri = NULL) { - if (!isset($uri)) { + protected function getLocalPath( $uri = null ) { + if ( ! isset( $uri ) ) { $uri = $this->uri; } - $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri); + $path = $this->getDirectoryPath() . '/' . $this->getTarget( $uri ); $realpath = $path; - - $directory = realpath($this->getDirectoryPath()); - if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) { - return FALSE; + $directory = realpath( $this->getDirectoryPath() ); + + if ( ! $realpath || ! $directory || strpos( $realpath, $directory ) !== 0 ) { + return false; } return $realpath; } @@ -155,12 +155,12 @@ protected function getLocalPath($uri = NULL) { * * @see http://php.net/manual/streamwrapper.stream-open.php */ - public function stream_open($uri, $mode, $options, &$opened_path) { + public function stream_open( $uri, $mode, $options, &$opened_path ) { $this->uri = $uri; $path = $this->getLocalPath(); - $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode); + $this->handle = ( $options & STREAM_REPORT_ERRORS ) ? fopen( $path, $mode ) : @fopen( $path, $mode ); - if ((bool) $this->handle && $options & STREAM_USE_PATH) { + if ( (bool) $this->handle && $options & STREAM_USE_PATH ) { $opened_path = $path; } @@ -183,12 +183,12 @@ public function stream_open($uri, $mode, $options, &$opened_path) { * * @see http://php.net/manual/streamwrapper.stream-lock.php */ - public function stream_lock($operation) { - if (in_array($operation, array(LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB))) { - return flock($this->handle, $operation); + public function stream_lock( $operation ) { + if ( in_array( $operation, array( LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB ) ) ) { + return flock( $this->handle, $operation ); } - return TRUE; + return true; } /** @@ -202,8 +202,8 @@ public function stream_lock($operation) { * * @see http://php.net/manual/streamwrapper.stream-read.php */ - public function stream_read($count) { - return fread($this->handle, $count); + public function stream_read( $count ) { + return fread( $this->handle, $count ); } /** @@ -217,8 +217,8 @@ public function stream_read($count) { * * @see http://php.net/manual/streamwrapper.stream-write.php */ - public function stream_write($data) { - return fwrite($this->handle, $data); + public function stream_write( $data ) { + return fwrite( $this->handle, $data ); } /** @@ -230,7 +230,7 @@ public function stream_write($data) { * @see http://php.net/manual/streamwrapper.stream-eof.php */ public function stream_eof() { - return feof($this->handle); + return feof( $this->handle ); } /** @@ -246,10 +246,10 @@ public function stream_eof() { * * @see http://php.net/manual/streamwrapper.stream-seek.php */ - public function stream_seek($offset, $whence) { + public function stream_seek( $offset, $whence ) { // fseek returns 0 on success and -1 on a failure. // stream_seek 1 on success and 0 on a failure. - return !fseek($this->handle, $offset, $whence); + return ! fseek( $this->handle, $offset, $whence ); } /** @@ -261,7 +261,7 @@ public function stream_seek($offset, $whence) { * @see http://php.net/manual/streamwrapper.stream-flush.php */ public function stream_flush() { - return fflush($this->handle); + return fflush( $this->handle ); } /** @@ -273,7 +273,7 @@ public function stream_flush() { * @see http://php.net/manual/streamwrapper.stream-tell.php */ public function stream_tell() { - return ftell($this->handle); + return ftell( $this->handle ); } /** @@ -286,7 +286,7 @@ public function stream_tell() { * @see http://php.net/manual/streamwrapper.stream-stat.php */ public function stream_stat() { - return fstat($this->handle); + return fstat( $this->handle ); } /** @@ -298,7 +298,7 @@ public function stream_stat() { * @see http://php.net/manual/streamwrapper.stream-close.php */ public function stream_close() { - return fclose($this->handle); + return fclose( $this->handle ); } /** @@ -313,7 +313,7 @@ public function stream_close() { * * @see http://php.net/manual/streamwrapper.stream-cast.php */ - public function stream_cast($cast_as) { + public function stream_cast( $cast_as ) { return false; } @@ -328,9 +328,9 @@ public function stream_cast($cast_as) { * * @see http://php.net/manual/streamwrapper.unlink.php */ - public function unlink($uri) { + public function unlink( $uri ) { $this->uri = $uri; - return unlink($this->getLocalPath()); + return unlink( $this->getLocalPath() ); } /** @@ -346,8 +346,8 @@ public function unlink($uri) { * * @see http://php.net/manual/streamwrapper.rename.php */ - public function rename($from_uri, $to_uri) { - return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri)); + public function rename( $from_uri, $to_uri ) { + return rename( $this->getLocalPath( $from_uri ), $this->getLocalPath( $to_uri ) ); } /** @@ -365,22 +365,20 @@ public function rename($from_uri, $to_uri) { * * @see http://php.net/manual/streamwrapper.mkdir.php */ - public function mkdir($uri, $mode, $options) { + public function mkdir( $uri, $mode, $options ) { $this->uri = $uri; $recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE); - if ($recursive) { + if ( $recursive ) { // $this->getLocalPath() fails if $uri has multiple levels of directories // that do not yet exist. - $localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri); - } - else { - $localpath = $this->getLocalPath($uri); - } - if ($options & STREAM_REPORT_ERRORS) { - return mkdir($localpath, $mode, $recursive); + $localpath = $this->getDirectoryPath() . '/' . $this->getTarget( $uri ); + } else { + $localpath = $this->getLocalPath( $uri ); } - else { - return @mkdir($localpath, $mode, $recursive); + if ( $options & STREAM_REPORT_ERRORS ) { + return mkdir( $localpath, $mode, $recursive ); + } else { + return @mkdir( $localpath, $mode, $recursive ); } } @@ -397,13 +395,12 @@ public function mkdir($uri, $mode, $options) { * * @see http://php.net/manual/streamwrapper.rmdir.php */ - public function rmdir($uri, $options) { + public function rmdir( $uri, $options ) { $this->uri = $uri; - if ($options & STREAM_REPORT_ERRORS) { - return rmdir($this->getLocalPath()); - } - else { - return @rmdir($this->getLocalPath()); + if ( $options & STREAM_REPORT_ERRORS ) { + return rmdir( $this->getLocalPath() ); + } else { + return @rmdir( $this->getLocalPath() ); } } @@ -421,16 +418,15 @@ public function rmdir($uri, $options) { * * @see http://php.net/manual/streamwrapper.url-stat.php */ - public function url_stat($uri, $flags) { + public function url_stat( $uri, $flags ) { $this->uri = $uri; $path = $this->getLocalPath(); // Suppress warnings if requested or if the file or directory does not // exist. This is consistent with PHP's plain filesystem stream wrapper. - if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) { - return @stat($path); - } - else { - return stat($path); + if ( $flags & STREAM_URL_STAT_QUIET || ! file_exists( $path ) ) { + return @stat( $path ); + } else { + return stat( $path ); } } @@ -447,9 +443,9 @@ public function url_stat($uri, $flags) { * * @see http://php.net/manual/streamwrapper.dir-opendir.php */ - public function dir_opendir($uri, $options) { + public function dir_opendir( $uri, $options ) { $this->uri = $uri; - $this->handle = opendir($this->getLocalPath()); + $this->handle = opendir( $this->getLocalPath() ); return (bool) $this->handle; } @@ -463,7 +459,7 @@ public function dir_opendir($uri, $options) { * @see http://php.net/manual/streamwrapper.dir-readdir.php */ public function dir_readdir() { - return readdir($this->handle); + return readdir( $this->handle ); } /** @@ -475,11 +471,11 @@ public function dir_readdir() { * @see http://php.net/manual/streamwrapper.dir-rewinddir.php */ public function dir_rewinddir() { - rewinddir($this->handle); + rewinddir( $this->handle ); // We do not really have a way to signal a failure as rewinddir() does not // have a return value and there is no way to read a directory handler // without advancing to the next file. - return TRUE; + return true; } /** @@ -491,9 +487,9 @@ public function dir_rewinddir() { * @see http://php.net/manual/streamwrapper.dir-closedir.php */ public function dir_closedir() { - closedir($this->handle); + closedir( $this->handle ); // We do not really have a way to signal a failure as closedir() does not // have a return value. - return TRUE; + return true; } -} \ No newline at end of file +} diff --git a/inc/class-s3-uploads-stream-wrapper.php b/inc/class-s3-uploads-stream-wrapper.php index 6f582cf8..b7179865 100644 --- a/inc/class-s3-uploads-stream-wrapper.php +++ b/inc/class-s3-uploads-stream-wrapper.php @@ -28,15 +28,15 @@ public function stream_flush() { /// Expires: if ( defined( 'S3_UPLOADS_HTTP_EXPIRES' ) ) { - $this->params[ 'Expires' ] = S3_UPLOADS_HTTP_EXPIRES; + $this->params['Expires'] = S3_UPLOADS_HTTP_EXPIRES; } // Cache-Control: if ( defined( 'S3_UPLOADS_HTTP_CACHE_CONTROL' ) ) { if ( is_numeric( S3_UPLOADS_HTTP_CACHE_CONTROL ) ) { - $this->params[ 'CacheControl' ] = 'max-age='. S3_UPLOADS_HTTP_CACHE_CONTROL; + $this->params['CacheControl'] = 'max-age=' . S3_UPLOADS_HTTP_CACHE_CONTROL; } else { - $this->params[ 'CacheControl' ] = S3_UPLOADS_HTTP_CACHE_CONTROL; + $this->params['CacheControl'] = S3_UPLOADS_HTTP_CACHE_CONTROL; } } @@ -71,7 +71,7 @@ public function stream_open( $path, $mode, $options, &$opened_path ) { $mode_short = substr( $mode, 0, 1 ); - if ( $mode_short === 'r' || $mode_short === 'a' ) { + if ( 'r' === $mode_short || 'a' === $mode_short ) { return $result; } @@ -86,9 +86,9 @@ public function stream_open( $path, $mode, $options, &$opened_path ) { try { $p = $this->params; $p['Body'] = ''; - static::$client->putObject($p); - } catch (\Exception $e) { - return $this->triggerError($e->getMessage()); + static::$client->putObject( $p ); + } catch ( \Exception $e ) { + return $this->triggerError( $e->getMessage() ); } return $result; @@ -107,7 +107,7 @@ public function stream_open( $path, $mode, $options, &$opened_path ) { * @link http://www.php.net/manual/en/streamwrapper.url-stat.php */ public function url_stat( $path, $flags ) { - $extension = pathinfo($path, PATHINFO_EXTENSION); + $extension = pathinfo( $path, PATHINFO_EXTENSION ); /** * If the file is actually just a path to a directory diff --git a/inc/class-s3-uploads-uploadsyncbuilder.php b/inc/class-s3-uploads-uploadsyncbuilder.php index 8b015061..30ccaf34 100644 --- a/inc/class-s3-uploads-uploadsyncbuilder.php +++ b/inc/class-s3-uploads-uploadsyncbuilder.php @@ -2,44 +2,43 @@ class S3_Uploads_UploadSyncBuilder extends Aws\S3\Sync\UploadSyncBuilder { - public function __construct( $is_dry_run = false ) { - $this->dry_run = $is_dry_run; - } + public function __construct( $is_dry_run = false ) { + $this->dry_run = $is_dry_run; + } /** - * Builds a UploadSync or DownloadSync object - * - * @return AbstractSync - */ - public function build() - { - $this->validateRequirements(); - $this->sourceConverter = $this->sourceConverter ?: $this->getDefaultSourceConverter(); - $this->targetConverter = $this->targetConverter ?: $this->getDefaultTargetConverter(); - - // Only wrap the source iterator in a changed files iterator if we are not forcing the transfers - if (!$this->forcing) { - $this->sourceIterator->rewind(); - $this->sourceIterator = new S3_Uploads_ChangedFilesIterator( - new \NoRewindIterator($this->sourceIterator), - $this->getTargetIterator(), - $this->sourceConverter, - $this->targetConverter - ); - $this->sourceIterator->dry_run = $this->dry_run; - $this->sourceIterator->rewind(); - } - - $sync = $this->specificBuild(); - - if ($this->params) { - $this->addCustomParamListener($sync); - } - - if ($this->debug) { - $this->addDebugListener($sync, is_bool($this->debug) ? STDOUT : $this->debug); - } - - return $sync; - } -} \ No newline at end of file + * Builds a UploadSync or DownloadSync object + * + * @return AbstractSync + */ + public function build() { + $this->validateRequirements(); + $this->sourceConverter = $this->sourceConverter ?: $this->getDefaultSourceConverter(); + $this->targetConverter = $this->targetConverter ?: $this->getDefaultTargetConverter(); + + // Only wrap the source iterator in a changed files iterator if we are not forcing the transfers + if ( ! $this->forcing ) { + $this->sourceIterator->rewind(); + $this->sourceIterator = new S3_Uploads_ChangedFilesIterator( + new \NoRewindIterator( $this->sourceIterator ), + $this->getTargetIterator(), + $this->sourceConverter, + $this->targetConverter + ); + $this->sourceIterator->dry_run = $this->dry_run; + $this->sourceIterator->rewind(); + } + + $sync = $this->specificBuild(); + + if ( $this->params ) { + $this->addCustomParamListener( $sync ); + } + + if ( $this->debug ) { + $this->addDebugListener( $sync, is_bool( $this->debug ) ? STDOUT : $this->debug ); + } + + return $sync; + } +} diff --git a/inc/class-s3-uploads.php b/inc/class-s3-uploads.php index d74f73c1..b75ad81e 100644 --- a/inc/class-s3-uploads.php +++ b/inc/class-s3-uploads.php @@ -129,8 +129,9 @@ public function get_s3_url() { public function get_original_upload_dir() { - if ( empty( $this->original_upload_dir ) ) + if ( empty( $this->original_upload_dir ) ) { wp_upload_dir(); + } return $this->original_upload_dir; } @@ -140,8 +141,9 @@ public function get_original_upload_dir() { */ public function s3() { - if ( ! empty( $this->s3 ) ) + if ( ! empty( $this->s3 ) ) { return $this->s3; + } $params = array(); @@ -176,7 +178,7 @@ public function s3() { public function filter_editors( $editors ) { if ( ( $position = array_search( 'WP_Image_Editor_Imagick', $editors ) ) !== false ) { - unset($editors[$position]); + unset( $editors[ $position ] ); } array_unshift( $editors, 'S3_Uploads_Image_Editor_Imagick' ); diff --git a/s3-uploads.php b/s3-uploads.php index b6dea54e..3efaae38 100644 --- a/s3-uploads.php +++ b/s3-uploads.php @@ -103,7 +103,7 @@ function s3_uploads_autoload( $class_name ) { } } -spl_autoload_register( 's3_uploads_autoload'); +spl_autoload_register( 's3_uploads_autoload' ); // Require AWS Autoloader file. require_once dirname( __FILE__ ) . '/lib/aws-sdk/aws-autoloader.php';