Skip to content

Commit

Permalink
Increase psalm error level to level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Mar 28, 2020
1 parent 0e04422 commit 885cd68
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/Internal/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ private function resolve($value = null)
}

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

Expand Down
6 changes: 5 additions & 1 deletion lib/LazyPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class LazyPromise implements Promise
/** @var callable|null */
private $promisor;

/** @var \Amp\Promise|null */
/** @var Promise|null */
private $promise;

/**
Expand All @@ -30,11 +30,15 @@ public function __construct(callable $promisor)
public function onResolve(callable $onResolved)
{
if ($this->promise === null) {
\assert($this->promisor !== null);

$provider = $this->promisor;
$this->promisor = null;
$this->promise = call($provider);
}

\assert($this->promise !== null);

$this->promise->onResolve($onResolved);
}
}
11 changes: 11 additions & 0 deletions lib/Loop/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function stop()
*/
public function defer(callable $callback, $data = null): string
{
/** @psalm-var Watcher<null> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::DEFER;
$watcher->id = $this->nextId++;
Expand Down Expand Up @@ -216,10 +217,12 @@ public function delay(int $delay, callable $callback, $data = null): string
throw new \Error("Delay must be greater than or equal to zero");
}

/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::DELAY;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $delay;
$watcher->data = $data;

Expand Down Expand Up @@ -251,10 +254,12 @@ public function repeat(int $interval, callable $callback, $data = null): string
throw new \Error("Interval must be greater than or equal to zero");
}

/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::REPEAT;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $interval;
$watcher->data = $data;

Expand Down Expand Up @@ -285,10 +290,12 @@ public function repeat(int $interval, callable $callback, $data = null): string
*/
public function onReadable($stream, callable $callback, $data = null): string
{
/** @psalm-var Watcher<resource> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::READABLE;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $stream;
$watcher->data = $data;

Expand Down Expand Up @@ -319,10 +326,12 @@ public function onReadable($stream, callable $callback, $data = null): string
*/
public function onWritable($stream, callable $callback, $data = null): string
{
/** @psalm-var Watcher<resource> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::WRITABLE;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $stream;
$watcher->data = $data;

Expand Down Expand Up @@ -354,10 +363,12 @@ public function onWritable($stream, callable $callback, $data = null): string
*/
public function onSignal(int $signo, callable $callback, $data = null): string
{
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher;
$watcher->type = Watcher::SIGNAL;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
/** @psalm-suppress InvalidPropertyAssignmentValue https://github.com/vimeo/psalm/issues/3035 */
$watcher->value = $signo;
$watcher->data = $data;

Expand Down
11 changes: 11 additions & 0 deletions lib/Loop/EvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public function run()
{
$active = self::$activeSignals;

\assert($active !== null);

foreach ($active as $event) {
$event->stop();
}
Expand Down Expand Up @@ -230,10 +232,14 @@ protected function activate(array $watchers)
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
\assert(\is_resource($watcher->value));

$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
break;

case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));

$this->events[$id] = $this->handle->io(
$watcher->value,
\Ev::WRITE,
Expand All @@ -244,6 +250,8 @@ protected function activate(array $watchers)

case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));

$interval = $watcher->value / self::MILLISEC_PER_SEC;
$this->events[$id] = $this->handle->timer(
$interval,
Expand All @@ -254,6 +262,8 @@ protected function activate(array $watchers)
break;

case Watcher::SIGNAL:
\assert(\is_int($watcher->value));

$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
break;

Expand All @@ -267,6 +277,7 @@ protected function activate(array $watchers)
}

if ($watcher->type === Watcher::SIGNAL) {
/** @psalm-suppress PropertyTypeCoercion */
$this->signals[$id] = $this->events[$id];
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/Loop/EventDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function __construct()
}

$this->ioCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_resource($watcher->value));

try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);

Expand All @@ -73,6 +75,8 @@ public function __construct()
};

$this->timerCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_int($watcher->value));

if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id);
} else {
Expand Down Expand Up @@ -167,6 +171,8 @@ public function run()
{
$active = self::$activeSignals;

\assert($active !== null);

foreach ($active as $event) {
$event->del();
}
Expand Down Expand Up @@ -244,6 +250,8 @@ protected function activate(array $watchers)
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
\assert(\is_resource($watcher->value));

$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
Expand All @@ -254,6 +262,8 @@ protected function activate(array $watchers)
break;

case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));

$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
Expand All @@ -265,6 +275,8 @@ protected function activate(array $watchers)

case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));

$this->events[$id] = new \Event(
$this->handle,
-1,
Expand All @@ -275,6 +287,8 @@ protected function activate(array $watchers)
break;

case Watcher::SIGNAL:
\assert(\is_int($watcher->value));

$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
Expand All @@ -294,6 +308,8 @@ protected function activate(array $watchers)
switch ($watcher->type) {
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));

$interval = $watcher->value - ($now - $this->now());
$this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0);
break;
Expand Down
8 changes: 8 additions & 0 deletions lib/Loop/Internal/TimerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class TimerQueue
* @param Watcher $watcher
* @param int $expiration
*
* @psalm-param Watcher<int> $watcher
*
* @return void
*/
public function insert(Watcher $watcher, int $expiration)
Expand All @@ -32,6 +34,8 @@ public function insert(Watcher $watcher, int $expiration)
$this->pointers[$watcher->id] = $node;

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];
$this->data[$node] = $temp;
$this->pointers[$temp->watcher->id] = $node;
Expand All @@ -48,6 +52,8 @@ public function insert(Watcher $watcher, int $expiration)
*
* @param Watcher $watcher
*
* @psalm-param Watcher<int> $watcher
*
* @return void
*/
public function remove(Watcher $watcher)
Expand All @@ -68,6 +74,8 @@ public function remove(Watcher $watcher)
* @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.
*
* @psalm-return Watcher<int>|null
*/
public function extract(int $now)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Loop/NativeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ private function selectStreams(array $read, array $write, int $timeout)
}
}

\assert(\is_array($write)); // See https://github.com/vimeo/psalm/issues/3036

foreach ($write as $stream) {
$streamId = (int) $stream;
if (!isset($this->writeWatchers[$streamId])) {
Expand Down
6 changes: 5 additions & 1 deletion lib/Loop/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Amp\Struct;

/**
* @template TValue as (int|resource|null)
*/
class Watcher
{
use Struct;
Expand Down Expand Up @@ -42,7 +45,8 @@ class Watcher
/**
* 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;
}
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="4"
errorLevel="3"
phpVersion="7.0"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down

0 comments on commit 885cd68

Please sign in to comment.