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

Automatically add \ for buitins (#186)

Fixes #185.
This commit is contained in:
Markus Staab 2017-11-29 13:36:50 +01:00 committed by Niklas Keller
parent a8cb7f264e
commit 34bf671f13
14 changed files with 91 additions and 90 deletions

View File

@ -13,6 +13,7 @@ return PhpCsFixer\Config::create()
"cast_spaces" => true,
"combine_consecutive_unsets" => true,
"function_to_constant" => true,
"native_function_invocation" => true,
"no_multiline_whitespace_before_semicolons" => true,
"no_unused_imports" => true,
"no_useless_else" => true,

View File

@ -5,17 +5,17 @@ require __DIR__ . '/../../vendor/autoload.php';
use Amp\Loop;
if (stream_set_blocking(STDIN, false) !== true) {
fwrite(STDERR, "Unable to set STDIN to non-blocking" . PHP_EOL);
if (\stream_set_blocking(STDIN, false) !== true) {
\fwrite(STDERR, "Unable to set STDIN to non-blocking" . PHP_EOL);
exit(1);
}
print "Write something and hit enter" . PHP_EOL;
Loop::onReadable(STDIN, function ($watcher, $stream) {
$chunk = fread($stream, 8192);
$chunk = \fread($stream, 8192);
print "Read " . strlen($chunk) . " bytes" . PHP_EOL;
print "Read " . \strlen($chunk) . " bytes" . PHP_EOL;
});
Loop::run();

View File

@ -30,10 +30,10 @@ Loop::run(function () {
asyncCall($generator, $emitter);
while (yield $iterator->advance()) {
printf("Emitter emitted %d\n", $iterator->getCurrent());
\printf("Emitter emitted %d\n", $iterator->getCurrent());
yield new Delayed(500); // Listener consumption takes 500 ms.
}
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
\printf("Exception: %s\n", $exception);
}
});

View File

@ -29,10 +29,10 @@ Loop::run(function () {
$iterator = $emitter->iterate();
while (yield $iterator->advance()) {
printf("Emitter emitted %d\n", $iterator->getCurrent());
\printf("Emitter emitted %d\n", $iterator->getCurrent());
yield new Delayed(100); // Listener consumption takes 100 ms.
}
} catch (\Throwable $exception) {
printf("Exception: %s\n", $exception);
\printf("Exception: %s\n", $exception);
}
});

View File

@ -24,10 +24,10 @@ Loop::run(function () {
});
while (yield $iterator->advance()) {
printf("Producer emitted %d\n", $iterator->getCurrent());
\printf("Producer emitted %d\n", $iterator->getCurrent());
yield new Delayed(100); // Listener consumption takes 100 ms.
}
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
\printf("Exception: %s\n", $exception);
}
});

View File

@ -13,7 +13,7 @@ namespace Amp\Internal;
* @internal
*/
function formatStacktrace(array $trace): string {
return implode("\n", array_map(function ($e, $i) {
return \implode("\n", \array_map(function ($e, $i) {
$line = "#{$i} {$e['file']}:{$e['line']} ";
if ($e["type"]) {
@ -21,7 +21,7 @@ function formatStacktrace(array $trace): string {
}
return $line . $e["function"] . "()";
}, $trace, array_keys($trace)));
}, $trace, \array_keys($trace)));
}
/**

View File

@ -40,10 +40,10 @@ class AdaptTest extends \PHPUnit\Framework\TestCase {
->method("then")
->with(
$this->callback(function ($resolve) {
return is_callable($resolve);
return \is_callable($resolve);
}),
$this->callback(function ($reject) {
return is_callable($reject);
return \is_callable($reject);
})
);

View File

@ -9,7 +9,7 @@ class DelayedTest extends \PHPUnit\Framework\TestCase {
public function testDelayed() {
$time = 100;
$value = "test";
$start = microtime(true);
$start = \microtime(true);
Loop::run(function () use (&$result, $time, $value) {
$promise = new Delayed($time, $value);
@ -21,14 +21,14 @@ class DelayedTest extends \PHPUnit\Framework\TestCase {
$promise->onResolve($callback);
});
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (microtime(true) - $start) * 1000);
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertSame($value, $result);
}
public function testUnreference() {
$time = 100;
$value = "test";
$start = microtime(true);
$start = \microtime(true);
$invoked = false;
Loop::run(function () use (&$invoked, $time, $value) {
@ -42,7 +42,7 @@ class DelayedTest extends \PHPUnit\Framework\TestCase {
$promise->onResolve($callback);
});
$this->assertLessThanOrEqual($time - 1 /* 1ms grace period */, (microtime(true) - $start) * 1000);
$this->assertLessThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertFalse($invoked);
}
@ -52,7 +52,7 @@ class DelayedTest extends \PHPUnit\Framework\TestCase {
public function testReference() {
$time = 100;
$value = "test";
$start = microtime(true);
$start = \microtime(true);
$invoked = false;
Loop::run(function () use (&$invoked, $time, $value) {
@ -67,7 +67,7 @@ class DelayedTest extends \PHPUnit\Framework\TestCase {
$promise->onResolve($callback);
});
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (microtime(true) - $start) * 1000);
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertTrue($invoked);
}
}

View File

@ -107,7 +107,7 @@ class IteratorFromIterableTest extends \PHPUnit\Framework\TestCase {
public function testInterval() {
Loop::run(function () {
$count = 3;
$iterator = Iterator\fromIterable(range(1, $count), self::TIMEOUT);
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
$i = 0;
while (yield $iterator->advance()) {
@ -124,7 +124,7 @@ class IteratorFromIterableTest extends \PHPUnit\Framework\TestCase {
public function testSlowConsumer() {
$count = 5;
Loop::run(function () use ($count) {
$iterator = Iterator\fromIterable(range(1, $count), self::TIMEOUT);
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
for ($i = 0; yield $iterator->advance(); ++$i) {
yield new Delayed(self::TIMEOUT * 2);

View File

@ -12,15 +12,15 @@ use Amp\Loop\UnsupportedFeatureException;
use PHPUnit\Framework\TestCase;
use React\Promise\RejectedPromise as RejectedReactPromise;
if (!defined("SIGUSR1")) {
define("SIGUSR1", 30);
if (!\defined("SIGUSR1")) {
\define("SIGUSR1", 30);
}
if (!defined("SIGUSR2")) {
define("SIGUSR2", 31);
if (!\defined("SIGUSR2")) {
\define("SIGUSR2", 31);
}
if (!defined("PHP_INT_MIN")) {
define("PHP_INT_MIN", ~PHP_INT_MAX);
if (!\defined("PHP_INT_MIN")) {
\define("PHP_INT_MIN", ~PHP_INT_MAX);
}
abstract class DriverTest extends TestCase {
@ -43,7 +43,7 @@ abstract class DriverTest extends TestCase {
// Required for error handler to work
Loop::set($this->loop);
gc_collect_cycles();
\gc_collect_cycles();
}
public function tearDown() {
@ -226,7 +226,7 @@ abstract class DriverTest extends TestCase {
$invoked = true;
});
usleep(1000 * 10);
\usleep(1000 * 10);
});
$this->assertTrue($invoked);
}
@ -276,15 +276,15 @@ abstract class DriverTest extends TestCase {
$this->start(function (Driver $loop) use ($type, $args, &$invoked) {
if ($type == "onReadable") {
$ends = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($ends[0], "trigger readability watcher");
$ends = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
\fwrite($ends[0], "trigger readability watcher");
$args = [$ends[1]];
} else {
array_pop($args);
\array_pop($args);
}
$expectedData = 20.75;
if (substr($type, 0, 2) == "on") {
if (\substr($type, 0, 2) == "on") {
$args[] = function ($watcherId, $arg, int $data) use ($loop, &$invoked, $expectedData) {
$invoked = true;
$this->assertSame((int) $expectedData, $data);
@ -300,7 +300,7 @@ abstract class DriverTest extends TestCase {
};
}
$args[] = $expectedData;
call_user_func_array([$loop, $type], $args);
\call_user_func_array([$loop, $type], $args);
if ($type == "onSignal") {
$loop->defer(function () {
@ -341,8 +341,8 @@ abstract class DriverTest extends TestCase {
$loop = $this->loop;
$func = [$loop, $type];
if (substr($type, 0, 2) === "on") {
$type = "on_" . lcfirst(substr($type, 2));
if (\substr($type, 0, 2) === "on") {
$type = "on_" . \lcfirst(\substr($type, 2));
}
// being referenced is the default
@ -407,8 +407,8 @@ abstract class DriverTest extends TestCase {
$loop = $this->loop;
$func = [$loop, $type];
if (substr($type, 0, 2) === "on") {
$type = "on_" . lcfirst(substr($type, 2));
if (\substr($type, 0, 2) === "on") {
$type = "on_" . \lcfirst(\substr($type, 2));
}
$watcherId = \call_user_func_array($func, $args);
@ -478,7 +478,7 @@ abstract class DriverTest extends TestCase {
}
$this->start(function (Driver $loop) use ($type, $args, $runs) {
$initialMem = memory_get_usage();
$initialMem = \memory_get_usage();
$cb = function ($runs) use ($loop, $type, $args) {
$func = [$loop, $type];
for ($watchers = [], $i = 0; $i < $runs; $i++) {
@ -541,11 +541,11 @@ abstract class DriverTest extends TestCase {
$fn = function ($watcherId, $socket, $i) use (&$fn, $loop) {
$loop->cancel($watcherId);
if ($socket) {
fwrite($socket, ".");
\fwrite($socket, ".");
}
if ($i) {
// explicitly use *different* streams with *different* resource ids
$ends = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$ends = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$loop->onWritable($ends[0], $fn, --$i);
$loop->onReadable($ends[1], function ($watcherId) use ($loop) {
$loop->cancel($watcherId);
@ -571,15 +571,15 @@ abstract class DriverTest extends TestCase {
$loop->run();
}
};
$closureMem = memory_get_usage() - $initialMem;
$closureMem = \memory_get_usage() - $initialMem;
$cb($runs); /* just to set up eventual structures inside loop without counting towards memory comparison */
gc_collect_cycles();
$initialMem = memory_get_usage() - $closureMem;
\gc_collect_cycles();
$initialMem = \memory_get_usage() - $closureMem;
$cb($runs);
unset($cb);
gc_collect_cycles();
$endMem = memory_get_usage();
\gc_collect_cycles();
$endMem = \memory_get_usage();
/* this is allowing some memory usage due to runtime caches etc., but nothing actually leaking */
$this->assertLessThan($runs * 4, $endMem - $initialMem); // * 4, as 4 is minimal sizeof(void *)
@ -591,19 +591,19 @@ abstract class DriverTest extends TestCase {
* indicates the order within the tick.
*/
public function testExecutionOrderGuarantees() {
$this->expectOutputString("01 02 03 04 " . str_repeat("05 ", 8) . "10 11 12 " . str_repeat("13 ", 4) . "20 " . str_repeat("21 ", 4) . "30 40 41 ");
$this->expectOutputString("01 02 03 04 " . \str_repeat("05 ", 8) . "10 11 12 " . \str_repeat("13 ", 4) . "20 " . \str_repeat("21 ", 4) . "30 40 41 ");
$this->start(function (Driver $loop) use (&$ticks) {
// Wrap in extra defer, so driver creation time doesn't count for timers, as timers are driver creation
// relative instead of last tick relative before first tick.
$loop->defer(function () use ($loop, &$ticks) {
$f = function () use ($loop) {
$args = func_get_args();
$args = \func_get_args();
return function ($watcherId) use ($loop, &$args) {
if (!$args) {
$this->fail("Watcher callback called too often");
}
$loop->cancel($watcherId);
echo array_shift($args) . array_shift($args), " ";
echo \array_shift($args) . \array_shift($args), " ";
};
};
@ -673,7 +673,7 @@ abstract class DriverTest extends TestCase {
$loop->defer(function () use ($loop, $del5, $f) {
$loop->enable($del5);
$loop->defer(function () use ($loop, $f) {
usleep(700000); // to have $msDelay == 500 and $msDelay == 600 run at the same tick (but not $msDelay == 150)
\usleep(700000); // to have $msDelay == 500 and $msDelay == 600 run at the same tick (but not $msDelay == 150)
$loop->defer(function () use ($loop, $f) {
$loop->defer($f(4, 0));
});
@ -1237,7 +1237,7 @@ abstract class DriverTest extends TestCase {
}
public function testLoopStopPreventsTimerExecution() {
$t = microtime(1);
$t = \microtime(1);
$this->start(function (Driver $loop) {
$loop->delay($msDelay = 10000, function () {
$this->fail("Timer was executed despite stopped loop");
@ -1246,7 +1246,7 @@ abstract class DriverTest extends TestCase {
$loop->stop();
});
});
$this->assertTrue($t + 0.1 > microtime(1));
$this->assertTrue($t + 0.1 > \microtime(1));
}
public function testDeferEnabledInNextTick() {
@ -1349,8 +1349,8 @@ abstract class DriverTest extends TestCase {
break;
case "onReadable":
$ends = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($ends[0], "trigger readability watcher");
$ends = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
\fwrite($ends[0], "trigger readability watcher");
$args[] = $ends[1];
break;
@ -1372,7 +1372,7 @@ abstract class DriverTest extends TestCase {
};
}
call_user_func_array([$this->loop, $watcher], $args);
\call_user_func_array([$this->loop, $watcher], $args);
if ($watcher == "onSignal") {
$this->loop->delay(100, function () {
@ -1391,7 +1391,7 @@ abstract class DriverTest extends TestCase {
}
public function testMultipleWatchersOnSameDescriptor() {
$sockets = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$sockets = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
\fwrite($sockets[1], "testing");
$invoked = 0;
@ -1489,11 +1489,11 @@ abstract class DriverTest extends TestCase {
$emits = 3;
$this->loop->defer(function () use (&$time, $emits) {
$time = microtime(true);
$time = \microtime(true);
for ($i = 0; $i < $emits; ++$i) {
yield new Delayed(100);
}
$time = microtime(true) - $time;
$time = \microtime(true) - $time;
});
$this->loop->run();

View File

@ -2,7 +2,7 @@
Test order of destruction not interfering with access to UV handles
--SKIPIF--
<?php
extension_loaded("uv") or die("SKIP: ext/uv required for this test");
\extension_loaded("uv") or die("SKIP: ext/uv required for this test");
?>
--FILE--
<?php

View File

@ -20,8 +20,8 @@ class LoopTest extends TestCase {
public function testOnReadable() {
Loop::run(function () {
$ends = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($ends[0], "trigger readability watcher");
$ends = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
\fwrite($ends[0], "trigger readability watcher");
Loop::onReadable($ends[1], function () {
$this->assertTrue(true);

View File

@ -82,11 +82,11 @@ class ProducerTest extends TestCase {
$emits = 3;
Loop::run(function () use (&$time, $emits) {
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
$time = microtime(true);
$time = \microtime(true);
for ($i = 0; $i < $emits; ++$i) {
yield $emit($i);
}
$time = microtime(true) - $time;
$time = \microtime(true) - $time;
});
while (yield $producer->advance()) {

View File

@ -77,7 +77,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, $succeeder) = $this->promise();
$succeeder($value);
$promise->onResolve(function ($e, $v) use (&$invoked, $value) {
$this->assertSame(null, $e);
$this->assertNull($e);
$this->assertSame($value, $v);
$invoked = true;
});
@ -89,26 +89,26 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
$invoked = 0;
$promise->onResolve(function ($e, $v) use (&$invoked) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
});
$promise->onResolve(function ($e, $v) use (&$invoked) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
});
$succeeder(true);
$promise->onResolve(function ($e, $v) use (&$invoked) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
});
$promise->onResolve(function ($e, $v) use (&$invoked) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
});
@ -118,7 +118,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
public function testPromiseExceptionFailure() {
list($promise, , $failer) = $this->promise();
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked = true;
});
$failer(new \RuntimeException);
@ -129,7 +129,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, , $failer) = $this->promise();
$failer(new \RuntimeException);
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked = true;
});
$this->assertTrue($invoked);
@ -140,22 +140,22 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
$invoked = 0;
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked++;
});
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked++;
});
$failer(new \RuntimeException);
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked++;
});
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "RuntimeException");
$this->assertSame(\get_class($e), "RuntimeException");
$invoked++;
});
@ -169,7 +169,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, , $failer) = $this->promise();
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "Error");
$this->assertSame(\get_class($e), "Error");
$invoked = true;
});
$failer(new \Error);
@ -184,7 +184,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, , $failer) = $this->promise();
$failer(new \Error);
$promise->onResolve(function ($e) use (&$invoked) {
$this->assertSame(get_class($e), "Error");
$this->assertSame(\get_class($e), "Error");
$invoked = true;
});
$this->assertTrue($invoked);
@ -225,8 +225,8 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, $succeeder) = $this->promise();
$succeeder(true);
$promise->onResolve(function ($e, $v) use (&$invoked, $promise) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
throw new \Exception;
@ -234,8 +234,8 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, $succeeder) = $this->promise();
$promise->onResolve(function ($e, $v) use (&$invoked, $promise) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
throw new \Exception;
@ -256,15 +256,15 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
list($promise, $succeeder) = $this->promise();
$promise->onResolve(function ($e, $v) use (&$invoked, $promise) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
throw new \Exception;
});
$promise->onResolve(function ($e, $v) use (&$invoked, $promise) {
$this->assertSame(null, $e);
$this->assertSame(true, $v);
$this->assertNull($e);
$this->assertTrue($v);
$invoked++;
});
$succeeder(true);
@ -337,7 +337,7 @@ class PromiseTest extends \PHPUnit\Framework\TestCase {
$promise->onResolve(function () { });
$promise->onResolve(function () use (&$invoked) {
$invoked = true;
$this->assertLessThan(30, count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
$this->assertLessThan(30, \count(\debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
});
$last = $promise;