0
0
mirror of https://github.com/pmmp/PocketMine-MP.git synced 2025-02-17 23:39:14 +00:00

PHPStan 2.0 fixes

This commit is contained in:
Dylan K. Taylor
2025-01-07 22:10:42 +00:00
parent 7b1b35ab1f
commit cd59e272bc
14 changed files with 43 additions and 33 deletions

@ -110,7 +110,6 @@ class CraftingManager{
/**
* @param Item[] $items
* @phpstan-param list<Item> $items
*
* @return Item[]
* @phpstan-return list<Item>
@ -135,7 +134,6 @@ class CraftingManager{
/**
* @param Item[] $outputs
* @phpstan-param list<Item> $outputs
*/
private static function hashOutputs(array $outputs) : string{
$outputs = self::pack($outputs);

@ -37,7 +37,6 @@ use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path;
use function array_key_last;
use function array_map;
use function array_values;
use function assert;
use function count;
use function get_debug_type;
@ -138,8 +137,8 @@ final class BlockStateUpgradeSchemaUtils{
$convertedRemappedValuesIndex = [];
foreach(Utils::stringifyKeys($model->remappedPropertyValuesIndex ?? []) as $mappingKey => $mappingValues){
foreach($mappingValues as $k => $oldNew){
$convertedRemappedValuesIndex[$mappingKey][$k] = new BlockStateUpgradeSchemaValueRemap(
foreach($mappingValues as $oldNew){
$convertedRemappedValuesIndex[$mappingKey][] = new BlockStateUpgradeSchemaValueRemap(
self::jsonModelToTag($oldNew->old),
self::jsonModelToTag($oldNew->new)
);
@ -361,7 +360,7 @@ final class BlockStateUpgradeSchemaUtils{
//remaps with the same number of criteria should be sorted alphabetically, but this is not strictly necessary
return json_encode($a->oldState ?? []) <=> json_encode($b->oldState ?? []);
});
$result->remappedStates[$oldBlockName] = array_values($keyedRemaps);
$result->remappedStates[$oldBlockName] = $keyedRemaps; //usort strips keys, so this is already a list
}
if(isset($result->remappedStates)){
ksort($result->remappedStates);

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\data\runtime;
use function array_values;
use function ceil;
use function count;
use function log;
@ -60,7 +59,7 @@ final class RuntimeEnumMetadata{
usort($members, fn(\UnitEnum $a, \UnitEnum $b) => $a->name <=> $b->name); //sort by name to ensure consistent ordering (and thus consistent bit assignments)
$this->bits = (int) ceil(log(count($members), 2));
$this->intToEnum = array_values($members);
$this->intToEnum = $members; //usort strips keys so this is already a list
$reversed = [];
foreach($this->intToEnum as $int => $enum){

@ -119,7 +119,7 @@ class HandlerListManager{
public function getHandlersFor(string $event) : array{
$cache = $this->handlerCaches[$event] ?? null;
//getListFor() will populate the cache for the next call
return $cache?->list ?? $this->getListFor($event)->getListenerList();
return $cache->list ?? $this->getListFor($event)->getListenerList();
}
/**

@ -232,7 +232,7 @@ class InventoryTransaction{
/**
* @param SlotChangeAction[] $possibleActions
* @phpstan-param list<SlotChangeAction> $possibleActions
* @phpstan-param array<int, SlotChangeAction> $possibleActions
*/
protected function findResultItem(Item $needOrigin, array $possibleActions) : ?Item{
assert(count($possibleActions) > 0);

@ -101,8 +101,9 @@ abstract class WritableBookBase extends Item{
* @return $this
*/
public function deletePage(int $pageId) : self{
unset($this->pages[$pageId]);
$this->pages = array_values($this->pages);
$newPages = $this->pages;
unset($newPages[$pageId]);
$this->pages = array_values($newPages);
return $this;
}

@ -32,6 +32,7 @@ use function array_merge;
use function array_search;
use function array_shift;
use function array_unique;
use function array_values;
use function count;
/**
@ -103,7 +104,8 @@ final class ItemEnchantmentTagRegistry{
foreach(Utils::stringifyKeys($this->tagMap) as $key => $nestedTags){
if(($nestedKey = array_search($tag, $nestedTags, true)) !== false){
unset($this->tagMap[$key][$nestedKey]);
unset($nestedTags[$nestedKey]);
$this->tagMap[$key] = array_values($nestedTags);
}
}
}
@ -115,7 +117,7 @@ final class ItemEnchantmentTagRegistry{
*/
public function removeNested(string $tag, array $nestedTags) : void{
$this->assertNotInternalTag($tag);
$this->tagMap[$tag] = array_diff($this->tagMap[$tag], $nestedTags);
$this->tagMap[$tag] = array_values(array_diff($this->tagMap[$tag], $nestedTags));
}
/**

@ -147,7 +147,6 @@ use function count;
use function explode;
use function floor;
use function get_class;
use function is_int;
use function max;
use function mb_strlen;
use function microtime;
@ -826,7 +825,6 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$X = null;
$Z = null;
World::getXZ($index, $X, $Z);
assert(is_int($X) && is_int($Z));
++$count;
@ -1346,7 +1344,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->nextChunkOrderRun = 0;
}
if(!$revert && $distanceSquared != 0){
if(!$revert && $distanceSquared !== 0.0){
$dx = $newPos->x - $oldPos->x;
$dy = $newPos->y - $oldPos->y;
$dz = $newPos->z - $oldPos->z;
@ -2319,7 +2317,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$ev = new PlayerQuitEvent($this, $quitMessage ?? $this->getLeaveMessage(), $reason);
$ev->call();
if(($quitMessage = $ev->getQuitMessage()) != ""){
if(($quitMessage = $ev->getQuitMessage()) !== ""){
$this->server->broadcastMessage($quitMessage);
}
$this->save();
@ -2460,7 +2458,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->xpManager->setXpAndProgress(0, 0.0);
}
if($ev->getDeathMessage() != ""){
if($ev->getDeathMessage() !== ""){
$this->server->broadcastMessage($ev->getDeathMessage());
}

@ -47,10 +47,16 @@ class ResourcePackManager{
private string $path;
private bool $serverForceResources = false;
/** @var ResourcePack[] */
/**
* @var ResourcePack[]
* @phpstan-var list<ResourcePack>
*/
private array $resourcePacks = [];
/** @var ResourcePack[] */
/**
* @var ResourcePack[]
* @phpstan-var array<string, ResourcePack>
*/
private array $uuidList = [];
/**
@ -165,6 +171,7 @@ class ResourcePackManager{
/**
* Returns an array of resource packs in use, sorted in order of priority.
* @return ResourcePack[]
* @phpstan-return list<ResourcePack>
*/
public function getResourceStack() : array{
return $this->resourcePacks;

@ -77,7 +77,10 @@ class BulkCurlTask extends AsyncTask{
* @phpstan-var \Closure(list<InternetRequestResult|InternetException>) : void
*/
$callback = $this->fetchLocal(self::TLS_KEY_COMPLETION_CALLBACK);
/** @var InternetRequestResult[]|InternetException[] $results */
/**
* @var InternetRequestResult[]|InternetException[] $results
* @phpstan-var list<InternetRequestResult|InternetException> $results
*/
$results = $this->getResult();
$callback($results);
}

@ -123,6 +123,7 @@ class TimingsHandler{
/**
* @return string[]
* @phpstan-return list<string>
*/
private static function printFooter() : array{
$result = [];
@ -173,6 +174,7 @@ class TimingsHandler{
}
}
/** @phpstan-var PromiseResolver<list<string>> $resolver */
$resolver = new PromiseResolver();
Promise::all($otherThreadRecordPromises)->onCompletion(
function(array $promisedRecords) use ($resolver, $thisThreadRecords) : void{

@ -131,7 +131,7 @@ final class TimingsRecord{
}
public function stopTiming(int $now) : void{
if($this->start == 0){
if($this->start === 0){
return;
}
if(self::$currentRecord !== $this){

@ -113,6 +113,7 @@ use function array_keys;
use function array_map;
use function array_merge;
use function array_sum;
use function array_values;
use function assert;
use function cos;
use function count;
@ -678,7 +679,6 @@ class World implements ChunkManager{
* Used for broadcasting sounds and particles with specific targets.
*
* @param Player[] $allowed
* @phpstan-param list<Player> $allowed
*
* @return array<int, Player>
*/
@ -1089,7 +1089,6 @@ class World implements ChunkManager{
/**
* @param Vector3[] $blocks
* @phpstan-param list<Vector3> $blocks
*
* @return ClientboundPacket[]
* @phpstan-return list<ClientboundPacket>
@ -1456,8 +1455,8 @@ class World implements ChunkManager{
$this->provider->saveChunk($chunkX, $chunkZ, new ChunkData(
$chunk->getSubChunks(),
$chunk->isPopulated(),
array_map(fn(Entity $e) => $e->saveNBT(), array_filter($this->getChunkEntities($chunkX, $chunkZ), fn(Entity $e) => $e->canSaveWithChunk())),
array_map(fn(Tile $t) => $t->saveNBT(), $chunk->getTiles()),
array_map(fn(Entity $e) => $e->saveNBT(), array_values(array_filter($this->getChunkEntities($chunkX, $chunkZ), fn(Entity $e) => $e->canSaveWithChunk()))),
array_map(fn(Tile $t) => $t->saveNBT(), array_values($chunk->getTiles())),
), $chunk->getTerrainDirtyFlags());
$chunk->clearTerrainDirtyFlags();
}
@ -3019,8 +3018,8 @@ class World implements ChunkManager{
$this->provider->saveChunk($x, $z, new ChunkData(
$chunk->getSubChunks(),
$chunk->isPopulated(),
array_map(fn(Entity $e) => $e->saveNBT(), array_filter($this->getChunkEntities($x, $z), fn(Entity $e) => $e->canSaveWithChunk())),
array_map(fn(Tile $t) => $t->saveNBT(), $chunk->getTiles()),
array_map(fn(Entity $e) => $e->saveNBT(), array_values(array_filter($this->getChunkEntities($x, $z), fn(Entity $e) => $e->canSaveWithChunk()))),
array_map(fn(Tile $t) => $t->saveNBT(), array_values($chunk->getTiles())),
), $chunk->getTerrainDirtyFlags());
}finally{
$this->timings->syncChunkSave->stopTiming();

@ -33,10 +33,8 @@ use pocketmine\world\format\io\exception\CorruptedChunkException;
use pocketmine\world\format\io\LoadedChunkData;
use pocketmine\world\format\io\WorldData;
use Symfony\Component\Filesystem\Path;
use function assert;
use function file_exists;
use function is_dir;
use function is_int;
use function morton2d_encode;
use function rename;
use function scandir;
@ -60,7 +58,12 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
public static function isValid(string $path) : bool{
if(file_exists(Path::join($path, "level.dat")) && is_dir($regionPath = Path::join($path, "region"))){
foreach(scandir($regionPath, SCANDIR_SORT_NONE) as $file){
$files = scandir($regionPath, SCANDIR_SORT_NONE);
if($files === false){
//we can't tell the type if we don't have read perms
return false;
}
foreach($files as $file){
$extPos = strrpos($file, ".");
if($extPos !== false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
//we don't care if other region types exist, we only care if this format is possible
@ -199,7 +202,6 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
public function loadChunk(int $chunkX, int $chunkZ) : ?LoadedChunkData{
$regionX = $regionZ = null;
self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
assert(is_int($regionX) && is_int($regionZ));
if(!file_exists($this->pathToRegion($regionX, $regionZ))){
return null;