CallbackInventoryChangeListener: provide signature information for closures

This commit is contained in:
Dylan K. Taylor 2020-01-27 18:08:59 +00:00
parent 42014311c5
commit 85fdb7ec05

View File

@ -27,11 +27,23 @@ use pocketmine\utils\Utils;
class CallbackInventoryChangeListener implements InventoryChangeListener{ class CallbackInventoryChangeListener implements InventoryChangeListener{
/** @var \Closure|null */ //TODO: turn the closure signatures into type aliases when PHPStan supports them
/**
* @var \Closure|null
* @phpstan-var (\Closure(Inventory, int) : void)|null
*/
private $onSlotChangeCallback; private $onSlotChangeCallback;
/** @var \Closure|null */ /**
* @var \Closure|null
* @phpstan-var (\Closure(Inventory) : void)|null
*/
private $onContentChangeCallback; private $onContentChangeCallback;
/**
* @phpstan-param (\Closure(Inventory, int) : void)|null $onSlotChange
* @phpstan-param (\Closure(Inventory) : void)|null $onContentChange
*/
public function __construct(?\Closure $onSlotChange, ?\Closure $onContentChange){ public function __construct(?\Closure $onSlotChange, ?\Closure $onContentChange){
if($onSlotChange !== null){ if($onSlotChange !== null){
Utils::validateCallableSignature(function(Inventory $inventory, int $slot){}, $onSlotChange); Utils::validateCallableSignature(function(Inventory $inventory, int $slot){}, $onSlotChange);
@ -44,6 +56,9 @@ class CallbackInventoryChangeListener implements InventoryChangeListener{
$this->onContentChangeCallback = $onContentChange; $this->onContentChangeCallback = $onContentChange;
} }
/**
* @phpstan-param \Closure(Inventory) : void $onChange
*/
public static function onAnyChange(\Closure $onChange) : self{ public static function onAnyChange(\Closure $onChange) : self{
return new self( return new self(
static function(Inventory $inventory, int $unused) use ($onChange) : void{ static function(Inventory $inventory, int $unused) use ($onChange) : void{