Moar typehints

This commit is contained in:
Dylan K. Taylor 2017-07-05 18:21:04 +01:00
parent 08b8debd78
commit 8fc1501e89
14 changed files with 111 additions and 88 deletions

View File

@ -576,7 +576,7 @@ class Server{
} }
/** /**
* @return \AttachableThreadedLogger * @return MainLogger
*/ */
public function getLogger(){ public function getLogger(){
return $this->logger; return $this->logger;

View File

@ -88,7 +88,7 @@ abstract class Worker extends \Worker{
ThreadManager::getInstance()->remove($this); ThreadManager::getInstance()->remove($this);
} }
public function getThreadName(){ public function getThreadName() : string{
return (new \ReflectionClass($this))->getShortName(); return (new \ReflectionClass($this))->getShortName();
} }
} }

View File

@ -173,7 +173,7 @@ class Level implements ChunkManager, Metadatable{
/** @var Player[][] */ /** @var Player[][] */
private $playerLoaders = []; private $playerLoaders = [];
/** @var DataPacket[] */ /** @var DataPacket[][] */
private $chunkPackets = []; private $chunkPackets = [];
/** @var float[] */ /** @var float[] */

View File

@ -43,9 +43,9 @@ class AsyncPool{
/** @var int[] */ /** @var int[] */
private $workerUsage = []; private $workerUsage = [];
public function __construct(Server $server, $size){ public function __construct(Server $server, int $size){
$this->server = $server; $this->server = $server;
$this->size = (int) $size; $this->size = $size;
for($i = 0; $i < $this->size; ++$i){ for($i = 0; $i < $this->size; ++$i){
$this->workerUsage[$i] = 0; $this->workerUsage[$i] = 0;
@ -55,12 +55,11 @@ class AsyncPool{
} }
} }
public function getSize(){ public function getSize() : int{
return $this->size; return $this->size;
} }
public function increaseSize($newSize){ public function increaseSize(int $newSize){
$newSize = (int) $newSize;
if($newSize > $this->size){ if($newSize > $this->size){
for($i = $this->size; $i < $newSize; ++$i){ for($i = $this->size; $i < $newSize; ++$i){
$this->workerUsage[$i] = 0; $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()){ if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){
return; return;
} }
$worker = (int) $worker;
if($worker < 0 or $worker >= $this->size){ if($worker < 0 or $worker >= $this->size){
throw new \InvalidArgumentException("Invalid worker $worker"); throw new \InvalidArgumentException("Invalid worker $worker");
} }
@ -106,7 +104,7 @@ class AsyncPool{
$this->submitTaskToWorker($task, $selectedWorker); $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(isset($this->taskWorkers[$task->getTaskId()])){
if(!$force and ($task->isRunning() or !$task->isGarbage())){ if(!$force and ($task->isRunning() or !$task->isGarbage())){
return; return;

View File

@ -25,7 +25,6 @@ namespace pocketmine\scheduler;
use pocketmine\Collectable; use pocketmine\Collectable;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\MainLogger;
/** /**
* Class used to run async tasks in other threads. * Class used to run async tasks in other threads.
@ -47,7 +46,7 @@ abstract class AsyncTask extends Collectable{
private $result = null; private $result = null;
private $serialized = false; private $serialized = false;
private $cancelRun = false; private $cancelRun = false;
/** @var int */ /** @var int|null */
private $taskId = null; private $taskId = null;
private $crashed = false; private $crashed = false;
@ -90,7 +89,7 @@ abstract class AsyncTask extends Collectable{
$this->setGarbage(); $this->setGarbage();
} }
public function isCrashed(){ public function isCrashed() : bool{
return $this->crashed; return $this->crashed;
} }
@ -105,14 +104,14 @@ abstract class AsyncTask extends Collectable{
$this->cancelRun = true; $this->cancelRun = true;
} }
public function hasCancelledRun(){ public function hasCancelledRun() : bool{
return $this->cancelRun === true; return $this->cancelRun === true;
} }
/** /**
* @return bool * @return bool
*/ */
public function hasResult(){ public function hasResult() : bool{
return $this->result !== null; return $this->result !== null;
} }
@ -120,15 +119,18 @@ abstract class AsyncTask extends Collectable{
* @param mixed $result * @param mixed $result
* @param bool $serialize * @param bool $serialize
*/ */
public function setResult($result, $serialize = true){ public function setResult($result, bool $serialize = true){
$this->result = $serialize ? serialize($result) : $result; $this->result = $serialize ? serialize($result) : $result;
$this->serialized = $serialize; $this->serialized = $serialize;
} }
public function setTaskId($taskId){ public function setTaskId(int $taskId){
$this->taskId = $taskId; $this->taskId = $taskId;
} }
/**
* @return int|null
*/
public function getTaskId(){ public function getTaskId(){
return $this->taskId; return $this->taskId;
} }
@ -140,7 +142,7 @@ abstract class AsyncTask extends Collectable{
* @param string $identifier * @param string $identifier
* @return mixed * @return mixed
*/ */
public function getFromThreadStore($identifier){ public function getFromThreadStore(string $identifier){
global $store; global $store;
return ($this->isGarbage() or !isset($store[$identifier])) ? null : $store[$identifier]; return ($this->isGarbage() or !isset($store[$identifier])) ? null : $store[$identifier];
} }
@ -152,7 +154,7 @@ abstract class AsyncTask extends Collectable{
* @param string $identifier * @param string $identifier
* @param mixed $value * @param mixed $value
*/ */
public function saveToThreadStore($identifier, $value){ public function saveToThreadStore(string $identifier, $value){
global $store; global $store;
if(!$this->isGarbage()){ if(!$this->isGarbage()){
$store[$identifier] = $value; $store[$identifier] = $value;

View File

@ -31,7 +31,7 @@ class AsyncWorker extends Worker{
private $logger; private $logger;
private $id; private $id;
public function __construct(MainLogger $logger, $id){ public function __construct(MainLogger $logger, int $id){
$this->logger = $logger; $this->logger = $logger;
$this->id = $id; $this->id = $id;
} }
@ -53,7 +53,7 @@ class AsyncWorker extends Worker{
$this->logger->logException($e); $this->logger->logException($e);
} }
public function getThreadName(){ public function getThreadName() : string{
return "Asynchronous Worker #" . $this->id; return "Asynchronous Worker #" . $this->id;
} }
} }

View File

@ -25,19 +25,27 @@ namespace pocketmine\scheduler;
class FileWriteTask extends AsyncTask{ class FileWriteTask extends AsyncTask{
/** @var string */
private $path; private $path;
/** @var mixed */
private $contents; private $contents;
/** @var int */
private $flags; 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->path = $path;
$this->contents = $contents; $this->contents = $contents;
$this->flags = (int) $flags; $this->flags = $flags;
} }
public function onRun(){ public function onRun(){
try{ try{
file_put_contents($this->path, $this->contents, (int) $this->flags); file_put_contents($this->path, $this->contents, $this->flags);
}catch(\Throwable $e){ }catch(\Throwable $e){
} }

View File

@ -43,7 +43,7 @@ abstract class PluginTask extends Task{
/** /**
* @return Plugin * @return Plugin
*/ */
final public function getOwner(){ final public function getOwner() : Plugin{
return $this->owner; return $this->owner;
} }

View File

@ -38,7 +38,12 @@ class SendUsageTask extends AsyncTask{
public $endpoint; public $endpoint;
public $data; 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") . "/"; $endpoint = "http://" . $server->getProperty("anonymous-statistics.host", "stats.pocketmine.net") . "/";
$data = []; $data = [];

View File

@ -180,11 +180,11 @@ class ServerScheduler{
return true; return true;
} }
public function getAsyncTaskPoolSize(){ public function getAsyncTaskPoolSize() : int{
return $this->asyncPool->getSize(); return $this->asyncPool->getSize();
} }
public function increaseAsyncTaskPoolSize($newSize){ public function increaseAsyncTaskPoolSize(int $newSize){
$this->asyncPool->increaseSize($newSize); $this->asyncPool->increaseSize($newSize);
} }
@ -194,8 +194,8 @@ class ServerScheduler{
* *
* @return null|TaskHandler * @return null|TaskHandler
*/ */
public function scheduleDelayedTask(Task $task, $delay){ public function scheduleDelayedTask(Task $task, int $delay){
return $this->addTask($task, (int) $delay, -1); return $this->addTask($task, $delay, -1);
} }
/** /**
@ -204,8 +204,8 @@ class ServerScheduler{
* *
* @return null|TaskHandler * @return null|TaskHandler
*/ */
public function scheduleRepeatingTask(Task $task, $period){ public function scheduleRepeatingTask(Task $task, int $period){
return $this->addTask($task, -1, (int) $period); return $this->addTask($task, -1, $period);
} }
/** /**
@ -215,14 +215,14 @@ class ServerScheduler{
* *
* @return null|TaskHandler * @return null|TaskHandler
*/ */
public function scheduleDelayedRepeatingTask(Task $task, $delay, $period){ public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period){
return $this->addTask($task, (int) $delay, (int) $period); return $this->addTask($task, $delay, $period);
} }
/** /**
* @param int $taskId * @param int $taskId
*/ */
public function cancelTask($taskId){ public function cancelTask(int $taskId){
if($taskId !== null and isset($this->tasks[$taskId])){ if($taskId !== null and isset($this->tasks[$taskId])){
$this->tasks[$taskId]->cancel(); $this->tasks[$taskId]->cancel();
unset($this->tasks[$taskId]); unset($this->tasks[$taskId]);
@ -259,20 +259,20 @@ class ServerScheduler{
* *
* @return bool * @return bool
*/ */
public function isQueued($taskId){ public function isQueued(int $taskId) : bool{
return isset($this->tasks[$taskId]); return isset($this->tasks[$taskId]);
} }
/** /**
* @param Task $task * @param Task $task
* @param $delay * @param int $delay
* @param $period * @param int $period
* *
* @return null|TaskHandler * @return null|TaskHandler
* *
* @throws PluginException * @throws PluginException
*/ */
private function addTask(Task $task, $delay, $period){ private function addTask(Task $task, int $delay, int $period){
if($task instanceof PluginTask){ if($task instanceof PluginTask){
if(!($task->getOwner() instanceof Plugin)){ if(!($task->getOwner() instanceof Plugin)){
throw new PluginException("Invalid owner of PluginTask " . get_class($task)); throw new PluginException("Invalid owner of PluginTask " . get_class($task));
@ -311,7 +311,7 @@ class ServerScheduler{
/** /**
* @param int $currentTick * @param int $currentTick
*/ */
public function mainThreadHeartbeat($currentTick){ public function mainThreadHeartbeat(int $currentTick){
$this->currentTick = $currentTick; $this->currentTick = $currentTick;
while($this->isReady($this->currentTick)){ while($this->isReady($this->currentTick)){
/** @var TaskHandler $task */ /** @var TaskHandler $task */
@ -341,14 +341,14 @@ class ServerScheduler{
$this->asyncPool->collectTasks(); $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 count($this->tasks) > 0 and $this->queue->current()->getNextRun() <= $currentTicks;
} }
/** /**
* @return int * @return int
*/ */
private function nextId(){ private function nextId() : int{
return $this->ids++; return $this->ids++;
} }

View File

@ -32,7 +32,7 @@ abstract class Task{
private $taskHandler = null; private $taskHandler = null;
/** /**
* @return TaskHandler * @return TaskHandler|null
*/ */
final public function getHandler(){ final public function getHandler(){
return $this->taskHandler; return $this->taskHandler;
@ -41,7 +41,7 @@ abstract class Task{
/** /**
* @return int * @return int
*/ */
final public function getTaskId(){ final public function getTaskId() : int{
if($this->taskHandler !== null){ if($this->taskHandler !== null){
return $this->taskHandler->getTaskId(); 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){ if($this->taskHandler === null or $taskHandler === null){
$this->taskHandler = $taskHandler; $this->taskHandler = $taskHandler;
} }

View File

@ -58,7 +58,7 @@ class TaskHandler{
* @param int $delay * @param int $delay
* @param int $period * @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->task = $task;
$this->taskId = $taskId; $this->taskId = $taskId;
$this->delay = $delay; $this->delay = $delay;
@ -71,63 +71,63 @@ class TaskHandler{
/** /**
* @return bool * @return bool
*/ */
public function isCancelled(){ public function isCancelled() : bool{
return $this->cancelled === true; return $this->cancelled === true;
} }
/** /**
* @return int * @return int
*/ */
public function getNextRun(){ public function getNextRun() : int{
return $this->nextRun; return $this->nextRun;
} }
/** /**
* @param int $ticks * @param int $ticks
*/ */
public function setNextRun($ticks){ public function setNextRun(int $ticks){
$this->nextRun = $ticks; $this->nextRun = $ticks;
} }
/** /**
* @return int * @return int
*/ */
public function getTaskId(){ public function getTaskId() : int{
return $this->taskId; return $this->taskId;
} }
/** /**
* @return Task * @return Task
*/ */
public function getTask(){ public function getTask() : Task{
return $this->task; return $this->task;
} }
/** /**
* @return int * @return int
*/ */
public function getDelay(){ public function getDelay() : int{
return $this->delay; return $this->delay;
} }
/** /**
* @return bool * @return bool
*/ */
public function isDelayed(){ public function isDelayed() : bool{
return $this->delay > 0; return $this->delay > 0;
} }
/** /**
* @return bool * @return bool
*/ */
public function isRepeating(){ public function isRepeating() : bool{
return $this->period > 0; return $this->period > 0;
} }
/** /**
* @return int * @return int
*/ */
public function getPeriod(){ public function getPeriod() : int{
return $this->period; return $this->period;
} }
@ -150,14 +150,14 @@ class TaskHandler{
/** /**
* @param int $currentTick * @param int $currentTick
*/ */
public function run($currentTick){ public function run(int $currentTick){
$this->task->onRun($currentTick); $this->task->onRun($currentTick);
} }
/** /**
* @return string * @return string
*/ */
public function getTaskName(){ public function getTaskName() : string{
if($this->timingName !== null){ if($this->timingName !== null){
return $this->timingName; return $this->timingName;
} }

View File

@ -28,20 +28,17 @@ class UUID{
private $parts = [0, 0, 0, 0]; private $parts = [0, 0, 0, 0];
private $version = null; private $version = null;
public function __construct($part1 = 0, $part2 = 0, $part3 = 0, $part4 = 0, $version = null){ public function __construct(int $part1 = 0, int $part2 = 0, int $part3 = 0, int $part4 = 0, int $version = null){
$this->parts[0] = (int) $part1; $this->parts = [$part1, $part2, $part3, $part4];
$this->parts[1] = (int) $part2;
$this->parts[2] = (int) $part3;
$this->parts[3] = (int) $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; 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]; 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 * @param int $version
* @return UUID * @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); return self::fromBinary(hex2bin(str_replace("-", "", trim($uuid))), $version);
} }
@ -62,8 +59,10 @@ class UUID{
* @param string $uuid * @param string $uuid
* @param int $version * @param int $version
* @return UUID * @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){ if(strlen($uuid) !== 16){
throw new \InvalidArgumentException("Must have exactly 16 bytes"); throw new \InvalidArgumentException("Must have exactly 16 bytes");
} }
@ -77,21 +76,21 @@ class UUID{
* @param string[] ...$data * @param string[] ...$data
* @return UUID * @return UUID
*/ */
public static function fromData(string ...$data){ public static function fromData(string ...$data) : UUID{
$hash = hash("md5", implode($data), true); $hash = hash("md5", implode($data), true);
return self::fromBinary($hash, 3); 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))); 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]); 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()); $hex = bin2hex($this->toBinary());
//xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx 8-4-4-4-12 //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); 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(); return $this->toString();
} }

View File

@ -33,6 +33,11 @@ class VersionString{
private $minor; private $minor;
private $development = false; private $development = false;
/**
* VersionString constructor.
*
* @param int|string $version
*/
public function __construct($version = \pocketmine\VERSION){ public function __construct($version = \pocketmine\VERSION){
if(is_int($version)){ if(is_int($version)){
$this->minor = $version & 0x1F; $this->minor = $version & 0x1F;
@ -40,9 +45,9 @@ class VersionString{
$this->generation = ($version >> 9) & 0x0F; $this->generation = ($version >> 9) & 0x0F;
}else{ }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); $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->generation = (int) ($version[2] ?? 0); //0-15
$this->major = isset($version[3]) ? (int) $version[3] : 0; //0-15 $this->major = (int) ($version[3] ?? 0); //0-15
$this->minor = isset($version[4]) ? (int) $version[4] : 0; //0-31 $this->minor = (int) ($version[4] ?? 0); //0-31
$this->development = $version[5] === "dev"; $this->development = $version[5] === "dev";
if($version[6] !== ""){ if($version[6] !== ""){
$this->build = (int) (substr($version[6], 1)); $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); return (int) (($this->generation << 9) + ($this->major << 5) + $this->minor);
} }
public function getGeneration(){ public function getGeneration() : int{
return $this->generation; return $this->generation;
} }
public function getMajor(){ public function getMajor() : int{
return $this->major; return $this->major;
} }
public function getMinor(){ public function getMinor() : int{
return $this->minor; return $this->minor;
} }
public function getRelease(){ public function getRelease() : string{
return $this->generation . "." . $this->major . ($this->minor > 0 ? "." . $this->minor : ""); return $this->generation . "." . $this->major . ($this->minor > 0 ? "." . $this->minor : "");
} }
public function getBuild(){ public function getBuild() : int{
return $this->build; return $this->build;
} }
public function isDev(){ public function isDev() : bool{
return $this->development === true; 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 : ""); 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(); 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){ if(($target instanceof VersionString) === false){
$target = new VersionString($target); $target = new VersionString($target);
} }