From 537721fe7d845bd369fe456ae0f28fc40e8ff4ef Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 19 Jul 2023 13:34:42 +0100 Subject: [PATCH] Replace Closure::fromCallable() usages with first-class callables PHP 8.1 <3 --- src/block/Liquid.php | 2 +- src/crafting/CraftingManagerFromDataHelper.php | 11 ++++------- src/network/mcpe/InventoryManager.php | 2 +- src/network/mcpe/NetworkSession.php | 4 ++-- src/world/World.php | 6 +++--- src/world/format/io/WorldProviderManager.php | 8 ++++---- .../phpstan/rules/UnsafeForeachArrayOfStringRule.php | 2 +- 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 98f1d5627..a56f666a2 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -312,7 +312,7 @@ abstract class Liquid extends Transparent{ } if($adjacentDecay <= self::MAX_DECAY){ - $calculator = new MinimumCostFlowCalculator($world, $this->getFlowDecayPerBlock(), \Closure::fromCallable([$this, 'canFlowInto'])); + $calculator = new MinimumCostFlowCalculator($world, $this->getFlowDecayPerBlock(), $this->canFlowInto(...)); foreach($calculator->getOptimalFlowDirections($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) as $facing){ $this->flowIntoBlock($world->getBlock($this->position->getSide($facing)), $adjacentDecay, false); } diff --git a/src/crafting/CraftingManagerFromDataHelper.php b/src/crafting/CraftingManagerFromDataHelper.php index 0a28ca328..812ff83e0 100644 --- a/src/crafting/CraftingManagerFromDataHelper.php +++ b/src/crafting/CraftingManagerFromDataHelper.php @@ -209,9 +209,6 @@ final class CraftingManagerFromDataHelper{ public static function make(string $directoryPath) : CraftingManager{ $result = new CraftingManager(); - $ingredientDeserializerFunc = \Closure::fromCallable([self::class, "deserializeIngredient"]); - $itemDeserializerFunc = \Closure::fromCallable([self::class, 'deserializeItemStack']); - foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'shapeless_crafting.json'), ShapelessRecipeData::class) as $recipe){ $recipeType = match($recipe->block){ "crafting_table" => ShapelessRecipeType::CRAFTING(), @@ -225,7 +222,7 @@ final class CraftingManagerFromDataHelper{ } $inputs = []; foreach($recipe->input as $inputData){ - $input = $ingredientDeserializerFunc($inputData); + $input = self::deserializeIngredient($inputData); if($input === null){ //unknown input item continue 2; } @@ -233,7 +230,7 @@ final class CraftingManagerFromDataHelper{ } $outputs = []; foreach($recipe->output as $outputData){ - $output = $itemDeserializerFunc($outputData); + $output = self::deserializeItemStack($outputData); if($output === null){ //unknown output item continue 2; } @@ -251,7 +248,7 @@ final class CraftingManagerFromDataHelper{ } $inputs = []; foreach(Utils::stringifyKeys($recipe->input) as $symbol => $inputData){ - $input = $ingredientDeserializerFunc($inputData); + $input = self::deserializeIngredient($inputData); if($input === null){ //unknown input item continue 2; } @@ -259,7 +256,7 @@ final class CraftingManagerFromDataHelper{ } $outputs = []; foreach($recipe->output as $outputData){ - $output = $itemDeserializerFunc($outputData); + $output = self::deserializeItemStack($outputData); if($output === null){ //unknown output item continue 2; } diff --git a/src/network/mcpe/InventoryManager.php b/src/network/mcpe/InventoryManager.php index a94dbe946..316abfd3e 100644 --- a/src/network/mcpe/InventoryManager.php +++ b/src/network/mcpe/InventoryManager.php @@ -108,7 +108,7 @@ class InventoryManager{ private NetworkSession $session ){ $this->containerOpenCallbacks = new ObjectSet(); - $this->containerOpenCallbacks->add(\Closure::fromCallable([self::class, 'createContainerOpen'])); + $this->containerOpenCallbacks->add(self::createContainerOpen(...)); $this->add(ContainerIds::INVENTORY, $this->player->getInventory()); $this->add(ContainerIds::OFFHAND, $this->player->getOffHandInventory()); diff --git a/src/network/mcpe/NetworkSession.php b/src/network/mcpe/NetworkSession.php index b10e7f25a..252db34cf 100644 --- a/src/network/mcpe/NetworkSession.php +++ b/src/network/mcpe/NetworkSession.php @@ -225,13 +225,13 @@ class NetworkSession{ $this->logger->setPrefix($this->getLogPrefix()); $this->manager->markLoginReceived($this); }, - \Closure::fromCallable([$this, "setAuthenticationStatus"]) + $this->setAuthenticationStatus(...) )); } protected function createPlayer() : void{ $this->server->createPlayer($this, $this->info, $this->authenticated, $this->cachedOfflinePlayerData)->onCompletion( - \Closure::fromCallable([$this, 'onPlayerCreated']), + $this->onPlayerCreated(...), function() : void{ //TODO: this should never actually occur... right? $this->logger->error("Failed to create player"); diff --git a/src/world/World.php b/src/world/World.php index deb03ef6d..f556d2e1f 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1610,7 +1610,7 @@ class World implements ChunkManager{ * the current weather and time of day. */ public function getHighestAdjacentFullLightAt(int $x, int $y, int $z) : int{ - return $this->getHighestAdjacentLight($x, $y, $z, \Closure::fromCallable([$this, 'getFullLightAt'])); + return $this->getHighestAdjacentLight($x, $y, $z, $this->getFullLightAt(...)); } /** @@ -1706,7 +1706,7 @@ class World implements ChunkManager{ * Returns the highest potential level of sky light in the positions adjacent to the specified block coordinates. */ public function getHighestAdjacentPotentialBlockSkyLight(int $x, int $y, int $z) : int{ - return $this->getHighestAdjacentLight($x, $y, $z, \Closure::fromCallable([$this, 'getPotentialBlockSkyLightAt'])); + return $this->getHighestAdjacentLight($x, $y, $z, $this->getPotentialBlockSkyLightAt(...)); } /** @@ -1721,7 +1721,7 @@ class World implements ChunkManager{ * Returns the highest block light level available in the positions adjacent to the specified block coordinates. */ public function getHighestAdjacentBlockLight(int $x, int $y, int $z) : int{ - return $this->getHighestAdjacentLight($x, $y, $z, \Closure::fromCallable([$this, 'getBlockLightAt'])); + return $this->getHighestAdjacentLight($x, $y, $z, $this->getBlockLightAt(...)); } private function executeQueuedLightUpdates() : void{ diff --git a/src/world/format/io/WorldProviderManager.php b/src/world/format/io/WorldProviderManager.php index 8a30bcb57..1767c539a 100644 --- a/src/world/format/io/WorldProviderManager.php +++ b/src/world/format/io/WorldProviderManager.php @@ -41,13 +41,13 @@ final class WorldProviderManager{ private WritableWorldProviderManagerEntry $default; public function __construct(){ - $leveldb = new WritableWorldProviderManagerEntry(\Closure::fromCallable([LevelDB::class, 'isValid']), fn(string $path, \Logger $logger) => new LevelDB($path, $logger), \Closure::fromCallable([LevelDB::class, 'generate'])); + $leveldb = new WritableWorldProviderManagerEntry(LevelDB::isValid(...), fn(string $path, \Logger $logger) => new LevelDB($path, $logger), LevelDB::generate(...)); $this->default = $leveldb; $this->addProvider($leveldb, "leveldb"); - $this->addProvider(new ReadOnlyWorldProviderManagerEntry(\Closure::fromCallable([Anvil::class, 'isValid']), fn(string $path, \Logger $logger) => new Anvil($path, $logger)), "anvil"); - $this->addProvider(new ReadOnlyWorldProviderManagerEntry(\Closure::fromCallable([McRegion::class, 'isValid']), fn(string $path, \Logger $logger) => new McRegion($path, $logger)), "mcregion"); - $this->addProvider(new ReadOnlyWorldProviderManagerEntry(\Closure::fromCallable([PMAnvil::class, 'isValid']), fn(string $path, \Logger $logger) => new PMAnvil($path, $logger)), "pmanvil"); + $this->addProvider(new ReadOnlyWorldProviderManagerEntry(Anvil::isValid(...), fn(string $path, \Logger $logger) => new Anvil($path, $logger)), "anvil"); + $this->addProvider(new ReadOnlyWorldProviderManagerEntry(McRegion::isValid(...), fn(string $path, \Logger $logger) => new McRegion($path, $logger)), "mcregion"); + $this->addProvider(new ReadOnlyWorldProviderManagerEntry(PMAnvil::isValid(...), fn(string $path, \Logger $logger) => new PMAnvil($path, $logger)), "pmanvil"); } /** diff --git a/tests/phpstan/rules/UnsafeForeachArrayOfStringRule.php b/tests/phpstan/rules/UnsafeForeachArrayOfStringRule.php index d53baa365..e42d32927 100644 --- a/tests/phpstan/rules/UnsafeForeachArrayOfStringRule.php +++ b/tests/phpstan/rules/UnsafeForeachArrayOfStringRule.php @@ -78,7 +78,7 @@ final class UnsafeForeachArrayOfStringRule implements Rule{ return $type; }); if($hasCastableKeyTypes && !$expectsIntKeyTypes){ - $func = \Closure::fromCallable([Utils::class, 'stringifyKeys']); + $func = Utils::stringifyKeys(...); return [ RuleErrorBuilder::message(sprintf( "Unsafe foreach on array with key type %s (they might be casted to int).",