Skip to content

Commit

Permalink
Apply PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 21, 2018
1 parent c05f1a9 commit 5efddbc
Show file tree
Hide file tree
Showing 29 changed files with 308 additions and 196 deletions.
46 changes: 8 additions & 38 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
<?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__ . "/examples")
->in(__DIR__ . "/lib")
->in(__DIR__ . "/test")
);
$config = new Amp\CodeStyle\Config();
$config->getFinder()->in(__DIR__);

$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"require-dev": {
"amphp/phpunit-util": "^1",
"phpunit/phpunit": "^6",
"friendsofphp/php-cs-fixer": "^2.3"
"friendsofphp/php-cs-fixer": "^2.3",
"amphp/php-cs-fixer-config": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 11 additions & 11 deletions examples/benchmark-throughput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

Loop::set(new Loop\NativeDriver());

$args = getopt('i:o:t:');
$args = \getopt('i:o:t:');
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
$of = isset($args['o']) ? $args['o'] : '/dev/null';
$t = isset($args['t']) ? $args['t'] : 30;

// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
$if = preg_replace('(^/dev/fd/)', 'php://fd/', $if);
$of = preg_replace('(^/dev/fd/)', 'php://fd/', $of);
$if = \preg_replace('(^/dev/fd/)', 'php://fd/', $if);
$of = \preg_replace('(^/dev/fd/)', 'php://fd/', $of);

$stderr = new ResourceOutputStream(STDERR);
$in = new ResourceInputStream(fopen($if, 'r'), 65536 /* Default size used by React to allow comparisons */);
$out = new ResourceOutputStream(fopen($of, 'w'));
$in = new ResourceInputStream(\fopen($if, 'r'), 65536 /* Default size used by React to allow comparisons */);
$out = new ResourceOutputStream(\fopen($of, 'w'));

if (extension_loaded('xdebug')) {
if (\extension_loaded('xdebug')) {
$stderr->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
}

Expand All @@ -41,16 +41,16 @@
Loop::delay($t * 1000, [$in, "close"]);

Loop::run(function () use ($stderr, $in, $out) {
$start = microtime(true);
$start = \microtime(true);

while (($chunk = yield $in->read()) !== null) {
yield $out->write($chunk);
}

$t = microtime(true) - $start;
$t = \microtime(true) - $start;

$bytes = ftell($out->getResource());
$bytes = \ftell($out->getResource());

$stderr->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
$stderr->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
$stderr->write('read ' . $bytes . ' byte(s) in ' . \round($t, 3) . ' second(s) => ' . \round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
$stderr->write('peak memory usage of ' . \round(\memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
});
3 changes: 2 additions & 1 deletion lib/ClosedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

namespace Amp\ByteStream;

final class ClosedException extends StreamException {
final class ClosedException extends StreamException
{
}
9 changes: 6 additions & 3 deletions lib/InMemoryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
/**
* Input stream with a single already known data chunk.
*/
final class InMemoryStream implements InputStream {
final class InMemoryStream implements InputStream
{
private $contents;

/**
* @param string|null $contents Data chunk or `null` for no data chunk.
*/
public function __construct(string $contents = null) {
public function __construct(string $contents = null)
{
$this->contents = $contents;
}

Expand All @@ -23,7 +25,8 @@ public function __construct(string $contents = null) {
*
* @return Promise Resolves with the full contents or `null` if the stream has closed / already been consumed.
*/
public function read(): Promise {
public function read(): Promise
{
if ($this->contents === null) {
return new Success;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/InputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
* }
* ```
*/
interface InputStream {
interface InputStream
{
/**
* Reads data from the stream.
*
Expand Down
9 changes: 6 additions & 3 deletions lib/IteratorStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
use Amp\Iterator;
use Amp\Promise;

final class IteratorStream implements InputStream {
final class IteratorStream implements InputStream
{
private $iterator;
private $exception;
private $pending = false;

public function __construct(Iterator $iterator) {
public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}

/** @inheritdoc */
public function read(): Promise {
public function read(): Promise
{
if ($this->exception) {
return new Failure($this->exception);
}
Expand Down
18 changes: 12 additions & 6 deletions lib/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
*
* @deprecated Use Amp\ByteStream\Payload instead.
*/
class Message implements InputStream, Promise {
class Message implements InputStream, Promise
{
/** @var InputStream */
private $source;

Expand Down Expand Up @@ -58,11 +59,13 @@ class Message implements InputStream, Promise {
/**
* @param InputStream $source An iterator that only emits strings.
*/
public function __construct(InputStream $source) {
public function __construct(InputStream $source)
{
$this->source = $source;
}

private function consume(): \Generator {
private function consume(): \Generator
{
while (($chunk = yield $this->source->read()) !== null) {
$buffer = $this->buffer .= $chunk;

Expand Down Expand Up @@ -94,7 +97,8 @@ private function consume(): \Generator {
}

/** @inheritdoc */
final public function read(): Promise {
final public function read(): Promise
{
if ($this->pendingRead) {
throw new PendingReadError;
}
Expand Down Expand Up @@ -140,7 +144,8 @@ final public function read(): Promise {
}

/** @inheritdoc */
final public function onResolve(callable $onResolved) {
final public function onResolve(callable $onResolved)
{
$this->buffering = true;

if ($this->coroutine === null) {
Expand All @@ -164,7 +169,8 @@ final public function onResolve(callable $onResolved) {
*
* @return InputStream
*/
final public function getInputStream(): InputStream {
final public function getInputStream(): InputStream
{
return $this->source;
}
}
15 changes: 10 additions & 5 deletions lib/OutputBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Amp\Promise;
use Amp\Success;

class OutputBuffer implements OutputStream, Promise {
class OutputBuffer implements OutputStream, Promise
{
/** @var \Amp\Deferred|null */
private $deferred;

Expand All @@ -15,11 +16,13 @@ class OutputBuffer implements OutputStream, Promise {

private $closed = false;

public function __construct() {
public function __construct()
{
$this->deferred = new Deferred;
}

public function write(string $data): Promise {
public function write(string $data): Promise
{
if ($this->closed) {
throw new ClosedException("The stream has already been closed.");
}
Expand All @@ -29,7 +32,8 @@ public function write(string $data): Promise {
return new Success(\strlen($data));
}

public function end(string $finalData = ""): Promise {
public function end(string $finalData = ""): Promise
{
if ($this->closed) {
throw new ClosedException("The stream has already been closed.");
}
Expand All @@ -43,7 +47,8 @@ public function end(string $finalData = ""): Promise {
return new Success(\strlen($finalData));
}

public function onResolve(callable $onResolved) {
public function onResolve(callable $onResolved)
{
$this->deferred->promise()->onResolve($onResolved);
}
}
3 changes: 2 additions & 1 deletion lib/OutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
/**
* An `OutputStream` allows writing data in chunks. Writers can wait on the returned promises to feel the backpressure.
*/
interface OutputStream {
interface OutputStream
{
/**
* Writes data to the stream.
*
Expand Down
18 changes: 12 additions & 6 deletions lib/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* be buffered and accessed in its entirety by calling buffer(). Once buffering is requested through buffer(), the
* stream cannot be read in chunks. On destruct any remaining data is read from the InputStream given to this class.
*/
class Payload implements InputStream {
class Payload implements InputStream
{
/** @var InputStream */
private $stream;

Expand All @@ -24,17 +25,20 @@ class Payload implements InputStream {
/**
* @param \Amp\ByteStream\InputStream $stream
*/
public function __construct(InputStream $stream) {
public function __construct(InputStream $stream)
{
$this->stream = $stream;
}

public function __destruct() {
public function __destruct()
{
if (!$this->promise) {
Promise\rethrow(new Coroutine($this->consume()));
}
}

private function consume(): \Generator {
private function consume(): \Generator
{
try {
if ($this->lastRead && null === yield $this->lastRead) {
return;
Expand All @@ -53,7 +57,8 @@ private function consume(): \Generator {
*
* @throws \Error If a buffered message was requested by calling buffer().
*/
final public function read(): Promise {
final public function read(): Promise
{
if ($this->promise) {
throw new \Error("Cannot stream message data once a buffered message has been requested");
}
Expand All @@ -66,7 +71,8 @@ final public function read(): Promise {
*
* @return Promise<string> Resolves with the entire message contents.
*/
final public function buffer(): Promise {
final public function buffer(): Promise
{
if ($this->promise) {
return $this->promise;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/PendingReadError.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* Thrown in case a second read operation is attempted while another read operation is still pending.
*/
final class PendingReadError extends \Error {
final class PendingReadError extends \Error
{
public function __construct(
string $message = "The previous read operation must complete before read can be called again",
int $code = 0,
Expand Down
Loading

0 comments on commit 5efddbc

Please sign in to comment.