avoid type juggling in conditions, always use explicit boolean conditions

This commit is contained in:
Dylan K. Taylor 2020-02-05 15:44:06 +00:00
parent b96bb7d824
commit e5a2cfb65f
17 changed files with 43 additions and 43 deletions

View File

@ -1885,7 +1885,7 @@ class Server{
$this->rcon->stop();
}
if($this->getProperty("network.upnp-forwarding", false)){
if((bool) $this->getProperty("network.upnp-forwarding", false)){
$this->logger->info("[UPnP] Removing port forward...");
UPnP::RemovePortForward($this->getPort());
}
@ -1958,12 +1958,12 @@ class Server{
$this->network->blockAddress($entry->getName(), -1);
}
if($this->getProperty("settings.send-usage", true)){
if((bool) $this->getProperty("settings.send-usage", true)){
$this->sendUsageTicker = 6000;
$this->sendUsage(SendUsageTask::TYPE_OPEN);
}
if($this->getProperty("network.upnp-forwarding", false)){
if((bool) $this->getProperty("network.upnp-forwarding", false)){
$this->logger->info("[UPnP] Trying to port forward...");
try{
UPnP::PortForward($this->getPort());
@ -2089,7 +2089,7 @@ class Server{
}
if($report){
$url = ($this->getProperty("auto-report.use-https", true) ? "https" : "http") . "://" . $this->getProperty("auto-report.host", "crash.pmmp.io") . "/submit/api";
$url = ((bool) $this->getProperty("auto-report.use-https", true) ? "https" : "http") . "://" . $this->getProperty("auto-report.host", "crash.pmmp.io") . "/submit/api";
$reply = Internet::postURL($url, [
"report" => "yes",
"name" => $this->getName() . " " . $this->getPocketMineVersion(),

View File

@ -78,7 +78,7 @@ class Anvil extends Fallable{
public function recalculateBoundingBox() : ?AxisAlignedBB{
$inset = 0.125;
if($this->meta & 0x01){ //east/west
if(($this->meta & 0x01) !== 0){ //east/west
return new AxisAlignedBB(
$this->x,
$this->y,

View File

@ -563,7 +563,7 @@ class Block extends Position implements BlockIds, Metadatable{
* @return AxisAlignedBB[]
*/
protected function recalculateCollisionBoxes() : array{
if($bb = $this->recalculateBoundingBox()){
if(($bb = $this->recalculateBoundingBox()) !== null){
return [$bb];
}

View File

@ -70,7 +70,7 @@ class DoublePlant extends Flowable{
* Returns whether this double-plant has a corresponding other half.
*/
public function isValidHalfPlant() : bool{
if($this->meta & self::BITFLAG_TOP){
if(($this->meta & self::BITFLAG_TOP) !== 0){
$other = $this->getSide(Vector3::SIDE_DOWN);
}else{
$other = $this->getSide(Vector3::SIDE_UP);
@ -102,7 +102,7 @@ class DoublePlant extends Flowable{
}
public function getDrops(Item $item) : array{
if($this->meta & self::BITFLAG_TOP){
if(($this->meta & self::BITFLAG_TOP) !== 0){
if($this->isCompatibleWithTool($item)){
return parent::getDrops($item);
}

View File

@ -172,7 +172,7 @@ class Leaves extends Transparent{
}
public function getDrops(Item $item) : array{
if($item->getBlockToolType() & BlockToolType::TYPE_SHEARS){
if(($item->getBlockToolType() & BlockToolType::TYPE_SHEARS) !== 0){
return $this->getDropsForCompatibleTool($item);
}

View File

@ -197,7 +197,7 @@ class Vine extends Flowable{
}
public function getDrops(Item $item) : array{
if($item->getBlockToolType() & BlockToolType::TYPE_SHEARS){
if(($item->getBlockToolType() & BlockToolType::TYPE_SHEARS) !== 0){
return $this->getDropsForCompatibleTool($item);
}

View File

@ -115,7 +115,7 @@ class DataPropertyManager{
}
public function setBlockPos(int $key, ?Vector3 $value, bool $force = false) : void{
$this->setPropertyValue($key, Entity::DATA_TYPE_POS, $value ? $value->floor() : null, $force);
$this->setPropertyValue($key, Entity::DATA_TYPE_POS, $value !== null ? $value->floor() : null, $force);
}
public function getLong(int $key) : ?int{
@ -135,7 +135,7 @@ class DataPropertyManager{
}
public function setVector3(int $key, ?Vector3 $value, bool $force = false) : void{
$this->setPropertyValue($key, Entity::DATA_TYPE_VECTOR3F, $value ? $value->asVector3() : null, $force);
$this->setPropertyValue($key, Entity::DATA_TYPE_VECTOR3F, $value !== null ? $value->asVector3() : null, $force);
}
public function removeProperty(int $key) : void{

View File

@ -424,9 +424,9 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
new DoubleTag("", $pos->z)
]),
new ListTag("Motion", [
new DoubleTag("", $motion ? $motion->x : 0.0),
new DoubleTag("", $motion ? $motion->y : 0.0),
new DoubleTag("", $motion ? $motion->z : 0.0)
new DoubleTag("", $motion !== null ? $motion->x : 0.0),
new DoubleTag("", $motion !== null ? $motion->y : 0.0),
new DoubleTag("", $motion !== null ? $motion->z : 0.0)
]),
new ListTag("Rotation", [
new FloatTag("", $yaw),

View File

@ -144,7 +144,7 @@ class CraftingManager{
*/
public static function sort(Item $i1, Item $i2){
//Use spaceship operator to compare each property, then try the next one if they are equivalent.
($retval = $i1->getId() <=> $i2->getId()) === 0 && ($retval = $i1->getDamage() <=> $i2->getDamage()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount());
($retval = $i1->getId() <=> $i2->getId()) === 0 && ($retval = $i1->getDamage() <=> $i2->getDamage()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
return $retval;
}

View File

@ -51,7 +51,7 @@ class SimpleChunkManager implements ChunkManager{
* @return int 0-255
*/
public function getBlockIdAt(int $x, int $y, int $z) : int{
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
return $chunk->getBlockId($x & 0xf, $y, $z & 0xf);
}
return 0;
@ -65,7 +65,7 @@ class SimpleChunkManager implements ChunkManager{
* @return void
*/
public function setBlockIdAt(int $x, int $y, int $z, int $id){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
$chunk->setBlockId($x & 0xf, $y, $z & 0xf, $id);
}
}
@ -76,7 +76,7 @@ class SimpleChunkManager implements ChunkManager{
* @return int 0-15
*/
public function getBlockDataAt(int $x, int $y, int $z) : int{
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
return $chunk->getBlockData($x & 0xf, $y, $z & 0xf);
}
return 0;
@ -90,13 +90,13 @@ class SimpleChunkManager implements ChunkManager{
* @return void
*/
public function setBlockDataAt(int $x, int $y, int $z, int $data){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
$chunk->setBlockData($x & 0xf, $y, $z & 0xf, $data);
}
}
public function getBlockLightAt(int $x, int $y, int $z) : int{
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
return $chunk->getBlockLight($x & 0xf, $y, $z & 0xf);
}
@ -104,13 +104,13 @@ class SimpleChunkManager implements ChunkManager{
}
public function setBlockLightAt(int $x, int $y, int $z, int $level){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
$chunk->setBlockLight($x & 0xf, $y, $z & 0xf, $level);
}
}
public function getBlockSkyLightAt(int $x, int $y, int $z) : int{
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
return $chunk->getBlockSkyLight($x & 0xf, $y, $z & 0xf);
}
@ -118,7 +118,7 @@ class SimpleChunkManager implements ChunkManager{
}
public function setBlockSkyLightAt(int $x, int $y, int $z, int $level){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){
$chunk->setBlockSkyLight($x & 0xf, $y, $z & 0xf, $level);
}
}

View File

@ -516,7 +516,7 @@ class NetworkBinaryStream extends BinaryStream{
* @see NetworkBinaryStream::putVector3()
*/
public function putVector3Nullable(?Vector3 $vector) : void{
if($vector){
if($vector !== null){
$this->putVector3($vector);
}else{
$this->putLFloat(0.0);

View File

@ -94,7 +94,7 @@ class AdventureSettingsPacket extends DataPacket{
}
public function getFlag(int $flag) : bool{
if($flag & self::BITFLAG_SECOND_SET){
if(($flag & self::BITFLAG_SECOND_SET) !== 0){
return ($this->flags2 & $flag) !== 0;
}
@ -105,7 +105,7 @@ class AdventureSettingsPacket extends DataPacket{
* @return void
*/
public function setFlag(int $flag, bool $value){
if($flag & self::BITFLAG_SECOND_SET){
if(($flag & self::BITFLAG_SECOND_SET) !== 0){
$flagSet =& $this->flags2;
}else{
$flagSet =& $this->flags;

View File

@ -47,7 +47,7 @@ class AnimatePacket extends DataPacket{
protected function decodePayload(){
$this->action = $this->getVarInt();
$this->entityRuntimeId = $this->getEntityRuntimeId();
if($this->action & 0x80){
if(($this->action & 0x80) !== 0){
$this->float = $this->getLFloat();
}
}
@ -55,7 +55,7 @@ class AnimatePacket extends DataPacket{
protected function encodePayload(){
$this->putVarInt($this->action);
$this->putEntityRuntimeId($this->entityRuntimeId);
if($this->action & 0x80){
if(($this->action & 0x80) !== 0){
$this->putLFloat($this->float);
}
}

View File

@ -295,13 +295,13 @@ class AvailableCommandsPacket extends DataPacket{
$parameter->isOptional = $this->getBool();
$parameter->flags = $this->getByte();
if($parameter->paramType & self::ARG_FLAG_ENUM){
if(($parameter->paramType & self::ARG_FLAG_ENUM) !== 0){
$index = ($parameter->paramType & 0xffff);
$parameter->enum = $enums[$index] ?? null;
if($parameter->enum === null){
throw new \UnexpectedValueException("deserializing $retval->commandName parameter $parameter->paramName: expected enum at $index, but got none");
}
}elseif($parameter->paramType & self::ARG_FLAG_POSTFIX){
}elseif(($parameter->paramType & self::ARG_FLAG_POSTFIX) !== 0){
$index = ($parameter->paramType & 0xffff);
$parameter->postfix = $postfixes[$index] ?? null;
if($parameter->postfix === null){
@ -365,8 +365,8 @@ class AvailableCommandsPacket extends DataPacket{
* @phpstan-param array<int, string> $postfixes
*/
private function argTypeToString(int $argtype, array $postfixes) : string{
if($argtype & self::ARG_FLAG_VALID){
if($argtype & self::ARG_FLAG_ENUM){
if(($argtype & self::ARG_FLAG_VALID) !== 0){
if(($argtype & self::ARG_FLAG_ENUM) !== 0){
return "stringenum (" . ($argtype & 0xffff) . ")";
}
@ -392,7 +392,7 @@ class AvailableCommandsPacket extends DataPacket{
case self::ARG_TYPE_COMMAND:
return "command";
}
}elseif($argtype & self::ARG_FLAG_POSTFIX){
}elseif(($argtype & self::ARG_FLAG_POSTFIX) !== 0){
$postfix = $postfixes[$argtype & 0xffff];
return "int (postfix $postfix)";

View File

@ -55,14 +55,14 @@ class MoveActorDeltaPacket extends DataPacket{
public $zRot = 0.0;
private function maybeReadCoord(int $flag) : int{
if($this->flags & $flag){
if(($this->flags & $flag) !== 0){
return $this->getVarInt();
}
return 0;
}
private function maybeReadRotation(int $flag) : float{
if($this->flags & $flag){
if(($this->flags & $flag) !== 0){
return $this->getByteRotation();
}
return 0.0;
@ -80,13 +80,13 @@ class MoveActorDeltaPacket extends DataPacket{
}
private function maybeWriteCoord(int $flag, int $val) : void{
if($this->flags & $flag){
if(($this->flags & $flag) !== 0){
$this->putVarInt($val);
}
}
private function maybeWriteRotation(int $flag, float $val) : void{
if($this->flags & $flag){
if(($this->flags & $flag) !== 0){
$this->putByteRotation($val);
}
}

View File

@ -53,7 +53,7 @@ class AutoUpdater{
$this->server = $server;
$this->endpoint = "http://$endpoint/api/";
if($server->getProperty("auto-updater.enabled", true)){
if((bool) $server->getProperty("auto-updater.enabled", true)){
$this->doCheck();
}
}
@ -71,10 +71,10 @@ class AutoUpdater{
$this->checkUpdate();
if($this->hasUpdate()){
(new UpdateNotifyEvent($this))->call();
if($this->server->getProperty("auto-updater.on-update.warn-console", true)){
if((bool) $this->server->getProperty("auto-updater.on-update.warn-console", true)){
$this->showConsoleUpdate();
}
}elseif($this->server->getProperty("auto-updater.preferred-channel", true)){
}else{
if(!\pocketmine\IS_DEVELOPMENT_BUILD and $this->getChannel() !== "stable"){
$this->showChannelSuggestionStable();
}elseif(\pocketmine\IS_DEVELOPMENT_BUILD and $this->getChannel() === "stable"){

View File

@ -478,7 +478,7 @@ class Utils{
$hash = 0;
for($i = 0, $len = strlen($string); $i < $len; $i++){
$ord = ord($string[$i]);
if($ord & 0x80){
if(($ord & 0x80) !== 0){
$ord -= 0x100;
}
$hash = 31 * $hash + $ord;
@ -671,7 +671,7 @@ class Utils{
* @throws \ErrorException
*/
public static function errorExceptionHandler(int $severity, string $message, string $file, int $line) : bool{
if(error_reporting() & $severity){
if((error_reporting() & $severity) !== 0){
throw new \ErrorException($message, 0, $severity, $file, $line);
}