Skip to content

Commit 1d88001

Browse files
committed
Replace isset() calls with array_key_exists()
Besides being faster (since php/php-src#3360), array_key_exists is also safer as it narrows down key existence to arrays only and performs type-checking on both parameters.
1 parent ab24538 commit 1d88001

20 files changed

+45
-45
lines changed

src/muqsit/vanillagenerator/generator/VanillaBiomeGrid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class VanillaBiomeGrid implements BiomeGrid{
1313

1414
public function getBiome(int $x, int $z) : ?int{
1515
// upcasting is very important to get extended biomes
16-
return isset($this->biomes[$hash = $x | $z << 4]) ? $this->biomes[$hash] & 0xFF : null;
16+
return array_key_exists($hash = $x | $z << 4, $this->biomes) ? $this->biomes[$hash] & 0xFF : null;
1717
}
1818

1919
public function setBiome(int $x, int $z, int $biomeId) : void{

src/muqsit/vanillagenerator/generator/biomegrid/BiomeEdgeMapLayer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,27 @@ public function generateValues(int $x, int $z, int $sizeX, int $sizeZ) : array{
7070
$centerVal = $values[$j + 1 + ($i + 1) * $gridSizeX];
7171
$val = $centerVal;
7272
foreach(self::$EDGES as $edge){ // [$map, $entry]
73-
if(isset($edge->key[$centerVal])){
73+
if(array_key_exists($centerVal, $edge->key)){
7474
$upperVal = $values[$j + 1 + $i * $gridSizeX];
7575
$lowerVal = $values[$j + 1 + ($i + 2) * $gridSizeX];
7676
$leftVal = $values[$j + ($i + 1) * $gridSizeX];
7777
$rightVal = $values[$j + 2 + ($i + 1) * $gridSizeX];
7878

7979
if($edge->value === null && (
80-
!isset($edge->key[$upperVal])
81-
|| !isset($edge->key[$lowerVal])
82-
|| !isset($edge->key[$leftVal])
83-
|| !isset($edge->key[$rightVal])
80+
!array_key_exists($upperVal, $edge->key)
81+
|| !array_key_exists($lowerVal, $edge->key)
82+
|| !array_key_exists($leftVal, $edge->key)
83+
|| !array_key_exists($rightVal, $edge->key)
8484
)){
8585
$val = $edge->key[$centerVal];
8686
break;
8787
}
8888

8989
if($edge->value !== null && (
90-
isset($edge->value[$upperVal]) ||
91-
isset($edge->value[$lowerVal]) ||
92-
isset($edge->value[$leftVal]) ||
93-
isset($edge->value[$rightVal])
90+
array_key_exists($upperVal, $edge->value) ||
91+
array_key_exists($lowerVal, $edge->value) ||
92+
array_key_exists($leftVal, $edge->value) ||
93+
array_key_exists($rightVal, $edge->value)
9494
)){
9595
$val = $edge->key[$centerVal];
9696
break;

src/muqsit/vanillagenerator/generator/biomegrid/BiomeThinEdgeMapLayer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ public function generateValues(int $x, int $z, int $sizeX, int $sizeZ) : array{
6363
$centerVal = $values[$j + 1 + ($i + 1) * $gridSizeX];
6464
$val = $centerVal;
6565
foreach(self::$EDGES as $edge){
66-
if(isset($edge->key[$centerVal])){
66+
if(array_key_exists($centerVal, $edge->key)){
6767
$upperVal = $values[$j + 1 + $i * $gridSizeX];
6868
$lowerVal = $values[$j + 1 + ($i + 2) * $gridSizeX];
6969
$leftVal = $values[$j + ($i + 1) * $gridSizeX];
7070
$rightVal = $values[$j + 2 + ($i + 1) * $gridSizeX];
7171
if($edge->value === null && (
72-
(!isset(self::$OCEANS[$upperVal]) && !isset($edge->key[$upperVal]))
73-
|| (!isset(self::$OCEANS[$lowerVal]) && !isset($edge->key[$lowerVal]))
74-
|| (!isset(self::$OCEANS[$leftVal]) && !isset($edge->key[$leftVal]))
75-
|| (!isset(self::$OCEANS[$rightVal]) && !isset($edge->key[$rightVal]))
72+
(!array_key_exists($upperVal, self::$OCEANS) && !array_key_exists($upperVal, $edge->key))
73+
|| (!array_key_exists($lowerVal, self::$OCEANS) && !array_key_exists($lowerVal, $edge->key))
74+
|| (!array_key_exists($leftVal, self::$OCEANS) && !array_key_exists($leftVal, $edge->key))
75+
|| (!array_key_exists($rightVal, self::$OCEANS) && !array_key_exists($rightVal, $edge->key))
7676
)){
7777
$val = $edge->key[$centerVal];
7878
break;
7979
}
8080
if($edge->value !== null && (
81-
(!isset(self::$OCEANS[$upperVal]) && !isset($edge->value[$upperVal]))
82-
|| (!isset(self::$OCEANS[$lowerVal]) && !isset($edge->value[$lowerVal]))
83-
|| (!isset(self::$OCEANS[$leftVal]) && !isset($edge->value[$leftVal]))
84-
|| (!isset(self::$OCEANS[$rightVal]) && !isset($edge->value[$rightVal]))
81+
(!array_key_exists($upperVal, self::$OCEANS) && !array_key_exists($upperVal, $edge->value))
82+
|| (!array_key_exists($lowerVal, self::$OCEANS) && !array_key_exists($lowerVal, $edge->value))
83+
|| (!array_key_exists($leftVal, self::$OCEANS) && !array_key_exists($leftVal, $edge->value))
84+
|| (!array_key_exists($rightVal, self::$OCEANS) && !array_key_exists($rightVal, $edge->value))
8585
)){
8686
$val = $edge->key[$centerVal];
8787
break;

src/muqsit/vanillagenerator/generator/biomegrid/BiomeVariationMapLayer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ public function mergeValues(int $x, int $z, int $sizeX, int $sizeZ) : array{
114114
$centerValue = $values[$j + 1 + ($i + 1) * $gridSizeX];
115115
$variationValue = $variationValues[$j + 1 + ($i + 1) * $gridSizeX];
116116
if($centerValue !== 0 && $variationValue === 3 && $centerValue < 128){
117-
$finalValues[$j + $i * $sizeX] = isset(self::$BIOMES[$centerValue + 128]) ? $centerValue + 128 : $centerValue;
117+
$finalValues[$j + $i * $sizeX] = array_key_exists($centerValue + 128, self::$BIOMES) ? $centerValue + 128 : $centerValue;
118118
}elseif($variationValue === 2 || $this->nextInt(3) === 0){
119119
$val = $centerValue;
120-
if(isset(self::$VARIATIONS[$centerValue])){
120+
if(array_key_exists($centerValue, self::$VARIATIONS)){
121121
$val = self::$VARIATIONS[$centerValue][$this->nextInt(count(self::$VARIATIONS[$centerValue]))];
122122
}elseif($centerValue === BiomeIds::DEEP_OCEAN && $this->nextInt(3) === 0){
123123
$val = self::$ISLANDS[$this->nextInt(count(self::$ISLANDS))];
124124
}
125125
if($variationValue === 2 && $val !== $centerValue){
126-
$val = isset(self::$BIOMES[$val + 128]) ? $val + 128 : $centerValue;
126+
$val = array_key_exists($val + 128, self::$BIOMES) ? $val + 128 : $centerValue;
127127
}
128128
if($val !== $centerValue){
129129
$count = 0;

src/muqsit/vanillagenerator/generator/biomegrid/RarePlainsMapLayer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function generateValues(int $x, int $z, int $sizeX, int $sizeZ) : array{
3232
for($j = 0; $j < $sizeX; ++$j){
3333
$this->setCoordsSeed($x + $j, $z + $i);
3434
$centerValue = $values[$j + 1 + ($i + 1) * $gridSizeX];
35-
if($this->nextInt(57) === 0 && isset(self::$RARE_PLAINS[$centerValue])){
35+
if($this->nextInt(57) === 0 && array_key_exists($centerValue, self::$RARE_PLAINS)){
3636
$centerValue = self::$RARE_PLAINS[$centerValue];
3737
}
3838

src/muqsit/vanillagenerator/generator/biomegrid/RiverMapLayer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private function mergeRivers(int $x, int $z, int $sizeX, int $sizeZ) : array{
9191
$finalValues = [];
9292
for($i = 0; $i < $sizeX * $sizeZ; ++$i){
9393
$val = $mergeValues[$i];
94-
if(isset(self::$OCEANS[$mergeValues[$i]])){
94+
if(array_key_exists($mergeValues[$i], self::$OCEANS)){
9595
$val = $mergeValues[$i];
9696
}elseif($values[$i] === self::$RIVER_VALUE){
9797
$val = self::$SPECIAL_RIVERS[$mergeValues[$i]] ?? BiomeIds::RIVER;

src/muqsit/vanillagenerator/generator/biomegrid/ShoreMapLayer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function generateValues(int $x, int $z, int $sizeX, int $sizeZ) : array{
6464
$leftVal = $values[$j + ($i + 1) * $gridSizeX];
6565
$rightVal = $values[$j + 2 + ($i + 1) * $gridSizeX];
6666
$centerVal = $values[$j + 1 + ($i + 1) * $gridSizeX];
67-
if(!isset(self::$OCEANS[$centerVal]) && (
68-
isset(self::$OCEANS[$upperVal]) || isset(self::$OCEANS[$lowerVal])
69-
|| isset(self::$OCEANS[$leftVal]) || isset(self::$OCEANS[$rightVal])
67+
if(!array_key_exists($centerVal, self::$OCEANS) && (
68+
array_key_exists($upperVal, self::$OCEANS) || array_key_exists($lowerVal, self::$OCEANS)
69+
|| array_key_exists($leftVal, self::$OCEANS) || array_key_exists($rightVal, self::$OCEANS)
7070
)){
7171
$finalValues[$j + $i * $sizeX] = self::$SPECIAL_SHORES[$centerVal] ?? BiomeIds::BEACH;
7272
}else{

src/muqsit/vanillagenerator/generator/nether/decorator/MushroomDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function decorate(ChunkManager $world, Random $random, int $chunkX, int $
4747
if(
4848
$y < $height &&
4949
$block->getId() === BlockLegacyIds::AIR &&
50-
isset(self::$MATERIALS[$blockBelow->getId()])
50+
array_key_exists($blockBelow->getId(), self::$MATERIALS)
5151
){
5252
$world->setBlockAt($x, $y, $z, $this->type);
5353
}

src/muqsit/vanillagenerator/generator/object/BlockPatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function generate(ChunkManager $world, Random $random, int $sourceX, int
5151
}
5252
for($y = $sourceY - $this->vertRadius; $y <= $sourceY + $this->vertRadius; ++$y){
5353
$block = $world->getBlockAt($x, $y, $z);
54-
if(!isset($this->overridables[$block->getFullId()])){
54+
if(!array_key_exists($block->getFullId(), $this->overridables)){
5555
continue;
5656
}
5757

src/muqsit/vanillagenerator/generator/object/IceSpike.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function generate(ChunkManager $world, Random $random, int $sourceX, int
4242
}
4343
for($y = $tipOffset - 1; $y >= -3; --$y){
4444
$block = $world->getBlockAt($sourceX + $x, $sourceY + $y, $sourceZ + $z);
45-
if(isset(self::$MATERIALS[$block->getId()])){
45+
if(array_key_exists($block->getId(), self::$MATERIALS)){
4646
$world->setBlockAt($sourceX + $x, $sourceY + $y, $sourceZ + $z, VanillaBlocks::PACKED_ICE());
4747
--$stackHeight;
4848
if($stackHeight <= 0){
@@ -69,12 +69,12 @@ public function generate(ChunkManager $world, Random $random, int $sourceX, int
6969
continue;
7070
}
7171
// tip shape in top direction
72-
if(isset(self::$MATERIALS[$world->getBlockAt($sourceX + $x, $sourceY + $tipOffset + $y, $sourceZ + $z)->getId()])){
72+
if(array_key_exists($world->getBlockAt($sourceX + $x, $sourceY + $tipOffset + $y, $sourceZ + $z)->getId(), self::$MATERIALS)){
7373
$world->setBlockAt($sourceX + $x, $sourceY + $tipOffset + $y, $sourceZ + $z, VanillaBlocks::PACKED_ICE());
7474
$succeeded = true;
7575
}
7676
if($radius > 1 && $y !== 0){ // same shape in bottom direction
77-
if(isset(self::$MATERIALS[$world->getBlockAt($sourceX + $x, $sourceY + $tipOffset - $y, $sourceZ + $z)->getId()])){
77+
if(array_key_exists($world->getBlockAt($sourceX + $x, $sourceY + $tipOffset - $y, $sourceZ + $z)->getId(), self::$MATERIALS)){
7878
$world->setBlockAt($sourceX + $x, $sourceY + $tipOffset - $y, $sourceZ + $z, VanillaBlocks::PACKED_ICE());
7979
$succeeded = true;
8080
}

src/muqsit/vanillagenerator/generator/object/Lake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function generate(ChunkManager $world, Random $random, int $sourceX, int
7373
/** @var Chunk $chunk */
7474
$chunk = $world->getChunk($sourceX >> 4, $sourceZ >> 4);
7575
$biome = $chunk->getBiomeId(($sourceX + 8 + (int) self::MAX_DIAMETER / 2) & 0x0f, ($sourceZ + 8 + (int) self::MAX_DIAMETER / 2) & 0x0f);
76-
$mycelBiome = isset(self::$MYCEL_BIOMES[$biome]);
76+
$mycelBiome = array_key_exists($biome, self::$MYCEL_BIOMES);
7777

7878
$max_diameter = (int) self::MAX_DIAMETER;
7979
for($x = 0; $x < $max_diameter; ++$x){

src/muqsit/vanillagenerator/generator/object/StoneBoulder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function generate(ChunkManager $world, Random $random, int $sourceX, int
3030
continue;
3131
}
3232

33-
if(isset(self::$GROUND_TYPES[$block->getId()])){
33+
if(array_key_exists($block->getId(), self::$GROUND_TYPES)){
3434
$groundReached = true;
3535
++$sourceY;
3636
break;

src/muqsit/vanillagenerator/generator/object/tree/BigOakTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function generate(ChunkManager $world, Random $random, int $blockX, int $
6464
for($z = -$nodeDistance; $z <= $nodeDistance; ++$z){
6565
$sizeX = abs($x) + 0.5;
6666
$sizeZ = abs($z) + 0.5;
67-
if($sizeX * $sizeX + $sizeZ * $sizeZ <= $size * $size && isset($this->overridables[$world->getBlockAt($node->x + $x, $node->y + $y, $node->z + $z)->getId()])){
67+
if($sizeX * $sizeX + $sizeZ * $sizeZ <= $size * $size && array_key_exists($world->getBlockAt($node->x + $x, $node->y + $y, $node->z + $z)->getId(), $this->overridables)){
6868
$this->transaction->addBlockAt($node->x + $x, $node->y + $y, $node->z + $z, $this->leavesType);
6969
}
7070
}
@@ -117,7 +117,7 @@ private function countAvailableBlocks(Vector3 $from, Vector3 $to, ChunkManager $
117117
for($i = 0; $i <= $maxDistance; ++$i, ++$n){
118118
$target = $from->add(0.5 + $i * $dx, 0.5 + $i * $dy, 0.5 + $i * $dz);
119119
$target_floorY = $target->getFloorY();
120-
if($target_floorY < 0 || $target_floorY > $height || !isset($this->overridables[$world->getBlockAt($target->getFloorX(), $target->getFloorY(), $target->getFloorZ())->getId()])){
120+
if($target_floorY < 0 || $target_floorY > $height || !array_key_exists($world->getBlockAt($target->getFloorX(), $target->getFloorY(), $target->getFloorZ())->getId(), $this->overridables)){
121121
return $n;
122122
}
123123
}

src/muqsit/vanillagenerator/generator/object/tree/BrownMushroomTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function canPlace(int $baseX, int $baseY, int $baseZ, ChunkManager $world
5858
// skip source block check
5959
if($y !== $baseY || $x !== $baseX || $z !== $baseZ){
6060
// we can overlap leaves around
61-
if(!isset($this->overridables[$world->getBlockAt($x, $y, $z)->getId()])){
61+
if(!array_key_exists($world->getBlockAt($x, $y, $z)->getId(), $this->overridables)){
6262
return false;
6363
}
6464
}

src/muqsit/vanillagenerator/generator/object/tree/GenericTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function canPlace(int $baseX, int $baseY, int $baseZ, ChunkManager $world
118118
for($z = $baseZ - $radius; $z <= $baseZ + $radius; ++$z){
119119
if($y >= 0 && $y < $height){
120120
// we can overlap some blocks around
121-
if(!isset($this->overridables[$world->getBlockAt($x, $y, $z)->getId()])){
121+
if(!array_key_exists($world->getBlockAt($x, $y, $z)->getId(), $this->overridables)){
122122
return false;
123123
}
124124
}else{ // height out of range

src/muqsit/vanillagenerator/generator/object/tree/MegaJungleTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function canPlace(int $baseX, int $baseY, int $baseZ, ChunkManager $world
4747
for($z = $baseZ - $radius; $z <= $baseZ + $radius; ++$z){
4848
if($y >= 0 && $y < World::Y_MAX){
4949
// we can overlap some blocks around
50-
if(!isset($this->overridables[$world->getBlockAt($x, $y, $z)->getId()])){
50+
if(!array_key_exists($world->getBlockAt($x, $y, $z)->getId(), $this->overridables)){
5151
return false;
5252
}
5353
}else{ // height out of range

src/muqsit/vanillagenerator/generator/object/tree/RedwoodTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function canPlace(int $baseX, int $baseY, int $baseZ, ChunkManager $world
5353
if($y >= 0 && $y < World::Y_MAX){
5454
// we can overlap some blocks around
5555
$type = $world->getBlockAt($x, $y, $z)->getId();
56-
if(!isset($this->overridables[$type])){
56+
if(!array_key_exists($type, $this->overridables)){
5757
return false;
5858
}
5959
}else{ // $this->height out of range
@@ -105,7 +105,7 @@ public function generate(ChunkManager $world, Random $random, int $blockX, int $
105105
// generate the trunk
106106
for($y = 0; $y < $this->height - $random->nextBoundedInt(3); $y++){
107107
$type = $world->getBlockAt($blockX, $blockY + $y, $blockZ)->getId();
108-
if(isset($this->overridables[$type])){
108+
if(array_key_exists($type, $this->overridables)){
109109
$this->transaction->addBlockAt($blockX, $blockY + $y, $blockZ, $this->logType);
110110
}
111111
}

src/muqsit/vanillagenerator/generator/object/tree/SwampTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function canPlace(int $baseX, int $baseY, int $baseZ, ChunkManager $world
5757
for($z = $baseZ - $radius; $z <= $baseZ + $radius; ++$z){
5858
// we can overlap some blocks around
5959
$type = $world->getBlockAt($x, $y, $z)->getId();
60-
if(isset($this->overridables[$type])){
60+
if(array_key_exists($type, $this->overridables)){
6161
continue;
6262
}
6363

@@ -80,7 +80,7 @@ public function generate(ChunkManager $world, Random $random, int $blockX, int $
8080
$chunk_block_x = $blockX & 0x0f;
8181
$chunk_block_z = $blockZ & 0x0f;
8282
$block_factory = BlockFactory::getInstance();
83-
while(isset(self::$WATER_BLOCK_TYPES[$block_factory->fromFullBlock($chunk->getFullBlock($chunk_block_x, $blockY, $chunk_block_z))->getId()])){
83+
while(array_key_exists($block_factory->fromFullBlock($chunk->getFullBlock($chunk_block_x, $blockY, $chunk_block_z))->getId(), self::$WATER_BLOCK_TYPES)){
8484
--$blockY;
8585
}
8686

src/muqsit/vanillagenerator/generator/overworld/OverworldGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function generateChunkData(ChunkManager $world, int $chunkX, int $chun
146146
for($x = 0; $x < $sizeX; ++$x){
147147
for($z = 0; $z < $sizeZ; ++$z){
148148
$chunk->setBiomeId($x, $z, $id = $grid->getBiome($x, $z));
149-
if(isset(self::$GROUND_MAP[$id])){
149+
if(array_key_exists($id, self::$GROUND_MAP)){
150150
self::$GROUND_MAP[$id]->generateTerrainColumn($world, $this->random, $cx + $x, $cz + $z, $id, $surfaceNoise[$x | $z << 4]);
151151
}else{
152152
$this->groundGen->generateTerrainColumn($world, $this->random, $cx + $x, $cz + $z, $id, $surfaceNoise[$x | $z << 4]);

src/muqsit/vanillagenerator/generator/overworld/populator/OverworldPopulator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(){
7272

7373
public function populate(ChunkManager $world, Random $random, int $chunkX, int $chunkZ, Chunk $chunk) : void{
7474
$biome = $chunk->getBiomeId(8, 8);
75-
if(isset($this->biomePopulators[$biome])){
75+
if(array_key_exists($biome, $this->biomePopulators)){
7676
$this->biomePopulators[$biome]->populate($world, $random, $chunkX, $chunkZ, $chunk);
7777
}
7878
}

0 commit comments

Comments
 (0)