1
0
mirror of https://github.com/pmmp/PocketMine-MP.git synced 2025-05-23 14:04:38 +00:00

added some callable prototypes for phpstan

This commit is contained in:
Dylan K. Taylor 2020-01-29 19:56:16 +00:00
parent 68bea6d4aa
commit 5c4487c980
10 changed files with 64 additions and 7 deletions

@ -44,9 +44,15 @@ class EffectManager{
/** @var bool */ /** @var bool */
protected $onlyAmbientEffects = false; protected $onlyAmbientEffects = false;
/** @var \Closure[] */ /**
* @var \Closure[]
* @phpstan-var (\Closure(EffectInstance, bool $replacesOldEffect) : void)[]
*/
protected $effectAddHooks = []; protected $effectAddHooks = [];
/** @var \Closure[] */ /**
* @var \Closure[]
* @phpstan-var (\Closure(EffectInstance) : void)[]
*/
protected $effectRemoveHooks = []; protected $effectRemoveHooks = [];
public function __construct(Living $entity){ public function __construct(Living $entity){
@ -214,11 +220,17 @@ class EffectManager{
return !empty($this->effects); return !empty($this->effects);
} }
/**
* @phpstan-param \Closure(EffectInstance, bool $replacesOldEffect) : void $closure
*/
public function onEffectAdd(\Closure $closure) : void{ public function onEffectAdd(\Closure $closure) : void{
Utils::validateCallableSignature(function(EffectInstance $effect, bool $replacesOldEffect) : void{}, $closure); Utils::validateCallableSignature(function(EffectInstance $effect, bool $replacesOldEffect) : void{}, $closure);
$this->effectAddHooks[spl_object_id($closure)] = $closure; $this->effectAddHooks[spl_object_id($closure)] = $closure;
} }
/**
* @phpstan-param \Closure(EffectInstance) : void $closure
*/
public function onEffectRemove(\Closure $closure) : void{ public function onEffectRemove(\Closure $closure) : void{
Utils::validateCallableSignature(function(EffectInstance $effect) : void{}, $closure); Utils::validateCallableSignature(function(EffectInstance $effect) : void{}, $closure);
$this->effectRemoveHooks[spl_object_id($closure)] = $closure; $this->effectRemoveHooks[spl_object_id($closure)] = $closure;

@ -48,6 +48,9 @@ class ChunkRequestTask extends AsyncTask{
/** @var string */ /** @var string */
private $tiles = ""; private $tiles = "";
/**
* @phpstan-param (\Closure() : void)|null $onError
*/
public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, ?\Closure $onError = null){ public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, ?\Closure $onError = null){
$this->compressionLevel = Zlib::$LEVEL; $this->compressionLevel = Zlib::$LEVEL;
@ -68,7 +71,10 @@ class ChunkRequestTask extends AsyncTask{
} }
public function onError() : void{ public function onError() : void{
/** @var \Closure|null $hook */ /**
* @var \Closure|null $hook
* @phpstan-var (\Closure() : void)|null $hook
*/
$hook = $this->fetchLocal(self::TLS_KEY_ERROR_HOOK); $hook = $this->fetchLocal(self::TLS_KEY_ERROR_HOOK);
if($hook !== null){ if($hook !== null){
$hook(); $hook();

@ -430,6 +430,9 @@ class NetworkSession{
$this->sender->send($payload, $immediate); $this->sender->send($payload, $immediate);
} }
/**
* @phpstan-param \Closure() : void $func
*/
private function tryDisconnect(\Closure $func, string $reason) : void{ private function tryDisconnect(\Closure $func, string $reason) : void{
if($this->connected and !$this->disconnectGuard){ if($this->connected and !$this->disconnectGuard){
$this->disconnectGuard = true; $this->disconnectGuard = true;
@ -731,6 +734,7 @@ class NetworkSession{
/** /**
* Instructs the networksession to start using the chunk at the given coordinates. This may occur asynchronously. * Instructs the networksession to start using the chunk at the given coordinates. This may occur asynchronously.
* @param \Closure $onCompletion To be called when chunk sending has completed. * @param \Closure $onCompletion To be called when chunk sending has completed.
* @phpstan-param \Closure(int $chunkX, int $chunkZ) : void $onCompletion
*/ */
public function startUsingChunk(int $chunkX, int $chunkZ, \Closure $onCompletion) : void{ public function startUsingChunk(int $chunkX, int $chunkZ, \Closure $onCompletion) : void{
Utils::validateCallableSignature(function(int $chunkX, int $chunkZ){}, $onCompletion); Utils::validateCallableSignature(function(int $chunkX, int $chunkZ){}, $onCompletion);

@ -27,7 +27,10 @@ use pocketmine\utils\Utils;
use function array_push; use function array_push;
class CompressBatchPromise{ class CompressBatchPromise{
/** @var \Closure[] */ /**
* @var \Closure[]
* @phpstan-var (\Closure(self) : void)[]
*/
private $callbacks = []; private $callbacks = [];
/** @var string|null */ /** @var string|null */
@ -36,6 +39,9 @@ class CompressBatchPromise{
/** @var bool */ /** @var bool */
private $cancelled = false; private $cancelled = false;
/**
* @phpstan-param \Closure(self) : void ...$callbacks
*/
public function onResolve(\Closure ...$callbacks) : void{ public function onResolve(\Closure ...$callbacks) : void{
$this->checkCancelled(); $this->checkCancelled();
foreach($callbacks as $callback){ foreach($callbacks as $callback){
@ -65,6 +71,7 @@ class CompressBatchPromise{
/** /**
* @return \Closure[] * @return \Closure[]
* @phpstan-return (\Closure(self) : void)[]
*/ */
public function getResolveCallbacks() : array{ public function getResolveCallbacks() : array{
return $this->callbacks; return $this->callbacks;

@ -499,6 +499,7 @@ class PluginManager{
} }
} }
/** @phpstan-var \ReflectionClass<Event> $eventClass */
$this->registerEvent($eventClass->getName(), $handlerClosure, $priority, $plugin, $handleCancelled); $this->registerEvent($eventClass->getName(), $handlerClosure, $priority, $plugin, $handleCancelled);
} }
} }
@ -507,6 +508,10 @@ class PluginManager{
/** /**
* @param string $event Class name that extends Event * @param string $event Class name that extends Event
* *
* @phpstan-template TEvent of Event
* @phpstan-param class-string<TEvent> $event
* @phpstan-param \Closure(TEvent) : void $handler
*
* @throws \ReflectionException * @throws \ReflectionException
*/ */
public function registerEvent(string $event, \Closure $handler, int $priority, Plugin $plugin, bool $handleCancelled = false) : void{ public function registerEvent(string $event, \Closure $handler, int $priority, Plugin $plugin, bool $handleCancelled = false) : void{

@ -45,7 +45,10 @@ class CancellableClosureTask extends Task{
public const CONTINUE = true; public const CONTINUE = true;
public const CANCEL = false; public const CANCEL = false;
/** @var \Closure */ /**
* @var \Closure
* @phpstan-var \Closure(int $currentTick) : bool
*/
private $closure; private $closure;
/** /**
@ -53,6 +56,8 @@ class CancellableClosureTask extends Task{
* *
* The closure should follow the signature callback(int $currentTick) : bool. The return value will be used to * The closure should follow the signature callback(int $currentTick) : bool. The return value will be used to
* decide whether to continue repeating. * decide whether to continue repeating.
*
* @phpstan-param \Closure(int $currentTick) : bool $closure
*/ */
public function __construct(\Closure $closure){ public function __construct(\Closure $closure){
Utils::validateCallableSignature(function(int $currentTick) : bool{ return false; }, $closure); Utils::validateCallableSignature(function(int $currentTick) : bool{ return false; }, $closure);

@ -197,6 +197,10 @@ class TimingsHandler{
/** /**
* @return mixed the result of the given closure * @return mixed the result of the given closure
*
* @phpstan-template TClosureReturn
* @phpstan-param \Closure() : TClosureReturn $closure
* @phpstan-return TClosureReturn
*/ */
public function time(\Closure $closure){ public function time(\Closure $closure){
$this->startTiming(); $this->startTiming();

@ -131,6 +131,9 @@ class Utils{
return $reflect->getName(); return $reflect->getName();
} }
/**
* @phpstan-return \Closure(object) : object
*/
public static function cloneCallback() : \Closure{ public static function cloneCallback() : \Closure{
return static function(object $o){ return static function(object $o){
return clone $o; return clone $o;

@ -34,7 +34,10 @@ class BlockTransaction{
/** @var Block[][][] */ /** @var Block[][][] */
private $blocks = []; private $blocks = [];
/** @var \Closure[] */ /**
* @var \Closure[]
* @phpstan-var (\Closure(ChunkManager $world, int $x, int $y, int $z) : bool)[]
*/
private $validators = []; private $validators = [];
public function __construct(ChunkManager $world){ public function __construct(ChunkManager $world){
@ -115,6 +118,8 @@ class BlockTransaction{
* Add a validation predicate which will be used to validate every block. * Add a validation predicate which will be used to validate every block.
* The callable signature should be the same as the below dummy function. * The callable signature should be the same as the below dummy function.
* @see BlockTransaction::dummyValidator() * @see BlockTransaction::dummyValidator()
*
* @phpstan-param \Closure(ChunkManager $world, int $x, int $y, int $z) : bool $validator
*/ */
public function addValidator(\Closure $validator) : void{ public function addValidator(\Closure $validator) : void{
Utils::validateCallableSignature([$this, 'dummyValidator'], $validator); Utils::validateCallableSignature([$this, 'dummyValidator'], $validator);

@ -45,7 +45,10 @@ class SubChunkIteratorManager{
/** @var int */ /** @var int */
protected $currentZ; protected $currentZ;
/** @var \Closure|null */ /**
* @var \Closure|null
* @phpstan-var (\Closure() : void)|null
*/
private $onSubChunkChangeFunc = null; private $onSubChunkChangeFunc = null;
public function __construct(ChunkManager $world){ public function __construct(ChunkManager $world){
@ -83,6 +86,9 @@ class SubChunkIteratorManager{
return true; return true;
} }
/**
* @phpstan-param \Closure() : void $callback
*/
public function onSubChunkChange(\Closure $callback) : void{ public function onSubChunkChange(\Closure $callback) : void{
Utils::validateCallableSignature(function(){}, $callback); Utils::validateCallableSignature(function(){}, $callback);
$this->onSubChunkChangeFunc = $callback; $this->onSubChunkChangeFunc = $callback;