From 62428ca0ef67542e9196f23f680dce7b9435d104 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 17 Jun 2017 23:41:57 +0200 Subject: [PATCH] Fix code style --- .gitignore | 6 ++-- .php_cs | 15 ---------- .php_cs.dist | 39 ++++++++++++++++++++++++ Makefile | 45 ++++++++++++++++++++++++++++ lib/BlockingDriver.php | 45 +++++++++++++++++----------- lib/BlockingHandle.php | 30 +++++++++++-------- lib/Driver.php | 34 ++++++++++----------- lib/EioDriver.php | 11 ++++--- lib/EioHandle.php | 4 ++- lib/Handle.php | 16 +++++----- lib/Internal/FileTask.php | 21 +++++++------ lib/ParallelDriver.php | 11 +++++-- lib/ParallelHandle.php | 8 +++-- lib/UvDriver.php | 26 +++++++++------- lib/UvHandle.php | 7 +++-- lib/functions.php | 59 +++++++++++++++++++------------------ test/BlockingDriverTest.php | 2 +- test/BlockingHandleTest.php | 2 +- test/DriverTest.php | 2 +- test/EioDriverTest.php | 2 +- test/EioHandleTest.php | 2 +- test/ParallelDriverTest.php | 2 +- test/ParallelHandleTest.php | 2 +- test/UvDriverTest.php | 2 +- test/UvHandleTest.php | 4 +-- 25 files changed, 252 insertions(+), 145 deletions(-) delete mode 100644 .php_cs create mode 100644 .php_cs.dist create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 6b772a8..8a3d477 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -composer.lock -vendor/ -.idea +/composer.lock +/vendor +/.php_cs.cache \ No newline at end of file diff --git a/.php_cs b/.php_cs deleted file mode 100644 index d1a2e36..0000000 --- a/.php_cs +++ /dev/null @@ -1,15 +0,0 @@ -level(Symfony\CS\FixerInterface::NONE_LEVEL) - ->fixers([ - "psr2", - "-braces", - "-psr0", - ]) - ->finder( - Symfony\CS\Finder\DefaultFinder::create() - ->in(__DIR__ . "/lib") - ->in(__DIR__ . "/test") - ) -; diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..abf03b1 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,39 @@ +setRiskyAllowed(true) + ->setRules([ + "@PSR1" => true, + "@PSR2" => true, + "braces" => [ + "allow_single_line_closure" => true, + "position_after_functions_and_oop_constructs" => "same", + ], + "array_syntax" => ["syntax" => "short"], + "cast_spaces" => true, + "combine_consecutive_unsets" => true, + "function_to_constant" => true, + "no_multiline_whitespace_before_semicolons" => true, + "no_unused_imports" => true, + "no_useless_else" => true, + "no_useless_return" => true, + "no_whitespace_before_comma_in_array" => true, + "no_whitespace_in_blank_line" => true, + "non_printable_character" => true, + "normalize_index_brace" => true, + "ordered_imports" => true, + "php_unit_construct" => true, + "php_unit_dedicate_assert" => true, + "php_unit_fqcn_annotation" => true, + "phpdoc_summary" => true, + "phpdoc_types" => true, + "psr4" => true, + "return_type_declaration" => ["space_before" => "none"], + "short_scalar_cast" => true, + "single_blank_line_before_namespace" => true, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__ . "/lib") + ->in(__DIR__ . "/test") + ); \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8230c6b --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +PHP_BIN := php +COMPOSER_BIN := composer + +COVERAGE = coverage +SRCS = lib test + +find_php_files = $(shell find $(1) -type f -name "*.php") +src = $(foreach d,$(SRCS),$(call find_php_files,$(d))) + +.PHONY: test +test: setup phpunit code-style + +.PHONY: clean +clean: clean-coverage clean-vendor + +.PHONY: clean-coverage +clean-coverage: + test ! -e coverage || rm -r coverage + +.PHONY: clean-vendor +clean-vendor: + test ! -e vendor || rm -r vendor + +.PHONY: setup +setup: vendor/autoload.php + +.PHONY: deps-update +deps-update: + $(COMPOSER_BIN) update + +.PHONY: phpunit +phpunit: setup + $(PHP_BIN) vendor/bin/phpunit + +.PHONY: code-style +code-style: setup + PHP_CS_FIXER_IGNORE_ENV=1 $(PHP_BIN) vendor/bin/php-cs-fixer --diff -v fix + +composer.lock: composer.json + $(COMPOSER_BIN) install + touch $@ + +vendor/autoload.php: composer.lock + $(COMPOSER_BIN) install + touch $@ \ No newline at end of file diff --git a/lib/BlockingDriver.php b/lib/BlockingDriver.php index 6d1cd0a..661c8c1 100644 --- a/lib/BlockingDriver.php +++ b/lib/BlockingDriver.php @@ -2,7 +2,9 @@ namespace Amp\File; -use Amp\{ Success, Failure, Promise }; +use Amp\Failure; +use Amp\Promise; +use Amp\Success; class BlockingDriver implements Driver { /** @@ -41,6 +43,7 @@ class BlockingDriver implements Driver { if ($exists = @\file_exists($path)) { \clearstatcache(true, $path); } + return new Success($exists); } @@ -58,18 +61,22 @@ class BlockingDriver implements Driver { return new Failure(new FilesystemException( "Path does not exist" )); - } elseif (!@\is_file($path)) { + } + + if (!@\is_file($path)) { return new Failure(new FilesystemException( "Path is not a regular file" )); - } elseif (($size = @\filesize($path)) === false) { + } + + if (($size = @\filesize($path)) === false) { return new Failure(new FilesystemException( \error_get_last()["message"] )); - } else { - \clearstatcache(true, $path); - return new Success($size); } + + \clearstatcache(true, $path); + return new Success($size); } /** @@ -85,6 +92,7 @@ class BlockingDriver implements Driver { if (!@\file_exists($path)) { return new Success(false); } + $isDir = @\is_dir($path); \clearstatcache(true, $path); @@ -104,6 +112,7 @@ class BlockingDriver implements Driver { if (!@\file_exists($path)) { return new Success(false); } + $isFile = @\is_file($path); \clearstatcache(true, $path); @@ -111,7 +120,7 @@ class BlockingDriver implements Driver { } /** - * Retrieve the path's last modification time as a unix timestamp + * Retrieve the path's last modification time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -122,6 +131,7 @@ class BlockingDriver implements Driver { "Path does not exist" )); } + $mtime = @\filemtime($path); \clearstatcache(true, $path); @@ -129,7 +139,7 @@ class BlockingDriver implements Driver { } /** - * Retrieve the path's last access time as a unix timestamp + * Retrieve the path's last access time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -147,7 +157,7 @@ class BlockingDriver implements Driver { } /** - * Retrieve the path's creation time as a unix timestamp + * Retrieve the path's creation time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -158,6 +168,7 @@ class BlockingDriver implements Driver { "Path does not exist" )); } + $ctime = @\filectime($path); \clearstatcache(true, $path); @@ -253,16 +264,16 @@ class BlockingDriver implements Driver { "Not a directory" )); } elseif ($arr = @\scandir($path)) { - $arr = \array_values(\array_filter($arr, function($el) { + $arr = \array_values(\array_filter($arr, function ($el) { return !($el === "." || $el === ".."); })); \clearstatcache(true, $path); return new Success($arr); - } else { - return new Failure(new FilesystemException( - "Failed reading contents from {$path}" - )); } + + return new Failure(new FilesystemException( + "Failed reading contents from {$path}" + )); } /** @@ -305,8 +316,7 @@ class BlockingDriver implements Driver { $result = @\file_get_contents($path); return ($result === false) ? new Failure(new FilesystemException(\error_get_last()["message"])) - : new Success($result) - ; + : new Success($result); } /** @@ -316,7 +326,6 @@ class BlockingDriver implements Driver { $result = @\file_put_contents($path, $contents); return ($result === false) ? new Failure(new FilesystemException(\error_get_last()["message"])) - : new Success($result) - ; + : new Success($result); } } diff --git a/lib/BlockingHandle.php b/lib/BlockingHandle.php index 396d8c4..1ca7e8c 100644 --- a/lib/BlockingHandle.php +++ b/lib/BlockingHandle.php @@ -2,7 +2,9 @@ namespace Amp\File; -use Amp\{ Success, Failure, Promise }; +use Amp\Failure; +use Amp\Promise; +use Amp\Success; class BlockingHandle implements Handle { private $fh; @@ -35,13 +37,14 @@ class BlockingHandle implements Handle { } $data = \fread($this->fh, $length); + if ($data !== false) { return new Success(\strlen($data) ? $data : null); - } else { - return new Failure(new FilesystemException( - "Failed reading from file handle" - )); } + + return new Failure(new FilesystemException( + "Failed reading from file handle" + )); } /** @@ -53,13 +56,14 @@ class BlockingHandle implements Handle { } $len = \fwrite($this->fh, $data); + if ($len !== false) { return new Success($len); - } else { - return new Failure(new FilesystemException( - "Failed writing to file handle" - )); } + + return new Failure(new FilesystemException( + "Failed writing to file handle" + )); } /** @@ -84,11 +88,11 @@ class BlockingHandle implements Handle { if (@\fclose($fh)) { return new Success; - } else { - return new Failure(new FilesystemException( - "Failed closing file handle" - )); } + + return new Failure(new FilesystemException( + "Failed closing file handle" + )); } /** diff --git a/lib/Driver.php b/lib/Driver.php index e5b338c..6068a7a 100644 --- a/lib/Driver.php +++ b/lib/Driver.php @@ -6,7 +6,7 @@ use Amp\Promise; interface Driver { /** - * Open a handle for the specified path + * Open a handle for the specified path. * * @param string $path * @param string $mode @@ -15,7 +15,7 @@ interface Driver { public function open(string $path, string $mode): Promise; /** - * Execute a file stat operation + * Execute a file stat operation. * * If the requested path does not exist the resulting Promise will resolve to NULL. * @@ -69,7 +69,7 @@ interface Driver { public function isfile(string $path): Promise; /** - * Retrieve the path's last modification time as a unix timestamp + * Retrieve the path's last modification time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -77,7 +77,7 @@ interface Driver { public function mtime(string $path): Promise; /** - * Retrieve the path's last access time as a unix timestamp + * Retrieve the path's last access time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -85,7 +85,7 @@ interface Driver { public function atime(string $path): Promise; /** - * Retrieve the path's creation time as a unix timestamp + * Retrieve the path's creation time as a unix timestamp. * * @param string $path An absolute file system path * @return \Amp\Promise @@ -93,7 +93,7 @@ interface Driver { public function ctime(string $path): Promise; /** - * Same as stat() except if the path is a link then the link's data is returned + * Same as stat() except if the path is a link then the link's data is returned. * * @param string $path The file system path to stat * @return \Amp\Promise A promise resolving to an associative array upon successful resolution @@ -101,7 +101,7 @@ interface Driver { public function lstat(string $path): Promise; /** - * Create a symlink $link pointing to the file/directory located at $target + * Create a symlink $link pointing to the file/directory located at $target. * * @param string $target * @param string $link @@ -110,7 +110,7 @@ interface Driver { public function symlink(string $target, string $link): Promise; /** - * Create a hard link $link pointing to the file/directory located at $target + * Create a hard link $link pointing to the file/directory located at $target. * * @param string $target * @param string $link @@ -127,7 +127,7 @@ interface Driver { public function readlink(string $target): Promise; /** - * Rename a file or directory + * Rename a file or directory. * * @param string $from * @param string $to @@ -136,7 +136,7 @@ interface Driver { public function rename(string $from, string $to): Promise; /** - * Delete a file + * Delete a file. * * @param string $path * @return \Amp\Promise @@ -144,7 +144,7 @@ interface Driver { public function unlink(string $path): Promise; /** - * Create a director + * Create a director. * * @param string $path * @param int $mode @@ -154,7 +154,7 @@ interface Driver { public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise; /** - * Delete a directory + * Delete a directory. * * @param string $path * @return \Amp\Promise @@ -162,7 +162,7 @@ interface Driver { public function rmdir(string $path): Promise; /** - * Retrieve an array of files and directories inside the specified path + * Retrieve an array of files and directories inside the specified path. * * Dot entries are not included in the resulting array (i.e. "." and ".."). * @@ -172,7 +172,7 @@ interface Driver { public function scandir(string $path): Promise; /** - * chmod a file or directory + * chmod a file or directory. * * @param string $path * @param int $mode @@ -181,7 +181,7 @@ interface Driver { public function chmod(string $path, int $mode): Promise; /** - * chown a file or directory + * chown a file or directory. * * @param string $path * @param int $uid @@ -191,7 +191,7 @@ interface Driver { public function chown(string $path, int $uid, int $gid): Promise; /** - * Update the access and modification time of the specified path + * Update the access and modification time of the specified path. * * If the file does not exist it will be created automatically. * @@ -201,7 +201,7 @@ interface Driver { public function touch(string $path): Promise; /** - * Buffer the specified file's contents + * Buffer the specified file's contents. * * @param string $path The file path from which to buffer contents * @return \Amp\Promise A promise resolving to a string upon successful resolution diff --git a/lib/EioDriver.php b/lib/EioDriver.php index 4ad0e8b..656c87a 100644 --- a/lib/EioDriver.php +++ b/lib/EioDriver.php @@ -2,7 +2,10 @@ namespace Amp\File; -use Amp\{ Deferred, Loop, Promise, Success }; +use Amp\Deferred; +use Amp\Loop; +use Amp\Promise; +use Amp\Success; class EioDriver implements Driver { private $watcher; @@ -22,7 +25,7 @@ class EioDriver implements Driver { \eio_init(); self::$stream = \eio_get_event_stream(); } - $this->callableDecrementor = function() { + $this->callableDecrementor = function () { ($this->incrementor)(-1); }; $this->incrementor = function ($increment) { @@ -390,11 +393,11 @@ class EioDriver implements Driver { $priority = \EIO_PRI_DEFAULT; if ($recursive) { - $path = str_replace("/", DIRECTORY_SEPARATOR, $path); + $path = str_replace("/", DIRECTORY_SEPARATOR, $path); $arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path)); $tmpPath = ""; - $callback = function() use ( + $callback = function () use ( &$callback, &$arrayPath, &$tmpPath, $mode, $priority, $deferred ) { $tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath); diff --git a/lib/EioHandle.php b/lib/EioHandle.php index a92a925..7e7ee92 100644 --- a/lib/EioHandle.php +++ b/lib/EioHandle.php @@ -2,7 +2,9 @@ namespace Amp\File; -use Amp\{ Deferred, Promise, Success }; +use Amp\Deferred; +use Amp\Promise; +use Amp\Success; class EioHandle implements Handle { const OP_READ = 1; diff --git a/lib/Handle.php b/lib/Handle.php index dd9b02e..9f06a8a 100644 --- a/lib/Handle.php +++ b/lib/Handle.php @@ -10,7 +10,7 @@ interface Handle extends InputStream, OutputStream { const DEFAULT_READ_LENGTH = 8192; /** - * Read $len bytes from the open file handle starting at $offset + * Read $len bytes from the open file handle starting at $offset. * * @param int $length * @return \Amp\Promise @@ -18,7 +18,7 @@ interface Handle extends InputStream, OutputStream { public function read(int $length = self::DEFAULT_READ_LENGTH): Promise; /** - * Write $data to the open file handle starting at $offset + * Write $data to the open file handle starting at $offset. * * @param string $data * @return \Amp\Promise @@ -35,7 +35,7 @@ interface Handle extends InputStream, OutputStream { public function end(string $data = ""): Promise; /** - * Close the file handle + * Close the file handle. * * Applications are not required to manually close handles -- they will * be unloaded automatically when the object is garbage collected. @@ -45,7 +45,7 @@ interface Handle extends InputStream, OutputStream { public function close(): Promise; /** - * Set the handle's internal pointer position + * Set the handle's internal pointer position. * * $whence values: * @@ -60,28 +60,28 @@ interface Handle extends InputStream, OutputStream { public function seek(int $position, int $whence = \SEEK_SET): Promise; /** - * Return the current internal offset position of the file handle + * Return the current internal offset position of the file handle. * * @return int */ public function tell(): int; /** - * Test for "end-of-file" on the file handle + * Test for "end-of-file" on the file handle. * * @return bool */ public function eof(): bool; /** - * Retrieve the path used when opening the file handle + * Retrieve the path used when opening the file handle. * * @return string */ public function path(): string; /** - * Retrieve the mode used when opening the file handle + * Retrieve the mode used when opening the file handle. * * @return string */ diff --git a/lib/Internal/FileTask.php b/lib/Internal/FileTask.php index bb4c6fd..a23b326 100644 --- a/lib/Internal/FileTask.php +++ b/lib/Internal/FileTask.php @@ -1,8 +1,11 @@ operation) { $path = $this->args[0]; $mode = \str_replace(['b', 't'], '', $this->args[1]); - + switch ($mode) { case "r": case "r+": @@ -65,13 +68,13 @@ class FileTask extends BlockingDriver implements Task { case "c": case "c+": break; - + default: throw new FilesystemException("Invalid file mode"); } - + $handle = @\fopen($path, $mode . 'b'); - + if (!$handle) { $message = 'Could not open the file.'; if ($error = \error_get_last()) { @@ -79,12 +82,12 @@ class FileTask extends BlockingDriver implements Task { } throw new FilesystemException($message); } - + $file = new BlockingHandle($handle, $path, $mode); $id = (int) $handle; $size = \fstat($handle)["size"]; $environment->set($this->makeId($id), $file); - + return [$id, $size, $mode]; } diff --git a/lib/ParallelDriver.php b/lib/ParallelDriver.php index fbe6304..e599eb6 100644 --- a/lib/ParallelDriver.php +++ b/lib/ParallelDriver.php @@ -2,9 +2,13 @@ namespace Amp\File; -use Amp\{ Coroutine, Deferred, Promise }; +use Amp\Coroutine; +use Amp\Deferred; use Amp\Parallel\Worker; -use Amp\Parallel\Worker\{ Pool, TaskException, WorkerException }; +use Amp\Parallel\Worker\Pool; +use Amp\Parallel\Worker\TaskException; +use Amp\Parallel\Worker\WorkerException; +use Amp\Promise; class ParallelDriver implements Driver { /** @@ -211,4 +215,5 @@ class ParallelDriver implements Driver { */ public function put(string $path, string $contents): Promise { return new Coroutine($this->runFileTask(new Internal\FileTask("put", [$path, $contents]))); -}} + } +} diff --git a/lib/ParallelHandle.php b/lib/ParallelHandle.php index 000e3f7..2793983 100644 --- a/lib/ParallelHandle.php +++ b/lib/ParallelHandle.php @@ -2,8 +2,12 @@ namespace Amp\File; -use Amp\{ Coroutine, Promise, Success }; -use Amp\Parallel\Worker\{ TaskException, Worker, WorkerException }; +use Amp\Coroutine; +use Amp\Parallel\Worker\TaskException; +use Amp\Parallel\Worker\Worker; +use Amp\Parallel\Worker\WorkerException; +use Amp\Promise; +use Amp\Success; class ParallelHandle implements Handle { /** @var \Amp\Parallel\Worker\Worker */ diff --git a/lib/UvDriver.php b/lib/UvDriver.php index 933192f..7ba0dcf 100644 --- a/lib/UvDriver.php +++ b/lib/UvDriver.php @@ -2,7 +2,11 @@ namespace Amp\File; -use Amp\{ Coroutine, Deferred, Loop, Promise, Success }; +use Amp\Coroutine; +use Amp\Deferred; +use Amp\Loop; +use Amp\Promise; +use Amp\Success; class UvDriver implements Driver { /** @var \Amp\Loop\Driver */ @@ -22,7 +26,7 @@ class UvDriver implements Driver { $this->loop = $driver->getHandle(); // dummy handle to be able to tell the loop that there is work being done and it shouldn't abort if there are no other watchers at a given moment - $this->busy = $driver->repeat(PHP_INT_MAX, function(){ }); + $this->busy = $driver->repeat(PHP_INT_MAX, function () { }); $driver->unreference($this->busy); } @@ -41,7 +45,7 @@ class UvDriver implements Driver { $this->onOpenHandle($fh, $openArr); } else { $this->driver->unreference($this->busy); - list( , $path, $deferred) = $openArr; + list(, $path, $deferred) = $openArr; $deferred->fail(new FilesystemException( "Failed opening file handle to $path" )); @@ -79,20 +83,20 @@ class UvDriver implements Driver { if ($fh) { $this->finalizeHandle($fh, $size = 0, $openArr); } else { - list( , $path, $deferred) = $openArr; + list(, $path, $deferred) = $openArr; $deferred->fail(new FilesystemException( "Failed truncating file $path" )); } }); } else { - \uv_fs_fstat($this->loop, $fh, function($fh, $stat) use ($openArr) { + \uv_fs_fstat($this->loop, $fh, function ($fh, $stat) use ($openArr) { $this->driver->unreference($this->busy); if ($fh) { StatCache::set($openArr[1], $stat); $this->finalizeHandle($fh, $stat["size"], $openArr); } else { - list( , $path, $deferred) = $openArr; + list(, $path, $deferred) = $openArr; $deferred->fail(new FilesystemException( "Failed reading file size from open handle pointing to $path" )); @@ -117,7 +121,7 @@ class UvDriver implements Driver { $this->driver->reference($this->busy); $deferred = new Deferred; - \uv_fs_stat($this->loop, $path, function($fh, $stat) use ($deferred, $path) { + \uv_fs_stat($this->loop, $path, function ($fh, $stat) use ($deferred, $path) { if (empty($fh)) { $stat = null; } else { @@ -256,7 +260,7 @@ class UvDriver implements Driver { public function lstat(string $path): Promise { $this->driver->reference($this->busy); $deferred = new Deferred; - \uv_fs_lstat($this->loop, $path, function($fh, $stat) use ($deferred) { + \uv_fs_lstat($this->loop, $path, function ($fh, $stat) use ($deferred) { if (empty($fh)) { $stat = null; } @@ -347,7 +351,7 @@ class UvDriver implements Driver { $deferred = new Deferred; if ($recursive) { - $path = str_replace("/", DIRECTORY_SEPARATOR, $path); + $path = str_replace("/", DIRECTORY_SEPARATOR, $path); $arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path)); $tmpPath = ""; @@ -498,7 +502,7 @@ class UvDriver implements Driver { }); } else { $buffer = (yield $this->doFsRead($fh, $offset = 0, $stat["size"])); - if ($buffer === false ) { + if ($buffer === false) { \uv_fs_close($this->loop, $fh, function () use ($deferred) { $this->driver->unreference($this->busy); $deferred->fail(new FilesystemException( @@ -571,7 +575,7 @@ class UvDriver implements Driver { $deferred = new Deferred; $len = strlen($contents); \uv_fs_write($this->loop, $fh, $contents, $offset = 0, function ($fh, $result) use ($deferred, $len) { - \uv_fs_close($this->loop, $fh, function() use ($deferred, $result, $len) { + \uv_fs_close($this->loop, $fh, function () use ($deferred, $result, $len) { $this->driver->unreference($this->busy); if ($result < 0) { $deferred->fail(new FilesystemException( diff --git a/lib/UvHandle.php b/lib/UvHandle.php index c836b35..2560d5e 100644 --- a/lib/UvHandle.php +++ b/lib/UvHandle.php @@ -2,7 +2,10 @@ namespace Amp\File; -use Amp\{ Deferred, Loop, Promise, Success }; +use Amp\Deferred; +use Amp\Loop; +use Amp\Promise; +use Amp\Success; class UvHandle implements Handle { const OP_READ = 1; @@ -205,7 +208,7 @@ class UvHandle implements Handle { $this->isCloseInitialized = true; $this->driver->reference($this->busy); $deferred = new Deferred; - \uv_fs_close($this->loop, $this->fh, function($fh) use ($deferred) { + \uv_fs_close($this->loop, $this->fh, function ($fh) use ($deferred) { $this->driver->unreference($this->busy); $deferred->resolve(); }); diff --git a/lib/functions.php b/lib/functions.php index 58f9701..991f7c8 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -2,13 +2,14 @@ namespace Amp\File; -use Amp\{ Loop, Promise }; +use Amp\Loop; use Amp\Parallel\Worker\Worker; +use Amp\Promise; const LOOP_STATE_IDENTIFIER = Driver::class; /** - * Retrieve the application-wide filesystem instance + * Retrieve the application-wide filesystem instance. * * @param \Amp\File\Driver $driver Use the specified object as the application-wide filesystem instance * @return \Amp\File\Driver @@ -27,26 +28,26 @@ function filesystem(Driver $driver = null): Driver { } /** - * Create a new filesystem driver best-suited for the current environment + * Create a new filesystem driver best-suited for the current environment. * * @return \Amp\File\Driver */ function driver(): Driver { $driver = Loop::get(); - $loop = $driver->getHandle(); - if (\is_resource($loop) && \get_resource_type($loop) == "uv_loop") { + + if ($driver instanceof Loop\UvDriver) { return new UvDriver($driver); - } elseif (\extension_loaded("eio")) { - return new EioDriver; - } elseif (\interface_exists(Worker::class)) { - return new ParallelDriver; - } else { - return new BlockingDriver; } + + if (\extension_loaded("eio")) { + return new EioDriver; + } + + return new ParallelDriver; } /** - * Open a handle for the specified path + * Open a handle for the specified path. * * @param string $path * @param string $mode @@ -57,7 +58,7 @@ function open(string $path, string $mode): Promise { } /** - * Execute a file stat operation + * Execute a file stat operation. * * If the requested path does not exist the resulting Promise will resolve to NULL. * The returned Promise whould never resolve as a failure. @@ -123,7 +124,7 @@ function isfile(string $path): Promise { } /** - * Retrieve the path's last modification time as a unix timestamp + * Retrieve the path's last modification time as a unix timestamp. * * @param string $path An absolute file system path * @fails \Amp\Files\FilesystemException If the path does not exist @@ -134,7 +135,7 @@ function mtime(string $path): Promise { } /** - * Retrieve the path's last access time as a unix timestamp + * Retrieve the path's last access time as a unix timestamp. * * @param string $path An absolute file system path * @fails \Amp\Files\FilesystemException If the path does not exist @@ -145,7 +146,7 @@ function atime($path) { } /** - * Retrieve the path's creation time as a unix timestamp + * Retrieve the path's creation time as a unix timestamp. * * @param string $path An absolute file system path * @fails \Amp\Files\FilesystemException If the path does not exist @@ -156,7 +157,7 @@ function ctime(string $path): Promise { } /** - * Same as stat() except if the path is a link then the link's data is returned + * Same as stat() except if the path is a link then the link's data is returned. * * If the requested path does not exist the resulting Promise will resolve to NULL. * The returned Promise whould never resolve as a failure. @@ -169,7 +170,7 @@ function lstat(string $path): Promise { } /** - * Create a symlink $link pointing to the file/directory located at $original + * Create a symlink $link pointing to the file/directory located at $original. * * @param string $original * @param string $link @@ -181,7 +182,7 @@ function symlink(string $original, string $link): Promise { } /** - * Create a hard link $link pointing to the file/directory located at $original + * Create a hard link $link pointing to the file/directory located at $original. * * @param string $original * @param string $link @@ -193,7 +194,7 @@ function link(string $original, string $link): Promise { } /** - * Read the symlink at $path + * Read the symlink at $path. * * @param string $original * @param string $link @@ -205,7 +206,7 @@ function readlink(string $path): Promise { } /** - * Rename a file or directory + * Rename a file or directory. * * @param string $from * @param string $to @@ -217,7 +218,7 @@ function rename(string $from, string $to): Promise { } /** - * Delete a file + * Delete a file. * * @param string $path * @return \Amp\Promise @@ -227,7 +228,7 @@ function unlink(string $path): Promise { } /** - * Create a director + * Create a director. * * @param string $path * @param int $mode @@ -239,7 +240,7 @@ function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise } /** - * Delete a directory + * Delete a directory. * * @param string $path * @return \Amp\Promise @@ -249,7 +250,7 @@ function rmdir(string $path): Promise { } /** - * Retrieve an array of files and directories inside the specified path + * Retrieve an array of files and directories inside the specified path. * * Dot entries are not included in the resulting array (i.e. "." and ".."). * @@ -261,7 +262,7 @@ function scandir(string $path): Promise { } /** - * chmod a file or directory + * chmod a file or directory. * * @param string $path * @param int $mode @@ -272,7 +273,7 @@ function chmod(string $path, int $mode): Promise { } /** - * chown a file or directory + * chown a file or directory. * * @param string $path * @param int $uid -1 to ignore @@ -284,7 +285,7 @@ function chown(string $path, int $uid, int $gid = -1): Promise { } /** - * Update the access and modification time of the specified path + * Update the access and modification time of the specified path. * * If the file does not exist it will be created automatically. * @@ -296,7 +297,7 @@ function touch(string $path): Promise { } /** - * Buffer the specified file's contents + * Buffer the specified file's contents. * * @param string $path The file path from which to buffer contents * @return \Amp\Promise diff --git a/test/BlockingDriverTest.php b/test/BlockingDriverTest.php index ec1414c..2675f0c 100644 --- a/test/BlockingDriverTest.php +++ b/test/BlockingDriverTest.php @@ -4,7 +4,7 @@ namespace Amp\File\Test; class BlockingDriverTest extends DriverTest { protected function execute(callable $cb) { - \Amp\Loop::run(function() use ($cb) { + \Amp\Loop::run(function () use ($cb) { \Amp\File\filesystem(new \Amp\File\BlockingDriver); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); }); diff --git a/test/BlockingHandleTest.php b/test/BlockingHandleTest.php index ab03241..62b93f2 100644 --- a/test/BlockingHandleTest.php +++ b/test/BlockingHandleTest.php @@ -4,7 +4,7 @@ namespace Amp\File\Test; class BlockingHandleTest extends HandleTest { protected function execute(callable $cb) { - \Amp\Loop::run(function() use ($cb) { + \Amp\Loop::run(function () use ($cb) { \Amp\File\filesystem(new \Amp\File\BlockingDriver); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); }); diff --git a/test/DriverTest.php b/test/DriverTest.php index 809a080..451f097 100644 --- a/test/DriverTest.php +++ b/test/DriverTest.php @@ -64,7 +64,7 @@ abstract class DriverTest extends TestCase { $target = "{$fixtureDir}/small.txt"; $link = "{$fixtureDir}/symlink.txt"; $this->assertTrue(yield File\symlink($target, $link)); - $this->assertTrue(is_array(yield File\lstat($link))); + $this->assertInternalType('array', yield File\lstat($link)); yield File\unlink($link); }); } diff --git a/test/EioDriverTest.php b/test/EioDriverTest.php index 3eb335f..ef45151 100644 --- a/test/EioDriverTest.php +++ b/test/EioDriverTest.php @@ -5,7 +5,7 @@ namespace Amp\File\Test; class EioDriverTest extends DriverTest { protected function execute(callable $cb) { if (\extension_loaded("eio")) { - \Amp\Loop::run(function() use ($cb) { + \Amp\Loop::run(function () use ($cb) { \Amp\File\filesystem(new \Amp\File\EioDriver); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); }); diff --git a/test/EioHandleTest.php b/test/EioHandleTest.php index cf9ca57..9daef6a 100644 --- a/test/EioHandleTest.php +++ b/test/EioHandleTest.php @@ -7,7 +7,7 @@ use Amp\File as file; class EioHandleTest extends HandleTest { protected function execute(callable $cb) { if (\extension_loaded("eio")) { - \Amp\Loop::run(function() use ($cb) { + \Amp\Loop::run(function () use ($cb) { \Amp\File\filesystem(new \Amp\File\EioDriver); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); }); diff --git a/test/ParallelDriverTest.php b/test/ParallelDriverTest.php index 11e2cab..46c50fc 100644 --- a/test/ParallelDriverTest.php +++ b/test/ParallelDriverTest.php @@ -9,7 +9,7 @@ use function Amp\call; class ParallelDriverTest extends DriverTest { protected function execute(callable $cb) { - Loop::run(function() use ($cb) { + Loop::run(function () use ($cb) { $pool = new DefaultPool; $pool->start(); diff --git a/test/ParallelHandleTest.php b/test/ParallelHandleTest.php index 3a03432..b986d9d 100644 --- a/test/ParallelHandleTest.php +++ b/test/ParallelHandleTest.php @@ -9,7 +9,7 @@ use function Amp\call; class ParallelHandleTest extends HandleTest { protected function execute(callable $cb) { - Loop::run(function() use ($cb) { + Loop::run(function () use ($cb) { $pool = new DefaultPool; $pool->start(); diff --git a/test/UvDriverTest.php b/test/UvDriverTest.php index bae612b..5b08695 100644 --- a/test/UvDriverTest.php +++ b/test/UvDriverTest.php @@ -9,7 +9,7 @@ class UvDriverTest extends DriverTest { if (\extension_loaded("uv")) { $loop = new Loop\UvDriver; Loop::set($loop); - Loop::run(function() use ($cb, $loop) { + Loop::run(function () use ($cb, $loop) { \Amp\File\filesystem(new \Amp\File\UvDriver($loop)); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); }); diff --git a/test/UvHandleTest.php b/test/UvHandleTest.php index 0296573..9c21a17 100644 --- a/test/UvHandleTest.php +++ b/test/UvHandleTest.php @@ -2,15 +2,15 @@ namespace Amp\File\Test; -use Amp\Loop; use Amp\File as file; +use Amp\Loop; class UvHandleTest extends HandleTest { protected function execute(callable $cb) { if (\extension_loaded("uv")) { $loop = new Loop\UvDriver; Loop::set($loop); - Loop::run(function() use ($cb, $loop) { + Loop::run(function () use ($cb, $loop) { \Amp\File\filesystem(new \Amp\File\UvDriver($loop)); \Amp\Promise\rethrow(new \Amp\Coroutine($cb())); });