Apply typehints to all AsyncTask methods

Since we're breaking API here anyway, no point in holding back on this.
This commit is contained in:
Dylan K. Taylor 2018-09-10 15:54:01 +01:00
parent d62e00cc74
commit 37190c9a65
11 changed files with 23 additions and 28 deletions

View File

@ -126,7 +126,7 @@ class TimingsCommand extends VanillaCommand{
$this->storeLocal($sender); $this->storeLocal($sender);
} }
public function onCompletion(){ public function onCompletion() : void{
$sender = $this->fetchLocal(); $sender = $this->fetchLocal();
if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
return; return;

View File

@ -36,7 +36,7 @@ class LightPopulationTask extends AsyncTask{
$this->chunk = $chunk->fastSerialize(); $this->chunk = $chunk->fastSerialize();
} }
public function onRun(){ public function onRun() : void{
/** @var Chunk $chunk */ /** @var Chunk $chunk */
$chunk = Chunk::fastDeserialize($this->chunk); $chunk = Chunk::fastDeserialize($this->chunk);

View File

@ -63,7 +63,7 @@ abstract class AsyncTask extends Collectable{
private $crashed = false; private $crashed = false;
public function run(){ public function run() : void{
$this->result = null; $this->result = null;
if(!$this->cancelRun){ if(!$this->cancelRun){
@ -89,7 +89,7 @@ abstract class AsyncTask extends Collectable{
return $this->serialized ? unserialize($this->result) : $this->result; return $this->serialized ? unserialize($this->result) : $this->result;
} }
public function cancelRun(){ public function cancelRun() : void{
$this->cancelRun = true; $this->cancelRun = true;
} }
@ -108,19 +108,19 @@ abstract class AsyncTask extends Collectable{
* @param mixed $result * @param mixed $result
* @param bool $serialize * @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->result = $serialize ? serialize($result) : $result;
$this->serialized = $serialize; $this->serialized = $serialize;
} }
public function setTaskId(int $taskId){ public function setTaskId(int $taskId) : void{
$this->taskId = $taskId; $this->taskId = $taskId;
} }
/** /**
* @return int|null * @return int|null
*/ */
public function getTaskId(){ public function getTaskId() : ?int{
return $this->taskId; return $this->taskId;
} }
@ -143,7 +143,7 @@ abstract class AsyncTask extends Collectable{
* @param string $identifier * @param string $identifier
* @param mixed $value * @param mixed $value
*/ */
public function saveToThreadStore(string $identifier, $value){ public function saveToThreadStore(string $identifier, $value) : void{
if($this->worker === null or $this->isGarbage()){ if($this->worker === null or $this->isGarbage()){
throw new \BadMethodCallException("Objects can only be added to AsyncWorker thread-local storage during task execution"); 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 * Actions to execute when run
*
* @return void
*/ */
abstract public function onRun(); abstract public function onRun() : void;
/** /**
* Actions to execute when completed (on main thread) * 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 * 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 * @internal Only call from AsyncPool.php on the main thread
*/ */
public function checkProgressUpdates(){ public function checkProgressUpdates() : void{
while($this->progressUpdates->count() !== 0){ while($this->progressUpdates->count() !== 0){
$progress = $this->progressUpdates->shift(); $progress = $this->progressUpdates->shift();
$this->onProgressUpdate(unserialize($progress)); $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 * @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed
* and then unserialize()'ed, as if it has been cloned. * 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 * @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()){ if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){
throw new \BadMethodCallException("Objects can only be stored from the parent thread"); throw new \BadMethodCallException("Objects can only be stored from the parent thread");
} }

View File

@ -49,7 +49,7 @@ class BulkCurlTask extends AsyncTask{
$this->operations = serialize($operations); $this->operations = serialize($operations);
} }
public function onRun(){ public function onRun() : void{
$operations = unserialize($this->operations); $operations = unserialize($this->operations);
$results = []; $results = [];
foreach($operations as $op){ foreach($operations as $op){

View File

@ -47,7 +47,7 @@ class DumpWorkerMemoryTask extends AsyncTask{
$this->maxStringSize = $maxStringSize; $this->maxStringSize = $maxStringSize;
} }
public function onRun(){ public function onRun() : void{
MemoryManager::dumpMemory( MemoryManager::dumpMemory(
$this->worker, $this->worker,
$this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(), $this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(),

View File

@ -43,7 +43,7 @@ class FileWriteTask extends AsyncTask{
$this->flags = $flags; $this->flags = $flags;
} }
public function onRun(){ public function onRun() : void{
try{ try{
file_put_contents($this->path, $this->contents, $this->flags); file_put_contents($this->path, $this->contents, $this->flags);
}catch(\Throwable $e){ }catch(\Throwable $e){

View File

@ -25,7 +25,7 @@ namespace pocketmine\scheduler;
class GarbageCollectionTask extends AsyncTask{ class GarbageCollectionTask extends AsyncTask{
public function onRun(){ public function onRun() : void{
gc_enable(); gc_enable();
gc_collect_cycles(); gc_collect_cycles();
} }

View File

@ -146,7 +146,7 @@ class SendUsageTask extends AsyncTask{
$this->data = json_encode($data/*, JSON_PRETTY_PRINT*/); $this->data = json_encode($data/*, JSON_PRETTY_PRINT*/);
} }
public function onRun(){ public function onRun() : void{
try{ try{
Internet::postURL($this->endpoint, $this->data, 5, [ Internet::postURL($this->endpoint, $this->data, 5, [
"Content-Type: application/json", "Content-Type: application/json",

View File

@ -42,7 +42,7 @@ class UpdateCheckTask extends AsyncTask{
$this->channel = $channel; $this->channel = $channel;
} }
public function onRun(){ public function onRun() : void{
$error = ""; $error = "";
$response = Internet::getURL($this->endpoint . "?channel=" . $this->channel, 4, [], $error); $response = Internet::getURL($this->endpoint . "?channel=" . $this->channel, 4, [], $error);
$this->error = $error; $this->error = $error;
@ -70,7 +70,7 @@ class UpdateCheckTask extends AsyncTask{
} }
} }
public function onCompletion(){ public function onCompletion() : void{
/** @var AutoUpdater $updater */ /** @var AutoUpdater $updater */
$updater = $this->fetchLocal(); $updater = $this->fetchLocal();
if($this->hasResult()){ if($this->hasResult()){

View File

@ -39,7 +39,7 @@ class AsyncTaskMainLoggerTest extends Test{
$this->storeLocal($testObject); $this->storeLocal($testObject);
} }
public function onRun(){ public function onRun() : void{
ob_start(); ob_start();
MainLogger::getLogger()->info("Testing"); MainLogger::getLogger()->info("Testing");
if(strpos(ob_get_contents(), "Testing") !== false){ if(strpos(ob_get_contents(), "Testing") !== false){
@ -48,7 +48,7 @@ class AsyncTaskMainLoggerTest extends Test{
ob_end_flush(); ob_end_flush();
} }
public function onCompletion(){ public function onCompletion() : void{
/** @var AsyncTaskMainLoggerTest $test */ /** @var AsyncTaskMainLoggerTest $test */
$test = $this->fetchLocal(); $test = $this->fetchLocal();
$test->setResult($this->success ? Test::RESULT_OK : Test::RESULT_FAILED); $test->setResult($this->success ? Test::RESULT_OK : Test::RESULT_FAILED);

View File

@ -50,7 +50,7 @@ class AsyncTaskMemoryLeakTest extends Test{
class TestAsyncTask extends AsyncTask{ class TestAsyncTask extends AsyncTask{
public static $destroyed = false; public static $destroyed = false;
public function onRun(){ public function onRun() : void{
usleep(50 * 1000); //1 server tick usleep(50 * 1000); //1 server tick
} }