From 7e331c590d2d1924f504acfd6845d1b85da71838 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 27 Jun 2020 13:08:36 +0100 Subject: [PATCH] Use static-analysis-friendly method for getting effects from EffectManager fixes 5 phpstan level 8 errors --- src/entity/Living.php | 8 ++++---- src/event/entity/EntityDamageByEntityEvent.php | 8 ++++---- tests/phpstan/configs/l8-baseline.neon | 10 ---------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index b812884df..3c13b7fea 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -281,7 +281,7 @@ abstract class Living extends Entity{ * Returns the initial upwards velocity of a jumping entity in blocks/tick, including additional velocity due to effects. */ public function getJumpVelocity() : float{ - return $this->jumpVelocity + ($this->effectManager->has(VanillaEffects::JUMP_BOOST()) ? ($this->effectManager->get(VanillaEffects::JUMP_BOOST())->getEffectLevel() / 10) : 0); + return $this->jumpVelocity + ((($jumpBoost = $this->effectManager->get(VanillaEffects::JUMP_BOOST())) !== null ? $jumpBoost->getEffectLevel() : 0) / 10); } /** @@ -294,7 +294,7 @@ abstract class Living extends Entity{ } public function fall(float $fallDistance) : void{ - $damage = ceil($fallDistance - 3 - ($this->effectManager->has(VanillaEffects::JUMP_BOOST()) ? $this->effectManager->get(VanillaEffects::JUMP_BOOST())->getEffectLevel() : 0)); + $damage = ceil($fallDistance - 3 - (($jumpBoost = $this->effectManager->get(VanillaEffects::JUMP_BOOST())) !== null ? $jumpBoost->getEffectLevel() : 0)); if($damage > 0){ $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_FALL, $damage); $this->attack($ev); @@ -367,8 +367,8 @@ abstract class Living extends Entity{ } $cause = $source->getCause(); - if($this->effectManager->has(VanillaEffects::RESISTANCE()) and $cause !== EntityDamageEvent::CAUSE_VOID and $cause !== EntityDamageEvent::CAUSE_SUICIDE){ - $source->setModifier(-$source->getFinalDamage() * min(1, 0.2 * $this->effectManager->get(VanillaEffects::RESISTANCE())->getEffectLevel()), EntityDamageEvent::MODIFIER_RESISTANCE); + if(($resistance = $this->effectManager->get(VanillaEffects::RESISTANCE())) !== null and $cause !== EntityDamageEvent::CAUSE_VOID and $cause !== EntityDamageEvent::CAUSE_SUICIDE){ + $source->setModifier(-$source->getFinalDamage() * min(1, 0.2 * $resistance->getEffectLevel()), EntityDamageEvent::MODIFIER_RESISTANCE); } $totalEpf = 0; diff --git a/src/event/entity/EntityDamageByEntityEvent.php b/src/event/entity/EntityDamageByEntityEvent.php index 6774e808e..90d487c70 100644 --- a/src/event/entity/EntityDamageByEntityEvent.php +++ b/src/event/entity/EntityDamageByEntityEvent.php @@ -49,12 +49,12 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{ protected function addAttackerModifiers(Entity $damager) : void{ if($damager instanceof Living){ //TODO: move this to entity classes $effects = $damager->getEffects(); - if($effects->has(VanillaEffects::STRENGTH())){ - $this->setModifier($this->getBaseDamage() * 0.3 * $effects->get(VanillaEffects::STRENGTH())->getEffectLevel(), self::MODIFIER_STRENGTH); + if(($strength = $effects->get(VanillaEffects::STRENGTH())) !== null){ + $this->setModifier($this->getBaseDamage() * 0.3 * $strength->getEffectLevel(), self::MODIFIER_STRENGTH); } - if($effects->has(VanillaEffects::WEAKNESS())){ - $this->setModifier(-($this->getBaseDamage() * 0.2 * $effects->get(VanillaEffects::WEAKNESS())->getEffectLevel()), self::MODIFIER_WEAKNESS); + if(($weakness = $effects->get(VanillaEffects::WEAKNESS())) !== null){ + $this->setModifier(-($this->getBaseDamage() * 0.2 * $weakness->getEffectLevel()), self::MODIFIER_WEAKNESS); } } } diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index a89b8e955..2ee317c4f 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -130,16 +130,6 @@ parameters: count: 1 path: ../../../src/entity/Entity.php - - - message: "#^Cannot call method getEffectLevel\\(\\) on pocketmine\\\\entity\\\\effect\\\\EffectInstance\\|null\\.$#" - count: 3 - path: ../../../src/entity/Living.php - - - - message: "#^Cannot call method getEffectLevel\\(\\) on pocketmine\\\\entity\\\\effect\\\\EffectInstance\\|null\\.$#" - count: 2 - path: ../../../src/event/entity/EntityDamageByEntityEvent.php - - message: "#^Parameter \\#2 \\$oldContents of method pocketmine\\\\inventory\\\\InventoryListener\\:\\:onContentChange\\(\\) expects array\\, array\\ given\\.$#" count: 1