From b3c6c11b200ef068487da897b276b41467493197 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 16 Mar 2021 23:56:14 +0000 Subject: [PATCH] BulkCurlTask now accepts a closure callback for onCompletion this requires much less boilerplate code than an anonymous class. --- src/command/defaults/TimingsCommand.php | 60 +++++++------------ src/scheduler/BulkCurlTask.php | 21 ++++++- .../check-explicit-mixed-baseline.neon | 7 +-- 3 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/command/defaults/TimingsCommand.php b/src/command/defaults/TimingsCommand.php index 6e2ece723..601688690 100644 --- a/src/command/defaults/TimingsCommand.php +++ b/src/command/defaults/TimingsCommand.php @@ -32,6 +32,7 @@ use pocketmine\scheduler\BulkCurlTask; use pocketmine\scheduler\BulkCurlTaskOperation; use pocketmine\timings\TimingsHandler; use pocketmine\utils\InternetException; +use pocketmine\utils\InternetRequestResult; use function count; use function fclose; use function file_exists; @@ -128,45 +129,28 @@ class TimingsCommand extends VanillaCommand{ $host = $sender->getServer()->getConfigGroup()->getProperty("timings.host", "timings.pmmp.io"); - $sender->getServer()->getAsyncPool()->submitTask(new class($sender, $host, $agent, $data) extends BulkCurlTask{ - private const TLS_KEY_SENDER = "sender"; - - /** @var string */ - private $host; - - /** - * @param string[] $data - * @phpstan-param array $data - */ - public function __construct(CommandSender $sender, string $host, string $agent, array $data){ - parent::__construct([ - new BulkCurlTaskOperation( - "https://$host?upload=true", - 10, - [], - [ - CURLOPT_HTTPHEADER => [ - "User-Agent: $agent", - "Content-Type: application/x-www-form-urlencoded" - ], - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query($data), - CURLOPT_AUTOREFERER => false, - CURLOPT_FOLLOWLOCATION => false - ] - ) - ]); - $this->host = $host; - $this->storeLocal(self::TLS_KEY_SENDER, $sender); - } - - public function onCompletion() : void{ - /** @var CommandSender $sender */ - $sender = $this->fetchLocal(self::TLS_KEY_SENDER); + $sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask( + [new BulkCurlTaskOperation( + "https://$host?upload=true", + 10, + [], + [ + CURLOPT_HTTPHEADER => [ + "User-Agent: $agent", + "Content-Type: application/x-www-form-urlencoded" + ], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($data), + CURLOPT_AUTOREFERER => false, + CURLOPT_FOLLOWLOCATION => false + ] + )], + function(array $results) use ($sender, $host) : void{ + /** @phpstan-var array $results */ if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender return; } - $result = $this->getResult()[0]; + $result = $results[0]; if($result instanceof InternetException){ $sender->getServer()->getLogger()->logException($result); return; @@ -174,12 +158,12 @@ class TimingsCommand extends VanillaCommand{ $response = json_decode($result->getBody(), true); if(is_array($response) && isset($response["id"])){ Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsRead", - ["https://" . $this->host . "/?id=" . $response["id"]])); + ["https://" . $host . "/?id=" . $response["id"]])); }else{ Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.pasteError")); } } - }); + )); }else{ fclose($fileTimings); Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsWrite", [$timings])); diff --git a/src/scheduler/BulkCurlTask.php b/src/scheduler/BulkCurlTask.php index fa60e2731..cd3547175 100644 --- a/src/scheduler/BulkCurlTask.php +++ b/src/scheduler/BulkCurlTask.php @@ -25,6 +25,7 @@ namespace pocketmine\scheduler; use pocketmine\utils\Internet; use pocketmine\utils\InternetException; +use pocketmine\utils\InternetRequestResult; use function igbinary_serialize; use function igbinary_unserialize; @@ -34,6 +35,8 @@ use function igbinary_unserialize; * The result of this AsyncTask is an array of arrays (returned from {@link Internet::simpleCurl}) or InternetException objects. */ class BulkCurlTask extends AsyncTask{ + private const TLS_KEY_COMPLETION_CALLBACK = "completionCallback"; + /** @var string */ private $operations; @@ -44,11 +47,12 @@ class BulkCurlTask extends AsyncTask{ * "timeout", "extraHeaders" and "extraOpts". Documentation of these options are same as those in * {@link Internet::simpleCurl}. * - * @param BulkCurlTaskOperation[] $operations - * @phpstan-param list $operations + * @param BulkCurlTaskOperation[] $operations + * @phpstan-param \Closure(list $results) : void $onCompletion */ - public function __construct(array $operations){ + public function __construct(array $operations, \Closure $onCompletion){ $this->operations = igbinary_serialize($operations); + $this->storeLocal(self::TLS_KEY_COMPLETION_CALLBACK, $onCompletion); } public function onRun() : void{ @@ -67,4 +71,15 @@ class BulkCurlTask extends AsyncTask{ } $this->setResult($results); } + + public function onCompletion() : void{ + /** + * @var \Closure + * @phpstan-var \Closure(list) : void + */ + $callback = $this->fetchLocal(self::TLS_KEY_COMPLETION_CALLBACK); + /** @var InternetRequestResult[]|InternetException[] $results */ + $results = $this->getResult(); + $callback($results); + } } diff --git a/tests/phpstan/configs/check-explicit-mixed-baseline.neon b/tests/phpstan/configs/check-explicit-mixed-baseline.neon index 63009a2d1..70ec278e4 100644 --- a/tests/phpstan/configs/check-explicit-mixed-baseline.neon +++ b/tests/phpstan/configs/check-explicit-mixed-baseline.neon @@ -96,12 +96,7 @@ parameters: path: ../../../src/command/CommandReader.php - - message: "#^Parameter \\#2 \\$host of class class@anonymous/src/command/defaults/TimingsCommand\\.php\\:131 constructor expects string, mixed given\\.$#" - count: 1 - path: ../../../src/command/defaults/TimingsCommand.php - - - - message: "#^Cannot access offset 0 on mixed\\.$#" + message: "#^Part \\$host \\(mixed\\) of encapsed string cannot be cast to string\\.$#" count: 1 path: ../../../src/command/defaults/TimingsCommand.php