From 89c6da13acc0122a1ff465344903d06c1d31292c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 31 Jan 2020 22:05:33 +0000 Subject: [PATCH] phpstan: use more class-string --- src/block/BlockIdentifier.php | 13 ++++++++++++- src/block/tile/TileFactory.php | 18 +++++++++++++++++- src/entity/EntityFactory.php | 19 ++++++++++++++++++- src/item/ProjectileItem.php | 1 + src/item/SpawnEgg.php | 6 +++++- src/world/format/io/WorldProviderManager.php | 15 +++++++++++---- tests/phpstan/configs/phpstan-bugs.neon | 6 ++++++ 7 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/block/BlockIdentifier.php b/src/block/BlockIdentifier.php index 87faf3a31..1df7d942c 100644 --- a/src/block/BlockIdentifier.php +++ b/src/block/BlockIdentifier.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\tile\Tile; + class BlockIdentifier{ /** @var int */ @@ -31,9 +33,15 @@ class BlockIdentifier{ private $variant; /** @var int|null */ private $itemId; - /** @var string|null */ + /** + * @var string|null + * @phpstan-var class-string|null + */ private $tileClass; + /** + * @phpstan-param class-string|null $tileClass + */ public function __construct(int $blockId, int $variant = 0, ?int $itemId = null, ?string $tileClass = null){ $this->blockId = $blockId; $this->variant = $variant; @@ -60,6 +68,9 @@ class BlockIdentifier{ return $this->itemId ?? ($this->blockId > 255 ? 255 - $this->blockId : $this->blockId); } + /** + * @phpstan-return class-string|null + */ public function getTileClass() : ?string{ return $this->tileClass; } diff --git a/src/block/tile/TileFactory.php b/src/block/tile/TileFactory.php index 20752f39c..130d99e56 100644 --- a/src/block/tile/TileFactory.php +++ b/src/block/tile/TileFactory.php @@ -44,7 +44,10 @@ final class TileFactory{ * @phpstan-var array, list> */ private static $saveNames = []; - /** @var string[] base class => overridden class */ + /** + * @var string[] base class => overridden class + * @phpstan-var array, class-string> + */ private static $classMapping = []; private function __construct(){ @@ -119,6 +122,10 @@ final class TileFactory{ * @param string $baseClass Already-registered tile class to override * @param string $newClass Class which extends the base class * + * TODO: use an explicit template for param1 + * @phpstan-param class-string $baseClass + * @phpstan-param class-string $newClass + * * @throws \InvalidArgumentException if the base class is not a registered tile */ public static function override(string $baseClass, string $newClass) : void{ @@ -131,7 +138,12 @@ final class TileFactory{ } /** + * @phpstan-template TTile of Tile + * @phpstan-param class-string $baseClass + * * @return Tile (will be an instanceof $baseClass) + * @phpstan-return TTile + * * @throws \InvalidArgumentException if the specified class is not a registered tile */ public static function create(string $baseClass, World $world, Vector3 $pos) : Tile{ @@ -140,6 +152,7 @@ final class TileFactory{ assert(is_a($class, $baseClass, true)); /** * @var Tile $tile + * @phpstan-var TTile $tile * @see Tile::__construct() */ $tile = new $class($world, $pos); @@ -170,6 +183,9 @@ final class TileFactory{ return $tile; } + /** + * @phpstan-param class-string $class + */ public static function getSaveId(string $class) : string{ if(isset(self::$saveNames[$class])){ return reset(self::$saveNames[$class]); diff --git a/src/entity/EntityFactory.php b/src/entity/EntityFactory.php index 69fdbb328..d333ce16f 100644 --- a/src/entity/EntityFactory.php +++ b/src/entity/EntityFactory.php @@ -63,7 +63,10 @@ final class EntityFactory{ /** @var int */ private static $entityCount = 1; - /** @var string[] base class => currently used class for construction */ + /** + * @var string[] base class => currently used class for construction + * @phpstan-var array, class-string> + */ private static $classMapping = []; /** * @var string[] @@ -148,6 +151,10 @@ final class EntityFactory{ * @param string $baseClass Already-registered entity class to override * @param string $newClass Class which extends the base class * + * TODO: use an explicit template for param1 + * @phpstan-param class-string $baseClass + * @phpstan-param class-string $newClass + * * @throws \InvalidArgumentException */ public static function override(string $baseClass, string $newClass) : void{ @@ -163,6 +170,7 @@ final class EntityFactory{ * Returns an array of all registered entity classpaths. * * @return string[] + * @return class-string[] */ public static function getKnownTypes() : array{ return array_keys(self::$classMapping); @@ -181,9 +189,14 @@ final class EntityFactory{ * * TODO: make this NBT-independent * + * @phpstan-template TEntity of Entity + * * @param mixed ...$args + * @phpstan-param class-string $baseClass * * @return Entity instanceof $baseClass + * @phpstan-return TEntity + * * @throws \InvalidArgumentException if the class doesn't exist or is not registered */ public static function create(string $baseClass, World $world, CompoundTag $nbt, ...$args) : Entity{ @@ -192,6 +205,7 @@ final class EntityFactory{ assert(is_a($class, $baseClass, true)); /** * @var Entity $entity + * @phpstan-var TEntity $entity * @see Entity::__construct() */ $entity = new $class($world, $nbt, ...$args); @@ -230,6 +244,9 @@ final class EntityFactory{ return $entity; } + /** + * @phpstan-param class-string $class + */ public static function getSaveId(string $class) : string{ if(isset(self::$saveNames[$class])){ return reset(self::$saveNames[$class]); diff --git a/src/item/ProjectileItem.php b/src/item/ProjectileItem.php index cbae038b3..62d3e6e4b 100644 --- a/src/item/ProjectileItem.php +++ b/src/item/ProjectileItem.php @@ -38,6 +38,7 @@ abstract class ProjectileItem extends Item{ * Returns the entity type that this projectile creates. This should return a ::class extending Throwable. * * @return string class extends Throwable + * @phpstan-return class-string */ abstract public function getProjectileEntityClass() : string; diff --git a/src/item/SpawnEgg.php b/src/item/SpawnEgg.php index b9aa36165..098283914 100644 --- a/src/item/SpawnEgg.php +++ b/src/item/SpawnEgg.php @@ -33,11 +33,15 @@ use function lcg_value; class SpawnEgg extends Item{ - /** @var string */ + /** + * @var string + * @phpstan-var class-string + */ private $entityClass; /** * @param string $entityClass instanceof Entity + * @phpstan-param class-string $entityClass * * @throws \InvalidArgumentException */ diff --git a/src/world/format/io/WorldProviderManager.php b/src/world/format/io/WorldProviderManager.php index a4f936031..37e489709 100644 --- a/src/world/format/io/WorldProviderManager.php +++ b/src/world/format/io/WorldProviderManager.php @@ -38,7 +38,10 @@ abstract class WorldProviderManager{ */ protected static $providers = []; - /** @var string|WorldProvider */ + /** + * @var string + * @phpstan-var class-string + */ private static $default = LevelDB::class; public static function init() : void{ @@ -50,6 +53,8 @@ abstract class WorldProviderManager{ /** * Returns the default format used to generate new worlds. + * + * @phpstan-return class-string */ public static function getDefault() : string{ return self::$default; @@ -59,6 +64,7 @@ abstract class WorldProviderManager{ * Sets the default format. * * @param string $class Class implementing WritableWorldProvider + * @phpstan-param class-string $class * * @throws \InvalidArgumentException */ @@ -86,12 +92,12 @@ abstract class WorldProviderManager{ /** * Returns a WorldProvider class for this path, or null * - * @return string[]|WorldProvider[] + * @return string[] + * @phpstan-return array> */ public static function getMatchingProviders(string $path) : array{ $result = []; foreach(self::$providers as $alias => $provider){ - /** @var WorldProvider|string $provider */ if($provider::isValid($path)){ $result[$alias] = $provider; } @@ -100,7 +106,8 @@ abstract class WorldProviderManager{ } /** - * @return string[]|WorldProvider[] + * @return string[] + * @phpstan-return array> */ public static function getAvailableProviders() : array{ return self::$providers; diff --git a/tests/phpstan/configs/phpstan-bugs.neon b/tests/phpstan/configs/phpstan-bugs.neon index e2344dc5c..0cbf07abe 100644 --- a/tests/phpstan/configs/phpstan-bugs.neon +++ b/tests/phpstan/configs/phpstan-bugs.neon @@ -22,6 +22,12 @@ parameters: count: 1 path: ../../../src/MemoryManager.php + - + #is_a() type narrowing isn't respected yet + message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" + count: 1 + path: ../../../src/Server.php + - message: "#^Comparison operation \"\\>\\=\" between 0 and 2 is always false\\.$#" count: 1