From 3de79a6f6dfd3117eccd0c4d7809bcf39b106454 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 29 Dec 2023 09:33:40 -0600 Subject: [PATCH] Update Psalm and fix issues --- composer.json | 2 +- psalm.xml | 6 ++++++ src/Closable.php | 2 +- src/Internal/functions.php | 14 +++++++++++--- src/functions.php | 19 +++++++++---------- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index ad18a30a..e703b8bd 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "require-dev": { "amphp/php-cs-fixer-config": "^2", "phpunit/phpunit": "^9", - "psalm/phar": "^5.4" + "psalm/phar": "^5.18" }, "autoload": { "psr-4": { diff --git a/psalm.xml b/psalm.xml index edcf29e2..eeb587fe 100644 --- a/psalm.xml +++ b/psalm.xml @@ -41,5 +41,11 @@ + + + + + + diff --git a/src/Closable.php b/src/Closable.php index e8ffedce..a36ed27c 100644 --- a/src/Closable.php +++ b/src/Closable.php @@ -13,7 +13,7 @@ public function close(): void; /** * Returns whether this resource has been closed. * - * @return bool {@code true} if closed, otherwise {@code false} + * @return bool `true` if closed, otherwise `false`. */ public function isClosed(): bool; diff --git a/src/Internal/functions.php b/src/Internal/functions.php index 2b27754b..00dca1b2 100644 --- a/src/Internal/functions.php +++ b/src/Internal/functions.php @@ -5,7 +5,15 @@ /** * Formats a stacktrace obtained via `debug_backtrace()`. * - * @param list, class?: class-string, file: string, function: string, line: int, object?: object, type?: string}> $trace + * @param list, + * class?: class-string, + * file?: string, + * function: string, + * line?: int, + * object?: object, + * type?: string, + * }> $trace * Output of `debug_backtrace()`. * * @return string Formatted stacktrace. @@ -15,10 +23,10 @@ */ function formatStacktrace(array $trace): string { - return \implode("\n", \array_map(static function ($e, $i) { + return \implode("\n", \array_map(static function (array $e, int $i): string { $line = "#{$i} "; - if (isset($e["file"])) { + if (isset($e["file"], $e['line'])) { $line .= "{$e['file']}:{$e['line']} "; } diff --git a/src/functions.php b/src/functions.php index 6962e911..a9c67796 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,17 +2,16 @@ namespace Amp; -use Amp\Internal\FutureState; use Revolt\EventLoop; use Revolt\EventLoop\UnsupportedFeatureException; /** - * Creates a new fiber asynchronously using the given closure, returning a Future that is completed with the - * eventual return value of the passed function or will fail if the closure throws an exception. + * Creates a new fiber to execute the given closure asynchronously. A Future is returned which is completed with the + * return value of the passed closure or will fail if the closure throws an exception. * * @template T * - * @param \Closure(...):T $closure + * @param \Closure(mixed...):T $closure * @param mixed ...$args Arguments forwarded to the closure when starting the fiber. * * @return Future @@ -21,7 +20,7 @@ function async(\Closure $closure, mixed ...$args): Future { static $run = null; - $run ??= static function (FutureState $state, \Closure $closure, array $args): void { + $run ??= static function (Internal\FutureState $state, \Closure $closure, array $args): void { $s = $state; $c = $closure; @@ -37,7 +36,7 @@ function async(\Closure $closure, mixed ...$args): Future } }; - $state = new Internal\FutureState; + $state = new Internal\FutureState(); EventLoop::queue($run, $state, $closure, $args); @@ -156,16 +155,16 @@ function weakClosure(\Closure $closure): \Closure $closure = fn (mixed ...$args): mixed => $this->{$method}(...$args); if ($useBindTo && $scope) { $closure = $closure->bindTo(null, $scope->name); - - if (!$closure) { - throw new \RuntimeException('Unable to rebind function to type ' . $scope->name); - } } } else { // Rebind to remove reference to $that $closure = $closure->bindTo(new \stdClass()); } + if (!$closure) { + throw new \RuntimeException('Unable to rebind function to type ' . ($scope?->name ?? $that::class)); + } + $reference = \WeakReference::create($that); /** @var \Closure(mixed...):TReturn */