1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 11:54:54 +01:00

Fix code style

This commit is contained in:
Niklas Keller 2017-06-17 23:41:57 +02:00
parent 7a96e10a35
commit 62428ca0ef
25 changed files with 252 additions and 145 deletions

6
.gitignore vendored
View File

@ -1,3 +1,3 @@
composer.lock
vendor/
.idea
/composer.lock
/vendor
/.php_cs.cache

15
.php_cs
View File

@ -1,15 +0,0 @@
<?php
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::NONE_LEVEL)
->fixers([
"psr2",
"-braces",
"-psr0",
])
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__ . "/lib")
->in(__DIR__ . "/test")
)
;

39
.php_cs.dist Normal file
View File

@ -0,0 +1,39 @@
<?php
return PhpCsFixer\Config::create()
->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")
);

45
Makefile Normal file
View File

@ -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 $@

View File

@ -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,19 +61,23 @@ 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);
}
}
/**
* Does the specified path exist and is it a directory?
@ -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<int>
@ -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<int>
@ -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<int>
@ -158,6 +168,7 @@ class BlockingDriver implements Driver {
"Path does not exist"
));
}
$ctime = @\filectime($path);
\clearstatcache(true, $path);
@ -258,12 +269,12 @@ class BlockingDriver implements Driver {
}));
\clearstatcache(true, $path);
return new Success($arr);
} else {
}
return new Failure(new FilesystemException(
"Failed reading contents from {$path}"
));
}
}
/**
* {@inheritdoc}
@ -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);
}
}

View File

@ -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,14 +37,15 @@ 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"
));
}
}
/**
* {@inheritdoc}
@ -53,14 +56,15 @@ 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"
));
}
}
/**
* {@inheritdoc}
@ -84,12 +88,12 @@ class BlockingHandle implements Handle {
if (@\fclose($fh)) {
return new Success;
} else {
}
return new Failure(new FilesystemException(
"Failed closing file handle"
));
}
}
/**
* {@inheritdoc}

View File

@ -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<int>
@ -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<int>
@ -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<int>
@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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<string|null>
@ -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<int>
@ -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
*/

View File

@ -1,8 +1,11 @@
<?php
namespace Amp\File\Internal;
use Amp\File\{ BlockingDriver, BlockingHandle, FilesystemException };
use Amp\Parallel\Worker\{ Environment, Task };
use Amp\File\BlockingDriver;
use Amp\File\BlockingHandle;
use Amp\File\FilesystemException;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
/**
* @codeCoverageIgnore

View File

@ -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])));
}}
}
}

View File

@ -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 */

View File

@ -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 */

View File

@ -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;

View File

@ -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<null>
@ -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<null>
@ -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<string>

View File

@ -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);
});
}

View File

@ -2,8 +2,8 @@
namespace Amp\File\Test;
use Amp\Loop;
use Amp\File as file;
use Amp\Loop;
class UvHandleTest extends HandleTest {
protected function execute(callable $cb) {