From 37190c9a65643efccc515d5687374f30fcf26b96 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 10 Sep 2018 15:54:01 +0100 Subject: [PATCH] Apply typehints to all AsyncTask methods Since we're breaking API here anyway, no point in holding back on this. --- .../command/defaults/TimingsCommand.php | 2 +- .../level/light/LightPopulationTask.php | 2 +- src/pocketmine/scheduler/AsyncTask.php | 27 ++++++++----------- src/pocketmine/scheduler/BulkCurlTask.php | 2 +- .../scheduler/DumpWorkerMemoryTask.php | 2 +- src/pocketmine/scheduler/FileWriteTask.php | 2 +- .../scheduler/GarbageCollectionTask.php | 2 +- src/pocketmine/scheduler/SendUsageTask.php | 2 +- src/pocketmine/updater/UpdateCheckTask.php | 4 +-- .../tests/AsyncTaskMainLoggerTest.php | 4 +-- .../tests/AsyncTaskMemoryLeakTest.php | 2 +- 11 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/pocketmine/command/defaults/TimingsCommand.php b/src/pocketmine/command/defaults/TimingsCommand.php index cff8e2298..a7503860d 100644 --- a/src/pocketmine/command/defaults/TimingsCommand.php +++ b/src/pocketmine/command/defaults/TimingsCommand.php @@ -126,7 +126,7 @@ class TimingsCommand extends VanillaCommand{ $this->storeLocal($sender); } - public function onCompletion(){ + public function onCompletion() : void{ $sender = $this->fetchLocal(); if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender return; diff --git a/src/pocketmine/level/light/LightPopulationTask.php b/src/pocketmine/level/light/LightPopulationTask.php index 4902c7d59..ced91dc3d 100644 --- a/src/pocketmine/level/light/LightPopulationTask.php +++ b/src/pocketmine/level/light/LightPopulationTask.php @@ -36,7 +36,7 @@ class LightPopulationTask extends AsyncTask{ $this->chunk = $chunk->fastSerialize(); } - public function onRun(){ + public function onRun() : void{ /** @var Chunk $chunk */ $chunk = Chunk::fastDeserialize($this->chunk); diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index 021f6cb94..d3045c2b8 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -63,7 +63,7 @@ abstract class AsyncTask extends Collectable{ private $crashed = false; - public function run(){ + public function run() : void{ $this->result = null; if(!$this->cancelRun){ @@ -89,7 +89,7 @@ abstract class AsyncTask extends Collectable{ return $this->serialized ? unserialize($this->result) : $this->result; } - public function cancelRun(){ + public function cancelRun() : void{ $this->cancelRun = true; } @@ -108,19 +108,19 @@ abstract class AsyncTask extends Collectable{ * @param mixed $result * @param bool $serialize */ - public function setResult($result, bool $serialize = true){ + public function setResult($result, bool $serialize = true) : void{ $this->result = $serialize ? serialize($result) : $result; $this->serialized = $serialize; } - public function setTaskId(int $taskId){ + public function setTaskId(int $taskId) : void{ $this->taskId = $taskId; } /** * @return int|null */ - public function getTaskId(){ + public function getTaskId() : ?int{ return $this->taskId; } @@ -143,7 +143,7 @@ abstract class AsyncTask extends Collectable{ * @param string $identifier * @param mixed $value */ - public function saveToThreadStore(string $identifier, $value){ + public function saveToThreadStore(string $identifier, $value) : void{ if($this->worker === null or $this->isGarbage()){ throw new \BadMethodCallException("Objects can only be added to AsyncWorker thread-local storage during task execution"); } @@ -164,18 +164,14 @@ abstract class AsyncTask extends Collectable{ /** * Actions to execute when run - * - * @return void */ - abstract public function onRun(); + abstract public function onRun() : void; /** * Actions to execute when completed (on main thread) * Implement this if you want to handle the data in your AsyncTask after it has been processed - * - * @return void */ - public function onCompletion(){ + public function onCompletion() : void{ } @@ -192,7 +188,7 @@ abstract class AsyncTask extends Collectable{ /** * @internal Only call from AsyncPool.php on the main thread */ - public function checkProgressUpdates(){ + public function checkProgressUpdates() : void{ while($this->progressUpdates->count() !== 0){ $progress = $this->progressUpdates->shift(); $this->onProgressUpdate(unserialize($progress)); @@ -207,7 +203,7 @@ abstract class AsyncTask extends Collectable{ * @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed * and then unserialize()'ed, as if it has been cloned. */ - public function onProgressUpdate($progress){ + public function onProgressUpdate($progress) : void{ } @@ -232,9 +228,8 @@ abstract class AsyncTask extends Collectable{ * * @param mixed $complexData the data to store * - * @throws \BadMethodCallException if called from any thread except the main thread */ - protected function storeLocal($complexData){ + protected function storeLocal($complexData) : void{ if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){ throw new \BadMethodCallException("Objects can only be stored from the parent thread"); } diff --git a/src/pocketmine/scheduler/BulkCurlTask.php b/src/pocketmine/scheduler/BulkCurlTask.php index 51682efcf..0a1c4566e 100644 --- a/src/pocketmine/scheduler/BulkCurlTask.php +++ b/src/pocketmine/scheduler/BulkCurlTask.php @@ -49,7 +49,7 @@ class BulkCurlTask extends AsyncTask{ $this->operations = serialize($operations); } - public function onRun(){ + public function onRun() : void{ $operations = unserialize($this->operations); $results = []; foreach($operations as $op){ diff --git a/src/pocketmine/scheduler/DumpWorkerMemoryTask.php b/src/pocketmine/scheduler/DumpWorkerMemoryTask.php index ac3525e57..b36529b19 100644 --- a/src/pocketmine/scheduler/DumpWorkerMemoryTask.php +++ b/src/pocketmine/scheduler/DumpWorkerMemoryTask.php @@ -47,7 +47,7 @@ class DumpWorkerMemoryTask extends AsyncTask{ $this->maxStringSize = $maxStringSize; } - public function onRun(){ + public function onRun() : void{ MemoryManager::dumpMemory( $this->worker, $this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(), diff --git a/src/pocketmine/scheduler/FileWriteTask.php b/src/pocketmine/scheduler/FileWriteTask.php index ab926f595..fedb158c0 100644 --- a/src/pocketmine/scheduler/FileWriteTask.php +++ b/src/pocketmine/scheduler/FileWriteTask.php @@ -43,7 +43,7 @@ class FileWriteTask extends AsyncTask{ $this->flags = $flags; } - public function onRun(){ + public function onRun() : void{ try{ file_put_contents($this->path, $this->contents, $this->flags); }catch(\Throwable $e){ diff --git a/src/pocketmine/scheduler/GarbageCollectionTask.php b/src/pocketmine/scheduler/GarbageCollectionTask.php index 25c1f9845..b24ceb319 100644 --- a/src/pocketmine/scheduler/GarbageCollectionTask.php +++ b/src/pocketmine/scheduler/GarbageCollectionTask.php @@ -25,7 +25,7 @@ namespace pocketmine\scheduler; class GarbageCollectionTask extends AsyncTask{ - public function onRun(){ + public function onRun() : void{ gc_enable(); gc_collect_cycles(); } diff --git a/src/pocketmine/scheduler/SendUsageTask.php b/src/pocketmine/scheduler/SendUsageTask.php index b618f33dd..3d5549391 100644 --- a/src/pocketmine/scheduler/SendUsageTask.php +++ b/src/pocketmine/scheduler/SendUsageTask.php @@ -146,7 +146,7 @@ class SendUsageTask extends AsyncTask{ $this->data = json_encode($data/*, JSON_PRETTY_PRINT*/); } - public function onRun(){ + public function onRun() : void{ try{ Internet::postURL($this->endpoint, $this->data, 5, [ "Content-Type: application/json", diff --git a/src/pocketmine/updater/UpdateCheckTask.php b/src/pocketmine/updater/UpdateCheckTask.php index 5c70e4a02..0cb0d586f 100644 --- a/src/pocketmine/updater/UpdateCheckTask.php +++ b/src/pocketmine/updater/UpdateCheckTask.php @@ -42,7 +42,7 @@ class UpdateCheckTask extends AsyncTask{ $this->channel = $channel; } - public function onRun(){ + public function onRun() : void{ $error = ""; $response = Internet::getURL($this->endpoint . "?channel=" . $this->channel, 4, [], $error); $this->error = $error; @@ -70,7 +70,7 @@ class UpdateCheckTask extends AsyncTask{ } } - public function onCompletion(){ + public function onCompletion() : void{ /** @var AutoUpdater $updater */ $updater = $this->fetchLocal(); if($this->hasResult()){ diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php index 11d087f94..4972ec286 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php @@ -39,7 +39,7 @@ class AsyncTaskMainLoggerTest extends Test{ $this->storeLocal($testObject); } - public function onRun(){ + public function onRun() : void{ ob_start(); MainLogger::getLogger()->info("Testing"); if(strpos(ob_get_contents(), "Testing") !== false){ @@ -48,7 +48,7 @@ class AsyncTaskMainLoggerTest extends Test{ ob_end_flush(); } - public function onCompletion(){ + public function onCompletion() : void{ /** @var AsyncTaskMainLoggerTest $test */ $test = $this->fetchLocal(); $test->setResult($this->success ? Test::RESULT_OK : Test::RESULT_FAILED); diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php index 2b7fd4eab..686ad9628 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php @@ -50,7 +50,7 @@ class AsyncTaskMemoryLeakTest extends Test{ class TestAsyncTask extends AsyncTask{ public static $destroyed = false; - public function onRun(){ + public function onRun() : void{ usleep(50 * 1000); //1 server tick }