Replace empty() usages with count()

This commit is contained in:
Dylan K. Taylor 2019-12-18 11:06:28 +00:00
parent b08c38f8f9
commit 494660102e
23 changed files with 58 additions and 43 deletions

View File

@ -741,7 +741,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$data->overloads[0][0] = $parameter; $data->overloads[0][0] = $parameter;
$aliases = $command->getAliases(); $aliases = $command->getAliases();
if(!empty($aliases)){ if(count($aliases) > 0){
if(!in_array($data->commandName, $aliases, true)){ if(!in_array($data->commandName, $aliases, true)){
//work around a client bug which makes the original name not show when aliases are used //work around a client bug which makes the original name not show when aliases are used
$aliases[] = $data->commandName; $aliases[] = $data->commandName;
@ -1208,7 +1208,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
$this->loadQueue = $newOrder; $this->loadQueue = $newOrder;
if(!empty($this->loadQueue) or !empty($unloadChunks)){ if(count($this->loadQueue) > 0 or count($unloadChunks) > 0){
$pk = new NetworkChunkPublisherUpdatePacket(); $pk = new NetworkChunkPublisherUpdatePacket();
$pk->x = $this->getFloorX(); $pk->x = $this->getFloorX();
$pk->y = $this->getFloorY(); $pk->y = $this->getFloorY();

View File

@ -140,7 +140,7 @@ namespace pocketmine {
} }
function server(){ function server(){
if(!empty($messages = check_platform_dependencies())){ if(count($messages = check_platform_dependencies()) > 0){
echo PHP_EOL; echo PHP_EOL;
$binary = version_compare(PHP_VERSION, "5.4") >= 0 ? PHP_BINARY : "unknown"; $binary = version_compare(PHP_VERSION, "5.4") >= 0 ? PHP_BINARY : "unknown";
critical_error("Selected PHP binary ($binary) does not satisfy some requirements."); critical_error("Selected PHP binary ($binary) does not satisfy some requirements.");

View File

@ -1212,9 +1212,9 @@ class Server{
} }
$path = $this->getDataPath() . "worlds/" . $name . "/"; $path = $this->getDataPath() . "worlds/" . $name . "/";
if(!($this->getLevelByName($name) instanceof Level)){ if(!($this->getLevelByName($name) instanceof Level)){
return is_dir($path) and !empty(array_filter(scandir($path, SCANDIR_SORT_NONE), function($v){ return is_dir($path) and count(array_filter(scandir($path, SCANDIR_SORT_NONE), function($v){
return $v !== ".." and $v !== "."; return $v !== ".." and $v !== ".";
})); })) > 0;
} }
return true; return true;
@ -1928,14 +1928,14 @@ class Server{
* @param bool $immediate * @param bool $immediate
*/ */
public function batchPackets(array $players, array $packets, bool $forceSync = false, bool $immediate = false){ public function batchPackets(array $players, array $packets, bool $forceSync = false, bool $immediate = false){
if(empty($packets)){ if(count($packets) === 0){
throw new \InvalidArgumentException("Cannot send empty batch"); throw new \InvalidArgumentException("Cannot send empty batch");
} }
Timings::$playerNetworkTimer->startTiming(); Timings::$playerNetworkTimer->startTiming();
$targets = array_filter($players, function(Player $player) : bool{ return $player->isConnected(); }); $targets = array_filter($players, function(Player $player) : bool{ return $player->isConnected(); });
if(!empty($targets)){ if(count($targets) > 0){
$pk = new BatchPacket(); $pk = new BatchPacket();
foreach($packets as $p){ foreach($packets as $p){

View File

@ -40,6 +40,7 @@ use pocketmine\network\mcpe\protocol\types\RuntimeBlockMapping;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use function array_merge; use function array_merge;
use function count;
use function get_class; use function get_class;
use const PHP_INT_MAX; use const PHP_INT_MAX;
@ -727,7 +728,7 @@ class Block extends Position implements BlockIds, Metadatable{
*/ */
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResult{ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResult{
$bbs = $this->getCollisionBoxes(); $bbs = $this->getCollisionBoxes();
if(empty($bbs)){ if(count($bbs) === 0){
return null; return null;
} }

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use function count;
abstract class Fence extends Transparent{ abstract class Fence extends Transparent{
@ -85,7 +86,7 @@ abstract class Fence extends Transparent{
); );
} }
if(empty($bbs)){ if(count($bbs) === 0){
//centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made) //centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
return [ return [
new AxisAlignedBB( new AxisAlignedBB(

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use function count;
abstract class Thin extends Transparent{ abstract class Thin extends Transparent{
@ -77,7 +78,7 @@ abstract class Thin extends Transparent{
); );
} }
if(empty($bbs)){ if(count($bbs) === 0){
//centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made) //centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
return [ return [
new AxisAlignedBB( new AxisAlignedBB(

View File

@ -328,12 +328,12 @@ class SimpleCommandMap implements CommandMap{
} }
} }
if(!empty($recursive)){ if(count($recursive) > 0){
$this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.recursive", [$alias, implode(", ", $recursive)])); $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.recursive", [$alias, implode(", ", $recursive)]));
continue; continue;
} }
if(!empty($bad)){ if(count($bad) > 0){
$this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.notFound", [$alias, implode(", ", $bad)])); $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.notFound", [$alias, implode(", ", $bad)]));
continue; continue;
} }

View File

@ -1102,7 +1102,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
$this->justCreated = false; $this->justCreated = false;
$changedProperties = $this->propertyManager->getDirty(); $changedProperties = $this->propertyManager->getDirty();
if(!empty($changedProperties)){ if(count($changedProperties) > 0){
$this->sendData($this->hasSpawned, $changedProperties); $this->sendData($this->hasSpawned, $changedProperties);
$this->propertyManager->clearDirtyProperties(); $this->propertyManager->clearDirtyProperties();
} }

View File

@ -61,6 +61,7 @@ use function array_merge;
use function array_rand; use function array_rand;
use function array_values; use function array_values;
use function ceil; use function ceil;
use function count;
use function in_array; use function in_array;
use function max; use function max;
use function min; use function min;
@ -554,7 +555,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
} }
if(!empty($equipment)){ if(count($equipment) > 0){
$repairItem = $equipment[$k = array_rand($equipment)]; $repairItem = $equipment[$k = array_rand($equipment)];
if($repairItem->getDamage() > 0){ if($repairItem->getDamage() > 0){
$repairAmount = min($repairItem->getDamage(), $xpValue * 2); $repairAmount = min($repairItem->getDamage(), $xpValue * 2);

View File

@ -263,7 +263,7 @@ abstract class Living extends Entity implements Damageable{
* @return bool * @return bool
*/ */
public function hasEffects() : bool{ public function hasEffects() : bool{
return !empty($this->effects); return count($this->effects) > 0;
} }
/** /**
@ -332,7 +332,7 @@ abstract class Living extends Entity implements Damageable{
} }
} }
if(!empty($colors)){ if(count($colors) > 0){
$this->propertyManager->setInt(Entity::DATA_POTION_COLOR, Color::mix(...$colors)->toARGB()); $this->propertyManager->setInt(Entity::DATA_POTION_COLOR, Color::mix(...$colors)->toARGB());
$this->propertyManager->setByte(Entity::DATA_POTION_AMBIENT, $ambient ? 1 : 0); $this->propertyManager->setByte(Entity::DATA_POTION_AMBIENT, $ambient ? 1 : 0);
}else{ }else{
@ -713,7 +713,7 @@ abstract class Living extends Entity implements Damageable{
} }
} }
return !empty($this->effects); return count($this->effects) > 0;
} }
/** /**
@ -893,7 +893,7 @@ abstract class Living extends Entity implements Damageable{
*/ */
public function getTargetBlock(int $maxDistance, array $transparent = []) : ?Block{ public function getTargetBlock(int $maxDistance, array $transparent = []) : ?Block{
$line = $this->getLineOfSight($maxDistance, 1, $transparent); $line = $this->getLineOfSight($maxDistance, 1, $transparent);
if(!empty($line)){ if(count($line) > 0){
return array_shift($line); return array_shift($line);
} }

View File

@ -34,6 +34,7 @@ use pocketmine\item\Potion;
use pocketmine\network\mcpe\protocol\LevelEventPacket; use pocketmine\network\mcpe\protocol\LevelEventPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\utils\Color; use pocketmine\utils\Color;
use function count;
use function round; use function round;
use function sqrt; use function sqrt;
@ -63,7 +64,7 @@ class SplashPotion extends Throwable{
$effects = $this->getPotionEffects(); $effects = $this->getPotionEffects();
$hasEffects = true; $hasEffects = true;
if(empty($effects)){ if(count($effects) === 0){
$colors = [ $colors = [
new Color(0x38, 0x5d, 0xc6) //Default colour for splash water bottle and similar with no effects. new Color(0x38, 0x5d, 0xc6) //Default colour for splash water bottle and similar with no effects.
]; ];

View File

@ -135,6 +135,6 @@ class ShapelessRecipe implements CraftingRecipe{
return false; //failed to match the needed item to a given item return false; //failed to match the needed item to a given item
} }
return empty($input); //crafting grid should be empty apart from the given ingredient stacks return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks
} }
} }

View File

@ -68,14 +68,14 @@ class CraftingTransaction extends InventoryTransaction{
* @throws TransactionValidationException * @throws TransactionValidationException
*/ */
protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{ protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{
if(empty($recipeItems)){ if(count($recipeItems) === 0){
throw new TransactionValidationException("No recipe items given"); throw new TransactionValidationException("No recipe items given");
} }
if(empty($txItems)){ if(count($txItems) === 0){
throw new TransactionValidationException("No transaction items given"); throw new TransactionValidationException("No transaction items given");
} }
while(!empty($recipeItems)){ while(count($recipeItems) > 0){
/** @var Item $recipeItem */ /** @var Item $recipeItem */
$recipeItem = array_pop($recipeItems); $recipeItem = array_pop($recipeItems);
$needCount = $recipeItem->getCount(); $needCount = $recipeItem->getCount();
@ -114,7 +114,7 @@ class CraftingTransaction extends InventoryTransaction{
if($iterations < 1){ if($iterations < 1){
throw new TransactionValidationException("Tried to craft zero times"); throw new TransactionValidationException("Tried to craft zero times");
} }
if(!empty($txItems)){ if(count($txItems) > 0){
//all items should be destroyed in this process //all items should be destroyed in this process
throw new TransactionValidationException("Expected 0 ingredients left over, have " . count($txItems)); throw new TransactionValidationException("Expected 0 ingredients left over, have " . count($txItems));
} }

View File

@ -237,13 +237,13 @@ class InventoryTransaction{
* @return null|Item * @return null|Item
*/ */
protected function findResultItem(Item $needOrigin, array $possibleActions) : ?Item{ protected function findResultItem(Item $needOrigin, array $possibleActions) : ?Item{
assert(!empty($possibleActions)); assert(count($possibleActions) > 0);
foreach($possibleActions as $i => $action){ foreach($possibleActions as $i => $action){
if($action->getSourceItem()->equalsExact($needOrigin)){ if($action->getSourceItem()->equalsExact($needOrigin)){
$newList = $possibleActions; $newList = $possibleActions;
unset($newList[$i]); unset($newList[$i]);
if(empty($newList)){ if(count($newList) === 0){
return $action->getTargetItem(); return $action->getTargetItem();
} }
$result = $this->findResultItem($action->getTargetItem(), $newList); $result = $this->findResultItem($action->getTargetItem(), $newList);

View File

@ -31,6 +31,7 @@ use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelEventPacket; use pocketmine\network\mcpe\protocol\LevelEventPacket;
use pocketmine\Player; use pocketmine\Player;
use function array_rand; use function array_rand;
use function count;
class PaintingItem extends Item{ class PaintingItem extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
@ -66,7 +67,7 @@ class PaintingItem extends Item{
} }
} }
if(empty($motives)){ //No space available if(count($motives) === 0){ //No space available
return false; return false;
} }

View File

@ -503,7 +503,7 @@ class Level implements ChunkManager, Metadatable{
if(!is_array($pk)){ if(!is_array($pk)){
$pk = [$pk]; $pk = [$pk];
} }
if(!empty($pk)){ if(count($pk) > 0){
if($players === null){ if($players === null){
foreach($pk as $e){ foreach($pk as $e){
$this->broadcastPacketToViewers($sound, $e); $this->broadcastPacketToViewers($sound, $e);
@ -519,7 +519,7 @@ class Level implements ChunkManager, Metadatable{
if(!is_array($pk)){ if(!is_array($pk)){
$pk = [$pk]; $pk = [$pk];
} }
if(!empty($pk)){ if(count($pk) > 0){
if($players === null){ if($players === null){
foreach($pk as $e){ foreach($pk as $e){
$this->broadcastPacketToViewers($particle, $e); $this->broadcastPacketToViewers($particle, $e);
@ -881,7 +881,7 @@ class Level implements ChunkManager, Metadatable{
if(count($this->changedBlocks) > 0){ if(count($this->changedBlocks) > 0){
if(count($this->players) > 0){ if(count($this->players) > 0){
foreach($this->changedBlocks as $index => $blocks){ foreach($this->changedBlocks as $index => $blocks){
if(empty($blocks)){ //blocks can be set normally and then later re-set with direct send if(count($blocks) === 0){ //blocks can be set normally and then later re-set with direct send
continue; continue;
} }
unset($this->chunkCache[$index]); unset($this->chunkCache[$index]);
@ -909,8 +909,8 @@ class Level implements ChunkManager, Metadatable{
$this->checkSleep(); $this->checkSleep();
} }
if(!empty($this->globalPackets)){ if(count($this->globalPackets) > 0){
if(!empty($this->players)){ if(count($this->players) > 0){
$this->server->batchPackets($this->players, $this->globalPackets); $this->server->batchPackets($this->players, $this->globalPackets);
} }
$this->globalPackets = []; $this->globalPackets = [];
@ -1845,7 +1845,7 @@ class Level implements ChunkManager, Metadatable{
$item->onDestroyBlock($target); $item->onDestroyBlock($target);
if(!empty($drops)){ if(count($drops) > 0){
$dropPos = $target->add(0.5, 0.5, 0.5); $dropPos = $target->add(0.5, 0.5, 0.5);
foreach($drops as $drop){ foreach($drops as $drop){
if(!$drop->isNull()){ if(!$drop->isNull()){
@ -1949,7 +1949,7 @@ class Level implements ChunkManager, Metadatable{
if($hand->isSolid()){ if($hand->isSolid()){
foreach($hand->getCollisionBoxes() as $collisionBox){ foreach($hand->getCollisionBoxes() as $collisionBox){
if(!empty($this->getCollidingEntities($collisionBox))){ if(count($this->getCollidingEntities($collisionBox)) > 0){
return false; //Entity in block return false; //Entity in block
} }

View File

@ -45,6 +45,7 @@ use pocketmine\utils\Binary;
use pocketmine\utils\BinaryStream; use pocketmine\utils\BinaryStream;
use function array_values; use function array_values;
use function chr; use function chr;
use function count;
use function defined; use function defined;
use function explode; use function explode;
use function extension_loaded; use function extension_loaded;
@ -519,7 +520,7 @@ class LevelDB extends BaseLevelProvider{
* @param string $index * @param string $index
*/ */
private function writeTags(array $targets, string $index){ private function writeTags(array $targets, string $index){
if(!empty($targets)){ if(count($targets) > 0){
$nbt = new LittleEndianNBTStream(); $nbt = new LittleEndianNBTStream();
$this->db->put($index, $nbt->write($targets)); $this->db->put($index, $nbt->write($targets));
}else{ }else{

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\permission; namespace pocketmine\permission;
use function array_shift; use function array_shift;
use function count;
use function explode; use function explode;
use function implode; use function implode;
use function strlen; use function strlen;
@ -161,17 +162,17 @@ class BanEntry{
$str = explode("|", trim($str)); $str = explode("|", trim($str));
$entry = new BanEntry(trim(array_shift($str))); $entry = new BanEntry(trim(array_shift($str)));
do{ do{
if(empty($str)){ if(count($str) === 0){
break; break;
} }
$entry->setCreated(self::parseDate(array_shift($str))); $entry->setCreated(self::parseDate(array_shift($str)));
if(empty($str)){ if(count($str) === 0){
break; break;
} }
$entry->setSource(trim(array_shift($str))); $entry->setSource(trim(array_shift($str)));
if(empty($str)){ if(count($str) === 0){
break; break;
} }
@ -179,7 +180,7 @@ class BanEntry{
if($expire !== "" and strtolower($expire) !== "forever"){ if($expire !== "" and strtolower($expire) !== "forever"){
$entry->setExpires(self::parseDate($expire)); $entry->setExpires(self::parseDate($expire));
} }
if(empty($str)){ if(count($str) === 0){
break; break;
} }

View File

@ -168,7 +168,7 @@ class PermissionManager{
public function unsubscribeFromAllPermissions(Permissible $permissible) : void{ public function unsubscribeFromAllPermissions(Permissible $permissible) : void{
foreach($this->permSubs as $permission => &$subs){ foreach($this->permSubs as $permission => &$subs){
unset($subs[spl_object_hash($permissible)]); unset($subs[spl_object_hash($permissible)]);
if(empty($subs)){ if(count($subs) === 0){
unset($this->permSubs[$permission]); unset($this->permSubs[$permission]);
} }
} }

View File

@ -38,6 +38,7 @@ use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket; use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
use function ceil; use function ceil;
use function count;
use function max; use function max;
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
@ -232,7 +233,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
$packets[] = $pk; $packets[] = $pk;
} }
if(!empty($packets)){ if(count($packets) > 0){
foreach($this->getInventory()->getViewers() as $player){ foreach($this->getInventory()->getViewers() as $player){
$windowId = $player->getWindowId($this->getInventory()); $windowId = $player->getWindowId($this->getInventory());
if($windowId > 0){ if($windowId > 0){

View File

@ -420,7 +420,7 @@ class Config{
while(count($vars) > 0){ while(count($vars) > 0){
$nodeName = array_shift($vars); $nodeName = array_shift($vars);
if(isset($currentNode[$nodeName])){ if(isset($currentNode[$nodeName])){
if(empty($vars)){ //final node if(count($vars) === 0){ //final node
unset($currentNode[$nodeName]); unset($currentNode[$nodeName]);
}elseif(is_array($currentNode[$nodeName])){ }elseif(is_array($currentNode[$nodeName])){
$currentNode =& $currentNode[$nodeName]; $currentNode =& $currentNode[$nodeName];

View File

@ -33,6 +33,7 @@ use pocketmine\utils\Config;
use pocketmine\utils\Internet; use pocketmine\utils\Internet;
use pocketmine\utils\InternetException; use pocketmine\utils\InternetException;
use function base64_encode; use function base64_encode;
use function count;
use function fgets; use function fgets;
use function random_bytes; use function random_bytes;
use function sleep; use function sleep;
@ -59,7 +60,7 @@ class SetupWizard{
$this->message(\pocketmine\NAME . " set-up wizard"); $this->message(\pocketmine\NAME . " set-up wizard");
$langs = BaseLang::getLanguageList(); $langs = BaseLang::getLanguageList();
if(empty($langs)){ if(count($langs) === 0){
$this->error("No language files found, please use provided builds or clone the repository recursively."); $this->error("No language files found, please use provided builds or clone the repository recursively.");
return false; return false;
} }

View File

@ -58,6 +58,11 @@ parameters:
count: 1 count: 1
path: ../../../src/pocketmine/plugin/PharPluginLoader.php path: ../../../src/pocketmine/plugin/PharPluginLoader.php
-
message: "#^Strict comparison using \\=\\=\\= between int\\<1, max\\> and 0 will always evaluate to false\\.$#"
count: 1
path: ../../../src/pocketmine/utils/Config.php
- -
#ReflectionFunction::getClosureThis() should be nullable #ReflectionFunction::getClosureThis() should be nullable
message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#" message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#"