1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00

Increase psalm error level to level 3

This commit is contained in:
Niklas Keller 2020-03-28 21:55:44 +01:00
parent 0e04422dcb
commit 885cd68828
9 changed files with 63 additions and 4 deletions

View File

@ -145,7 +145,10 @@ trait Placeholder
} }
try { try {
/** @var mixed $result */ /**
* @var mixed $result
* @psalm-suppress PossiblyInvalidMethodCall https://github.com/vimeo/psalm/issues/3033
*/
$result = $onResolved(null, $this->result); $result = $onResolved(null, $this->result);
$onResolved = null; // allow garbage collection of $onResolved, to catch any exceptions from destructors $onResolved = null; // allow garbage collection of $onResolved, to catch any exceptions from destructors

View File

@ -12,7 +12,7 @@ final class LazyPromise implements Promise
/** @var callable|null */ /** @var callable|null */
private $promisor; private $promisor;
/** @var \Amp\Promise|null */ /** @var Promise|null */
private $promise; private $promise;
/** /**
@ -30,11 +30,15 @@ final class LazyPromise implements Promise
public function onResolve(callable $onResolved) public function onResolve(callable $onResolved)
{ {
if ($this->promise === null) { if ($this->promise === null) {
\assert($this->promisor !== null);
$provider = $this->promisor; $provider = $this->promisor;
$this->promisor = null; $this->promisor = null;
$this->promise = call($provider); $this->promise = call($provider);
} }
\assert($this->promise !== null);
$this->promise->onResolve($onResolved); $this->promise->onResolve($onResolved);
} }
} }

View File

@ -182,6 +182,7 @@ abstract class Driver
*/ */
public function defer(callable $callback, $data = null): string public function defer(callable $callback, $data = null): string
{ {
/** @psalm-var Watcher<null> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::DEFER; $watcher->type = Watcher::DEFER;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
@ -216,10 +217,12 @@ abstract class Driver
throw new \Error("Delay must be greater than or equal to zero"); throw new \Error("Delay must be greater than or equal to zero");
} }
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::DELAY; $watcher->type = Watcher::DELAY;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
$watcher->callback = $callback; $watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $delay; $watcher->value = $delay;
$watcher->data = $data; $watcher->data = $data;
@ -251,10 +254,12 @@ abstract class Driver
throw new \Error("Interval must be greater than or equal to zero"); throw new \Error("Interval must be greater than or equal to zero");
} }
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::REPEAT; $watcher->type = Watcher::REPEAT;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
$watcher->callback = $callback; $watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $interval; $watcher->value = $interval;
$watcher->data = $data; $watcher->data = $data;
@ -285,10 +290,12 @@ abstract class Driver
*/ */
public function onReadable($stream, callable $callback, $data = null): string public function onReadable($stream, callable $callback, $data = null): string
{ {
/** @psalm-var Watcher<resource> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::READABLE; $watcher->type = Watcher::READABLE;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
$watcher->callback = $callback; $watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $stream; $watcher->value = $stream;
$watcher->data = $data; $watcher->data = $data;
@ -319,10 +326,12 @@ abstract class Driver
*/ */
public function onWritable($stream, callable $callback, $data = null): string public function onWritable($stream, callable $callback, $data = null): string
{ {
/** @psalm-var Watcher<resource> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::WRITABLE; $watcher->type = Watcher::WRITABLE;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
$watcher->callback = $callback; $watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $stream; $watcher->value = $stream;
$watcher->data = $data; $watcher->data = $data;
@ -354,10 +363,12 @@ abstract class Driver
*/ */
public function onSignal(int $signo, callable $callback, $data = null): string public function onSignal(int $signo, callable $callback, $data = null): string
{ {
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher; $watcher = new Watcher;
$watcher->type = Watcher::SIGNAL; $watcher->type = Watcher::SIGNAL;
$watcher->id = $this->nextId++; $watcher->id = $this->nextId++;
$watcher->callback = $callback; $watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $signo; $watcher->value = $signo;
$watcher->data = $data; $watcher->data = $data;

View File

@ -157,6 +157,8 @@ class EvDriver extends Driver
{ {
$active = self::$activeSignals; $active = self::$activeSignals;
\assert($active !== null);
foreach ($active as $event) { foreach ($active as $event) {
$event->stop(); $event->stop();
} }
@ -230,10 +232,14 @@ class EvDriver extends Driver
if (!isset($this->events[$id = $watcher->id])) { if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) { switch ($watcher->type) {
case Watcher::READABLE: case Watcher::READABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher); $this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
break; break;
case Watcher::WRITABLE: case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = $this->handle->io( $this->events[$id] = $this->handle->io(
$watcher->value, $watcher->value,
\Ev::WRITE, \Ev::WRITE,
@ -244,6 +250,8 @@ class EvDriver extends Driver
case Watcher::DELAY: case Watcher::DELAY:
case Watcher::REPEAT: case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$interval = $watcher->value / self::MILLISEC_PER_SEC; $interval = $watcher->value / self::MILLISEC_PER_SEC;
$this->events[$id] = $this->handle->timer( $this->events[$id] = $this->handle->timer(
$interval, $interval,
@ -254,6 +262,8 @@ class EvDriver extends Driver
break; break;
case Watcher::SIGNAL: case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher); $this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
break; break;
@ -267,6 +277,7 @@ class EvDriver extends Driver
} }
if ($watcher->type === Watcher::SIGNAL) { if ($watcher->type === Watcher::SIGNAL) {
/** @psalm-suppress PropertyTypeCoercion */
$this->signals[$id] = $this->events[$id]; $this->signals[$id] = $this->events[$id];
} }
} }

View File

@ -53,6 +53,8 @@ class EventDriver extends Driver
} }
$this->ioCallback = function ($resource, $what, Watcher $watcher) { $this->ioCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_resource($watcher->value));
try { try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data); $result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
@ -73,6 +75,8 @@ class EventDriver extends Driver
}; };
$this->timerCallback = function ($resource, $what, Watcher $watcher) { $this->timerCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_int($watcher->value));
if ($watcher->type & Watcher::DELAY) { if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id); $this->cancel($watcher->id);
} else { } else {
@ -167,6 +171,8 @@ class EventDriver extends Driver
{ {
$active = self::$activeSignals; $active = self::$activeSignals;
\assert($active !== null);
foreach ($active as $event) { foreach ($active as $event) {
$event->del(); $event->del();
} }
@ -244,6 +250,8 @@ class EventDriver extends Driver
if (!isset($this->events[$id = $watcher->id])) { if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) { switch ($watcher->type) {
case Watcher::READABLE: case Watcher::READABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = new \Event( $this->events[$id] = new \Event(
$this->handle, $this->handle,
$watcher->value, $watcher->value,
@ -254,6 +262,8 @@ class EventDriver extends Driver
break; break;
case Watcher::WRITABLE: case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = new \Event( $this->events[$id] = new \Event(
$this->handle, $this->handle,
$watcher->value, $watcher->value,
@ -265,6 +275,8 @@ class EventDriver extends Driver
case Watcher::DELAY: case Watcher::DELAY:
case Watcher::REPEAT: case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$this->events[$id] = new \Event( $this->events[$id] = new \Event(
$this->handle, $this->handle,
-1, -1,
@ -275,6 +287,8 @@ class EventDriver extends Driver
break; break;
case Watcher::SIGNAL: case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
$this->events[$id] = new \Event( $this->events[$id] = new \Event(
$this->handle, $this->handle,
$watcher->value, $watcher->value,
@ -294,6 +308,8 @@ class EventDriver extends Driver
switch ($watcher->type) { switch ($watcher->type) {
case Watcher::DELAY: case Watcher::DELAY:
case Watcher::REPEAT: case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$interval = $watcher->value - ($now - $this->now()); $interval = $watcher->value - ($now - $this->now());
$this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0); $this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0);
break; break;

View File

@ -21,6 +21,8 @@ final class TimerQueue
* @param Watcher $watcher * @param Watcher $watcher
* @param int $expiration * @param int $expiration
* *
* @psalm-param Watcher<int> $watcher
*
* @return void * @return void
*/ */
public function insert(Watcher $watcher, int $expiration) public function insert(Watcher $watcher, int $expiration)
@ -32,6 +34,8 @@ final class TimerQueue
$this->pointers[$watcher->id] = $node; $this->pointers[$watcher->id] = $node;
while ($node !== 0 && $entry->expiration < $this->data[$parent = ($node - 1) >> 1]->expiration) { while ($node !== 0 && $entry->expiration < $this->data[$parent = ($node - 1) >> 1]->expiration) {
\assert(isset($parent)); // see https://github.com/vimeo/psalm/issues/3034
$temp = $this->data[$parent]; $temp = $this->data[$parent];
$this->data[$node] = $temp; $this->data[$node] = $temp;
$this->pointers[$temp->watcher->id] = $node; $this->pointers[$temp->watcher->id] = $node;
@ -48,6 +52,8 @@ final class TimerQueue
* *
* @param Watcher $watcher * @param Watcher $watcher
* *
* @psalm-param Watcher<int> $watcher
*
* @return void * @return void
*/ */
public function remove(Watcher $watcher) public function remove(Watcher $watcher)
@ -68,6 +74,8 @@ final class TimerQueue
* @param int $now Current loop time. * @param int $now Current loop time.
* *
* @return Watcher|null Expired watcher at the top of the heap or null if the watcher has not expired. * @return Watcher|null Expired watcher at the top of the heap or null if the watcher has not expired.
*
* @psalm-return Watcher<int>|null
*/ */
public function extract(int $now) public function extract(int $now)
{ {

View File

@ -217,6 +217,8 @@ class NativeDriver extends Driver
} }
} }
\assert(\is_array($write)); // See https://github.com/vimeo/psalm/issues/3036
foreach ($write as $stream) { foreach ($write as $stream) {
$streamId = (int) $stream; $streamId = (int) $stream;
if (!isset($this->writeWatchers[$streamId])) { if (!isset($this->writeWatchers[$streamId])) {

View File

@ -4,6 +4,9 @@ namespace Amp\Loop;
use Amp\Struct; use Amp\Struct;
/**
* @template TValue as (int|resource|null)
*/
class Watcher class Watcher
{ {
use Struct; use Struct;
@ -42,7 +45,8 @@ class Watcher
/** /**
* Watcher-dependent value storage. Stream for IO watchers, signal number for signal watchers, interval for timers. * Watcher-dependent value storage. Stream for IO watchers, signal number for signal watchers, interval for timers.
* *
* @var resource|int * @var resource|int|null
* @psalm-var TValue
*/ */
public $value; public $value;
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<psalm <psalm
totallyTyped="false" totallyTyped="false"
errorLevel="4" errorLevel="3"
phpVersion="7.0" phpVersion="7.0"
resolveFromConfigFile="true" resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"