phpstan: use more class-string

This commit is contained in:
Dylan K. Taylor 2020-01-31 22:05:33 +00:00
parent f65bf76fd8
commit 89c6da13ac
7 changed files with 70 additions and 8 deletions

View File

@ -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<Tile>|null
*/
private $tileClass;
/**
* @phpstan-param class-string<Tile>|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<Tile>|null
*/
public function getTileClass() : ?string{
return $this->tileClass;
}

View File

@ -44,7 +44,10 @@ final class TileFactory{
* @phpstan-var array<class-string<Tile>, list<string>>
*/
private static $saveNames = [];
/** @var string[] base class => overridden class */
/**
* @var string[] base class => overridden class
* @phpstan-var array<class-string<Tile>, class-string<Tile>>
*/
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<Tile> $baseClass
* @phpstan-param class-string<Tile> $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<TTile> $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<Tile> $class
*/
public static function getSaveId(string $class) : string{
if(isset(self::$saveNames[$class])){
return reset(self::$saveNames[$class]);

View File

@ -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<Entity>, class-string<Entity>>
*/
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<Entity> $baseClass
* @phpstan-param class-string<Entity> $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<Entity>[]
*/
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<TEntity> $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<Entity> $class
*/
public static function getSaveId(string $class) : string{
if(isset(self::$saveNames[$class])){
return reset(self::$saveNames[$class]);

View File

@ -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<Throwable>
*/
abstract public function getProjectileEntityClass() : string;

View File

@ -33,11 +33,15 @@ use function lcg_value;
class SpawnEgg extends Item{
/** @var string */
/**
* @var string
* @phpstan-var class-string<Entity>
*/
private $entityClass;
/**
* @param string $entityClass instanceof Entity
* @phpstan-param class-string<Entity> $entityClass
*
* @throws \InvalidArgumentException
*/

View File

@ -38,7 +38,10 @@ abstract class WorldProviderManager{
*/
protected static $providers = [];
/** @var string|WorldProvider */
/**
* @var string
* @phpstan-var class-string<WritableWorldProvider>
*/
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<WritableWorldProvider>
*/
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<WritableWorldProvider> $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<string, class-string<WorldProvider>>
*/
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<string, class-string<WorldProvider>>
*/
public static function getAvailableProviders() : array{
return self::$providers;

View File

@ -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\\<pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\>, class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\> given\\.$#"
count: 1
path: ../../../src/Server.php
-
message: "#^Comparison operation \"\\>\\=\" between 0 and 2 is always false\\.$#"
count: 1