From 7eb8d8e3669ae5ae6bd5641fc166455b0127bbb7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 27 May 2019 17:11:35 +0100 Subject: [PATCH] Require Closures in more places instead of callable Non-closure callables have strange context-dependent behaviour and are slower to call than a closure. It's possible to use Closure::fromCallable() in the origin scope, which guarantees that the callable will remain callable no matter where it's used. --- src/pocketmine/network/mcpe/CompressBatchPromise.php | 6 +++--- src/pocketmine/utils/Internet.php | 8 ++++---- src/pocketmine/world/BlockTransaction.php | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/pocketmine/network/mcpe/CompressBatchPromise.php b/src/pocketmine/network/mcpe/CompressBatchPromise.php index f58ef6ca0..fe4e8f864 100644 --- a/src/pocketmine/network/mcpe/CompressBatchPromise.php +++ b/src/pocketmine/network/mcpe/CompressBatchPromise.php @@ -27,7 +27,7 @@ use pocketmine\utils\Utils; use function array_push; class CompressBatchPromise{ - /** @var callable[] */ + /** @var \Closure[] */ private $callbacks = []; /** @var string|null */ @@ -36,7 +36,7 @@ class CompressBatchPromise{ /** @var bool */ private $cancelled = false; - public function onResolve(callable ...$callbacks) : void{ + public function onResolve(\Closure ...$callbacks) : void{ $this->checkCancelled(); foreach($callbacks as $callback){ Utils::validateCallableSignature(function(CompressBatchPromise $promise){}, $callback); @@ -64,7 +64,7 @@ class CompressBatchPromise{ } /** - * @return callable[] + * @return \Closure[] */ public function getResolveCallbacks() : array{ return $this->callbacks; diff --git a/src/pocketmine/utils/Internet.php b/src/pocketmine/utils/Internet.php index 67035f5e6..bd56cb9d4 100644 --- a/src/pocketmine/utils/Internet.php +++ b/src/pocketmine/utils/Internet.php @@ -184,16 +184,16 @@ class Internet{ * NOTE: This is a blocking operation and can take a significant amount of time. It is inadvisable to use this method on the main thread. * * @param string $page - * @param float|int $timeout The maximum connect timeout and timeout in seconds, correct to ms. + * @param float|int $timeout The maximum connect timeout and timeout in seconds, correct to ms. * @param string[] $extraHeaders extra headers to send as a plain string array - * @param array $extraOpts extra CURLOPT_* to set as an [opt => value] map - * @param callable|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle. + * @param array $extraOpts extra CURLOPT_* to set as an [opt => value] map + * @param \Closure|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle. * * @return array a plain array of three [result body : string, headers : array[], HTTP response code : int]. Headers are grouped by requests with strtolower(header name) as keys and header value as values * * @throws InternetException if a cURL error occurs */ - public static function simpleCurl(string $page, $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?callable $onSuccess = null) : array{ + public static function simpleCurl(string $page, $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?\Closure $onSuccess = null) : array{ if(!self::$online){ throw new InternetException("Cannot execute web request while offline"); } diff --git a/src/pocketmine/world/BlockTransaction.php b/src/pocketmine/world/BlockTransaction.php index 6232f4478..428daaf8d 100644 --- a/src/pocketmine/world/BlockTransaction.php +++ b/src/pocketmine/world/BlockTransaction.php @@ -134,9 +134,9 @@ class BlockTransaction{ * The callable signature should be the same as the below dummy function. * @see BlockTransaction::dummyValidator() * - * @param callable $validator + * @param \Closure $validator */ - public function addValidator(callable $validator) : void{ + public function addValidator(\Closure $validator) : void{ Utils::validateCallableSignature([$this, 'dummyValidator'], $validator); $this->validators[] = $validator; }