From 8fc1501e899fd06cb8e28232fb05c465ca0ce40c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 5 Jul 2017 18:21:04 +0100 Subject: [PATCH] Moar typehints --- src/pocketmine/Server.php | 2 +- src/pocketmine/Worker.php | 2 +- src/pocketmine/level/Level.php | 2 +- src/pocketmine/scheduler/AsyncPool.php | 14 +++---- src/pocketmine/scheduler/AsyncTask.php | 20 +++++----- src/pocketmine/scheduler/AsyncWorker.php | 4 +- src/pocketmine/scheduler/FileWriteTask.php | 14 +++++-- src/pocketmine/scheduler/PluginTask.php | 2 +- src/pocketmine/scheduler/SendUsageTask.php | 7 +++- src/pocketmine/scheduler/ServerScheduler.php | 32 ++++++++-------- src/pocketmine/scheduler/Task.php | 8 ++-- src/pocketmine/scheduler/TaskHandler.php | 24 ++++++------ src/pocketmine/utils/UUID.php | 29 +++++++-------- src/pocketmine/utils/VersionString.php | 39 +++++++++++++------- 14 files changed, 111 insertions(+), 88 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 702bba328..8fef7f486 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -576,7 +576,7 @@ class Server{ } /** - * @return \AttachableThreadedLogger + * @return MainLogger */ public function getLogger(){ return $this->logger; diff --git a/src/pocketmine/Worker.php b/src/pocketmine/Worker.php index 1c4916954..94d990c1a 100644 --- a/src/pocketmine/Worker.php +++ b/src/pocketmine/Worker.php @@ -88,7 +88,7 @@ abstract class Worker extends \Worker{ ThreadManager::getInstance()->remove($this); } - public function getThreadName(){ + public function getThreadName() : string{ return (new \ReflectionClass($this))->getShortName(); } } diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index cabef3ed1..1c6dd2570 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -173,7 +173,7 @@ class Level implements ChunkManager, Metadatable{ /** @var Player[][] */ private $playerLoaders = []; - /** @var DataPacket[] */ + /** @var DataPacket[][] */ private $chunkPackets = []; /** @var float[] */ diff --git a/src/pocketmine/scheduler/AsyncPool.php b/src/pocketmine/scheduler/AsyncPool.php index ce6c18a6f..6d13e3d6d 100644 --- a/src/pocketmine/scheduler/AsyncPool.php +++ b/src/pocketmine/scheduler/AsyncPool.php @@ -43,9 +43,9 @@ class AsyncPool{ /** @var int[] */ private $workerUsage = []; - public function __construct(Server $server, $size){ + public function __construct(Server $server, int $size){ $this->server = $server; - $this->size = (int) $size; + $this->size = $size; for($i = 0; $i < $this->size; ++$i){ $this->workerUsage[$i] = 0; @@ -55,12 +55,11 @@ class AsyncPool{ } } - public function getSize(){ + public function getSize() : int{ return $this->size; } - public function increaseSize($newSize){ - $newSize = (int) $newSize; + public function increaseSize(int $newSize){ if($newSize > $this->size){ for($i = $this->size; $i < $newSize; ++$i){ $this->workerUsage[$i] = 0; @@ -72,12 +71,11 @@ class AsyncPool{ } } - public function submitTaskToWorker(AsyncTask $task, $worker){ + public function submitTaskToWorker(AsyncTask $task, int $worker){ if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){ return; } - $worker = (int) $worker; if($worker < 0 or $worker >= $this->size){ throw new \InvalidArgumentException("Invalid worker $worker"); } @@ -106,7 +104,7 @@ class AsyncPool{ $this->submitTaskToWorker($task, $selectedWorker); } - private function removeTask(AsyncTask $task, $force = false){ + private function removeTask(AsyncTask $task, bool $force = false){ if(isset($this->taskWorkers[$task->getTaskId()])){ if(!$force and ($task->isRunning() or !$task->isGarbage())){ return; diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index 49321666d..8387993b8 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -25,7 +25,6 @@ namespace pocketmine\scheduler; use pocketmine\Collectable; use pocketmine\Server; -use pocketmine\utils\MainLogger; /** * Class used to run async tasks in other threads. @@ -47,7 +46,7 @@ abstract class AsyncTask extends Collectable{ private $result = null; private $serialized = false; private $cancelRun = false; - /** @var int */ + /** @var int|null */ private $taskId = null; private $crashed = false; @@ -90,7 +89,7 @@ abstract class AsyncTask extends Collectable{ $this->setGarbage(); } - public function isCrashed(){ + public function isCrashed() : bool{ return $this->crashed; } @@ -105,14 +104,14 @@ abstract class AsyncTask extends Collectable{ $this->cancelRun = true; } - public function hasCancelledRun(){ + public function hasCancelledRun() : bool{ return $this->cancelRun === true; } /** * @return bool */ - public function hasResult(){ + public function hasResult() : bool{ return $this->result !== null; } @@ -120,15 +119,18 @@ abstract class AsyncTask extends Collectable{ * @param mixed $result * @param bool $serialize */ - public function setResult($result, $serialize = true){ + public function setResult($result, bool $serialize = true){ $this->result = $serialize ? serialize($result) : $result; $this->serialized = $serialize; } - public function setTaskId($taskId){ + public function setTaskId(int $taskId){ $this->taskId = $taskId; } + /** + * @return int|null + */ public function getTaskId(){ return $this->taskId; } @@ -140,7 +142,7 @@ abstract class AsyncTask extends Collectable{ * @param string $identifier * @return mixed */ - public function getFromThreadStore($identifier){ + public function getFromThreadStore(string $identifier){ global $store; return ($this->isGarbage() or !isset($store[$identifier])) ? null : $store[$identifier]; } @@ -152,7 +154,7 @@ abstract class AsyncTask extends Collectable{ * @param string $identifier * @param mixed $value */ - public function saveToThreadStore($identifier, $value){ + public function saveToThreadStore(string $identifier, $value){ global $store; if(!$this->isGarbage()){ $store[$identifier] = $value; diff --git a/src/pocketmine/scheduler/AsyncWorker.php b/src/pocketmine/scheduler/AsyncWorker.php index 5339c84e5..81e313821 100644 --- a/src/pocketmine/scheduler/AsyncWorker.php +++ b/src/pocketmine/scheduler/AsyncWorker.php @@ -31,7 +31,7 @@ class AsyncWorker extends Worker{ private $logger; private $id; - public function __construct(MainLogger $logger, $id){ + public function __construct(MainLogger $logger, int $id){ $this->logger = $logger; $this->id = $id; } @@ -53,7 +53,7 @@ class AsyncWorker extends Worker{ $this->logger->logException($e); } - public function getThreadName(){ + public function getThreadName() : string{ return "Asynchronous Worker #" . $this->id; } } diff --git a/src/pocketmine/scheduler/FileWriteTask.php b/src/pocketmine/scheduler/FileWriteTask.php index 024f7e911..ab926f595 100644 --- a/src/pocketmine/scheduler/FileWriteTask.php +++ b/src/pocketmine/scheduler/FileWriteTask.php @@ -25,19 +25,27 @@ namespace pocketmine\scheduler; class FileWriteTask extends AsyncTask{ + /** @var string */ private $path; + /** @var mixed */ private $contents; + /** @var int */ private $flags; - public function __construct($path, $contents, $flags = 0){ + /** + * @param string $path + * @param mixed $contents + * @param int $flags + */ + public function __construct(string $path, $contents, int $flags = 0){ $this->path = $path; $this->contents = $contents; - $this->flags = (int) $flags; + $this->flags = $flags; } public function onRun(){ try{ - file_put_contents($this->path, $this->contents, (int) $this->flags); + file_put_contents($this->path, $this->contents, $this->flags); }catch(\Throwable $e){ } diff --git a/src/pocketmine/scheduler/PluginTask.php b/src/pocketmine/scheduler/PluginTask.php index e2cf85171..c5d503ec4 100644 --- a/src/pocketmine/scheduler/PluginTask.php +++ b/src/pocketmine/scheduler/PluginTask.php @@ -43,7 +43,7 @@ abstract class PluginTask extends Task{ /** * @return Plugin */ - final public function getOwner(){ + final public function getOwner() : Plugin{ return $this->owner; } diff --git a/src/pocketmine/scheduler/SendUsageTask.php b/src/pocketmine/scheduler/SendUsageTask.php index a8eb9e25e..59f183117 100644 --- a/src/pocketmine/scheduler/SendUsageTask.php +++ b/src/pocketmine/scheduler/SendUsageTask.php @@ -38,7 +38,12 @@ class SendUsageTask extends AsyncTask{ public $endpoint; public $data; - public function __construct(Server $server, $type, $playerList = []){ + /** + * @param Server $server + * @param int $type + * @param array $playerList + */ + public function __construct(Server $server, int $type, array $playerList = []){ $endpoint = "http://" . $server->getProperty("anonymous-statistics.host", "stats.pocketmine.net") . "/"; $data = []; diff --git a/src/pocketmine/scheduler/ServerScheduler.php b/src/pocketmine/scheduler/ServerScheduler.php index fba583e91..52c6fa8ce 100644 --- a/src/pocketmine/scheduler/ServerScheduler.php +++ b/src/pocketmine/scheduler/ServerScheduler.php @@ -180,11 +180,11 @@ class ServerScheduler{ return true; } - public function getAsyncTaskPoolSize(){ + public function getAsyncTaskPoolSize() : int{ return $this->asyncPool->getSize(); } - public function increaseAsyncTaskPoolSize($newSize){ + public function increaseAsyncTaskPoolSize(int $newSize){ $this->asyncPool->increaseSize($newSize); } @@ -194,8 +194,8 @@ class ServerScheduler{ * * @return null|TaskHandler */ - public function scheduleDelayedTask(Task $task, $delay){ - return $this->addTask($task, (int) $delay, -1); + public function scheduleDelayedTask(Task $task, int $delay){ + return $this->addTask($task, $delay, -1); } /** @@ -204,8 +204,8 @@ class ServerScheduler{ * * @return null|TaskHandler */ - public function scheduleRepeatingTask(Task $task, $period){ - return $this->addTask($task, -1, (int) $period); + public function scheduleRepeatingTask(Task $task, int $period){ + return $this->addTask($task, -1, $period); } /** @@ -215,14 +215,14 @@ class ServerScheduler{ * * @return null|TaskHandler */ - public function scheduleDelayedRepeatingTask(Task $task, $delay, $period){ - return $this->addTask($task, (int) $delay, (int) $period); + public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period){ + return $this->addTask($task, $delay, $period); } /** * @param int $taskId */ - public function cancelTask($taskId){ + public function cancelTask(int $taskId){ if($taskId !== null and isset($this->tasks[$taskId])){ $this->tasks[$taskId]->cancel(); unset($this->tasks[$taskId]); @@ -259,20 +259,20 @@ class ServerScheduler{ * * @return bool */ - public function isQueued($taskId){ + public function isQueued(int $taskId) : bool{ return isset($this->tasks[$taskId]); } /** * @param Task $task - * @param $delay - * @param $period + * @param int $delay + * @param int $period * * @return null|TaskHandler * * @throws PluginException */ - private function addTask(Task $task, $delay, $period){ + private function addTask(Task $task, int $delay, int $period){ if($task instanceof PluginTask){ if(!($task->getOwner() instanceof Plugin)){ throw new PluginException("Invalid owner of PluginTask " . get_class($task)); @@ -311,7 +311,7 @@ class ServerScheduler{ /** * @param int $currentTick */ - public function mainThreadHeartbeat($currentTick){ + public function mainThreadHeartbeat(int $currentTick){ $this->currentTick = $currentTick; while($this->isReady($this->currentTick)){ /** @var TaskHandler $task */ @@ -341,14 +341,14 @@ class ServerScheduler{ $this->asyncPool->collectTasks(); } - private function isReady($currentTicks){ + private function isReady(int $currentTicks) : bool{ return count($this->tasks) > 0 and $this->queue->current()->getNextRun() <= $currentTicks; } /** * @return int */ - private function nextId(){ + private function nextId() : int{ return $this->ids++; } diff --git a/src/pocketmine/scheduler/Task.php b/src/pocketmine/scheduler/Task.php index a7c0b4b39..a6cf96fb8 100644 --- a/src/pocketmine/scheduler/Task.php +++ b/src/pocketmine/scheduler/Task.php @@ -32,7 +32,7 @@ abstract class Task{ private $taskHandler = null; /** - * @return TaskHandler + * @return TaskHandler|null */ final public function getHandler(){ return $this->taskHandler; @@ -41,7 +41,7 @@ abstract class Task{ /** * @return int */ - final public function getTaskId(){ + final public function getTaskId() : int{ if($this->taskHandler !== null){ return $this->taskHandler->getTaskId(); } @@ -50,9 +50,9 @@ abstract class Task{ } /** - * @param TaskHandler $taskHandler + * @param TaskHandler|null $taskHandler */ - final public function setHandler($taskHandler){ + final public function setHandler(TaskHandler $taskHandler = null){ if($this->taskHandler === null or $taskHandler === null){ $this->taskHandler = $taskHandler; } diff --git a/src/pocketmine/scheduler/TaskHandler.php b/src/pocketmine/scheduler/TaskHandler.php index 3c58da9e7..8e04778b7 100644 --- a/src/pocketmine/scheduler/TaskHandler.php +++ b/src/pocketmine/scheduler/TaskHandler.php @@ -58,7 +58,7 @@ class TaskHandler{ * @param int $delay * @param int $period */ - public function __construct($timingName, Task $task, $taskId, $delay = -1, $period = -1){ + public function __construct(string $timingName, Task $task, int $taskId, int $delay = -1, int $period = -1){ $this->task = $task; $this->taskId = $taskId; $this->delay = $delay; @@ -71,63 +71,63 @@ class TaskHandler{ /** * @return bool */ - public function isCancelled(){ + public function isCancelled() : bool{ return $this->cancelled === true; } /** * @return int */ - public function getNextRun(){ + public function getNextRun() : int{ return $this->nextRun; } /** * @param int $ticks */ - public function setNextRun($ticks){ + public function setNextRun(int $ticks){ $this->nextRun = $ticks; } /** * @return int */ - public function getTaskId(){ + public function getTaskId() : int{ return $this->taskId; } /** * @return Task */ - public function getTask(){ + public function getTask() : Task{ return $this->task; } /** * @return int */ - public function getDelay(){ + public function getDelay() : int{ return $this->delay; } /** * @return bool */ - public function isDelayed(){ + public function isDelayed() : bool{ return $this->delay > 0; } /** * @return bool */ - public function isRepeating(){ + public function isRepeating() : bool{ return $this->period > 0; } /** * @return int */ - public function getPeriod(){ + public function getPeriod() : int{ return $this->period; } @@ -150,14 +150,14 @@ class TaskHandler{ /** * @param int $currentTick */ - public function run($currentTick){ + public function run(int $currentTick){ $this->task->onRun($currentTick); } /** * @return string */ - public function getTaskName(){ + public function getTaskName() : string{ if($this->timingName !== null){ return $this->timingName; } diff --git a/src/pocketmine/utils/UUID.php b/src/pocketmine/utils/UUID.php index abdbfa0a9..481f45700 100644 --- a/src/pocketmine/utils/UUID.php +++ b/src/pocketmine/utils/UUID.php @@ -28,20 +28,17 @@ class UUID{ private $parts = [0, 0, 0, 0]; private $version = null; - public function __construct($part1 = 0, $part2 = 0, $part3 = 0, $part4 = 0, $version = null){ - $this->parts[0] = (int) $part1; - $this->parts[1] = (int) $part2; - $this->parts[2] = (int) $part3; - $this->parts[3] = (int) $part4; + public function __construct(int $part1 = 0, int $part2 = 0, int $part3 = 0, int $part4 = 0, int $version = null){ + $this->parts = [$part1, $part2, $part3, $part4]; - $this->version = $version === null ? ($this->parts[1] & 0xf000) >> 12 : (int) $version; + $this->version = $version === null ? ($this->parts[1] & 0xf000) >> 12 : $version; } - public function getVersion(){ + public function getVersion() : int{ return $this->version; } - public function equals(UUID $uuid){ + public function equals(UUID $uuid) : bool{ return $uuid->parts[0] === $this->parts[0] and $uuid->parts[1] === $this->parts[1] and $uuid->parts[2] === $this->parts[2] and $uuid->parts[3] === $this->parts[3]; } @@ -52,7 +49,7 @@ class UUID{ * @param int $version * @return UUID */ - public static function fromString($uuid, $version = null){ + public static function fromString(string $uuid, int $version = null) : UUID{ return self::fromBinary(hex2bin(str_replace("-", "", trim($uuid))), $version); } @@ -62,8 +59,10 @@ class UUID{ * @param string $uuid * @param int $version * @return UUID + * + * @throws \InvalidArgumentException */ - public static function fromBinary($uuid, $version = null){ + public static function fromBinary(string $uuid, int $version = null) : UUID{ if(strlen($uuid) !== 16){ throw new \InvalidArgumentException("Must have exactly 16 bytes"); } @@ -77,21 +76,21 @@ class UUID{ * @param string[] ...$data * @return UUID */ - public static function fromData(string ...$data){ + public static function fromData(string ...$data) : UUID{ $hash = hash("md5", implode($data), true); return self::fromBinary($hash, 3); } - public static function fromRandom(){ + public static function fromRandom() : UUID{ return self::fromData(Binary::writeInt(time()), Binary::writeShort(getmypid()), Binary::writeShort(getmyuid()), Binary::writeInt(mt_rand(-0x7fffffff, 0x7fffffff)), Binary::writeInt(mt_rand(-0x7fffffff, 0x7fffffff))); } - public function toBinary(){ + public function toBinary() : string{ return Binary::writeInt($this->parts[0]) . Binary::writeInt($this->parts[1]) . Binary::writeInt($this->parts[2]) . Binary::writeInt($this->parts[3]); } - public function toString(){ + public function toString() : string{ $hex = bin2hex($this->toBinary()); //xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx 8-4-4-4-12 @@ -101,7 +100,7 @@ class UUID{ return substr($hex, 0, 8) . "-" . substr($hex, 8, 4) . "-" . substr($hex, 12, 4) . "-" . substr($hex, 16, 4) . "-" . substr($hex, 20, 12); } - public function __toString(){ + public function __toString() : string{ return $this->toString(); } diff --git a/src/pocketmine/utils/VersionString.php b/src/pocketmine/utils/VersionString.php index 0d03d8599..3d74b41e4 100644 --- a/src/pocketmine/utils/VersionString.php +++ b/src/pocketmine/utils/VersionString.php @@ -33,6 +33,11 @@ class VersionString{ private $minor; private $development = false; + /** + * VersionString constructor. + * + * @param int|string $version + */ public function __construct($version = \pocketmine\VERSION){ if(is_int($version)){ $this->minor = $version & 0x1F; @@ -40,9 +45,9 @@ class VersionString{ $this->generation = ($version >> 9) & 0x0F; }else{ $version = preg_split("/([A-Za-z]*)[ _\\-]?([0-9]*)\\.([0-9]*)\\.{0,1}([0-9]*)(dev|)(-[\\0-9]{1,}|)/", $version, -1, PREG_SPLIT_DELIM_CAPTURE); - $this->generation = isset($version[2]) ? (int) $version[2] : 0; //0-15 - $this->major = isset($version[3]) ? (int) $version[3] : 0; //0-15 - $this->minor = isset($version[4]) ? (int) $version[4] : 0; //0-31 + $this->generation = (int) ($version[2] ?? 0); //0-15 + $this->major = (int) ($version[3] ?? 0); //0-15 + $this->minor = (int) ($version[4] ?? 0); //0-31 $this->development = $version[5] === "dev"; if($version[6] !== ""){ $this->build = (int) (substr($version[6], 1)); @@ -52,43 +57,49 @@ class VersionString{ } } - public function getNumber(){ + public function getNumber() : int{ return (int) (($this->generation << 9) + ($this->major << 5) + $this->minor); } - public function getGeneration(){ + public function getGeneration() : int{ return $this->generation; } - public function getMajor(){ + public function getMajor() : int{ return $this->major; } - public function getMinor(){ + public function getMinor() : int{ return $this->minor; } - public function getRelease(){ + public function getRelease() : string{ return $this->generation . "." . $this->major . ($this->minor > 0 ? "." . $this->minor : ""); } - public function getBuild(){ + public function getBuild() : int{ return $this->build; } - public function isDev(){ - return $this->development === true; + public function isDev() : bool{ + return $this->development; } - public function get($build = false){ + public function get(bool $build = false) : string{ return $this->getRelease() . ($this->development === true ? "dev" : "") . (($this->build > 0 and $build === true) ? "-" . $this->build : ""); } - public function __toString(){ + public function __toString() : string{ return $this->get(); } - public function compare($target, $diff = false){ + /** + * @param VersionString|int|string $target + * @param bool $diff + * + * @return int + */ + public function compare($target, bool $diff = false) : int{ if(($target instanceof VersionString) === false){ $target = new VersionString($target); }