and more typehints

This commit is contained in:
Dylan K. Taylor
2017-07-14 10:56:51 +01:00
parent b9355387da
commit c3b8be3f60
119 changed files with 598 additions and 541 deletions

View File

@ -48,10 +48,8 @@ class PharPluginLoader implements PluginLoader{
* @param string $file
*
* @return Plugin|null
*
* @throws \Exception
*/
public function loadPlugin($file){
public function loadPlugin(string $file){
if(($description = $this->getPluginDescription($file)) instanceof PluginDescription){
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.plugin.load", [$description->getFullName()]));
$dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
@ -80,9 +78,9 @@ class PharPluginLoader implements PluginLoader{
*
* @param string $file
*
* @return PluginDescription|null
* @return null|PluginDescription
*/
public function getPluginDescription($file){
public function getPluginDescription(string $file){
$phar = new \Phar($file);
if(isset($phar["plugin.yml"])){
$pluginYml = $phar["plugin.yml"];
@ -99,7 +97,7 @@ class PharPluginLoader implements PluginLoader{
*
* @return string
*/
public function getPluginFilters(){
public function getPluginFilters() : string{
return "/\\.phar$/i";
}

View File

@ -50,7 +50,7 @@ interface Plugin extends CommandExecutor{
/**
* @return bool
*/
public function isEnabled();
public function isEnabled() : bool;
/**
* Called when the plugin is disabled
@ -58,61 +58,78 @@ interface Plugin extends CommandExecutor{
*/
public function onDisable();
public function isDisabled();
/**
* @return bool
*/
public function isDisabled() : bool;
/**
* Gets the plugin's data folder to save files and configuration.
* This directory name has a trailing slash.
*
* @return string
*/
public function getDataFolder();
public function getDataFolder() : string;
/**
* @return PluginDescription
*/
public function getDescription();
public function getDescription() : PluginDescription;
/**
* Gets an embedded resource in the plugin file.
*
* @param string $filename
*
* @return
*/
public function getResource($filename);
public function getResource(string $filename);
/**
* Saves an embedded resource to its relative location in the data folder
*
* @param string $filename
* @param bool $replace
* @param bool $replace
*
* @return bool
*/
public function saveResource($filename, $replace = false);
public function saveResource(string $filename, bool $replace = false) : bool;
/**
* Returns all the resources packaged with the plugin
*
* @return string[]
*/
public function getResources();
public function getResources() : array;
/**
* @return Config
*/
public function getConfig();
public function getConfig() : Config;
public function saveConfig();
public function saveDefaultConfig();
/**
* @return bool
*/
public function saveDefaultConfig() : bool;
public function reloadConfig();
/**
* @return Server
*/
public function getServer();
public function getServer() : Server;
public function getName();
/**
* @return string
*/
public function getName() : string;
/**
* @return PluginLogger
*/
public function getLogger();
public function getLogger() : PluginLogger;
/**
* @return PluginLoader

View File

@ -48,9 +48,11 @@ abstract class PluginBase implements Plugin{
/** @var string */
private $dataFolder;
private $config;
/** @var Config|null */
private $config = null;
/** @var string */
private $configFile;
/** @var string */
private $file;
/** @var PluginLogger */
@ -74,14 +76,14 @@ abstract class PluginBase implements Plugin{
/**
* @return bool
*/
final public function isEnabled(){
final public function isEnabled() : bool{
return $this->isEnabled === true;
}
/**
* @param bool $boolean
*/
final public function setEnabled($boolean = true){
final public function setEnabled(bool $boolean = true){
if($this->isEnabled !== $boolean){
$this->isEnabled = $boolean;
if($this->isEnabled === true){
@ -95,15 +97,15 @@ abstract class PluginBase implements Plugin{
/**
* @return bool
*/
final public function isDisabled(){
final public function isDisabled() : bool{
return $this->isEnabled === false;
}
final public function getDataFolder(){
final public function getDataFolder() : string{
return $this->dataFolder;
}
final public function getDescription(){
final public function getDescription() : PluginDescription{
return $this->description;
}
@ -123,14 +125,14 @@ abstract class PluginBase implements Plugin{
/**
* @return PluginLogger
*/
public function getLogger(){
public function getLogger() : PluginLogger{
return $this->logger;
}
/**
* @return bool
*/
final public function isInitialized(){
final public function isInitialized() : bool{
return $this->initialized;
}
@ -167,7 +169,7 @@ abstract class PluginBase implements Plugin{
/**
* @return bool
*/
protected function isPhar(){
protected function isPhar() : bool{
return strpos($this->file, "phar://") === 0;
}
@ -177,9 +179,9 @@ abstract class PluginBase implements Plugin{
*
* @param string $filename
*
* @return resource|null Resource data, or null
* @return null|resource Resource data, or null
*/
public function getResource($filename){
public function getResource(string $filename){
$filename = rtrim(str_replace("\\", "/", $filename), "/");
if(file_exists($this->file . "resources/" . $filename)){
return fopen($this->file . "resources/" . $filename, "rb");
@ -190,11 +192,11 @@ abstract class PluginBase implements Plugin{
/**
* @param string $filename
* @param bool $replace
* @param bool $replace
*
* @return bool
*/
public function saveResource($filename, $replace = false){
public function saveResource(string $filename, bool $replace = false) : bool{
if(trim($filename) === ""){
return false;
}
@ -223,7 +225,7 @@ abstract class PluginBase implements Plugin{
*
* @return string[]
*/
public function getResources(){
public function getResources() : array{
$resources = [];
if(is_dir($this->file . "resources/")){
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
@ -237,8 +239,8 @@ abstract class PluginBase implements Plugin{
/**
* @return Config
*/
public function getConfig(){
if(!isset($this->config)){
public function getConfig() : Config{
if($this->config === null){
$this->reloadConfig();
}
@ -251,7 +253,7 @@ abstract class PluginBase implements Plugin{
}
}
public function saveDefaultConfig(){
public function saveDefaultConfig() : bool{
if(!file_exists($this->configFile)){
return $this->saveResource("config.yml", false);
}
@ -269,25 +271,28 @@ abstract class PluginBase implements Plugin{
/**
* @return Server
*/
final public function getServer(){
final public function getServer() : Server{
return $this->server;
}
/**
* @return string
*/
final public function getName(){
final public function getName() : string{
return $this->description->getName();
}
/**
* @return string
*/
final public function getFullName(){
final public function getFullName() : string{
return $this->description->getFullName();
}
protected function getFile(){
/**
* @return string
*/
protected function getFile() : string{
return $this->file;
}

View File

@ -33,12 +33,17 @@ class PluginDescription{
private $depend = [];
private $softDepend = [];
private $loadBefore = [];
/** @var string */
private $version;
private $commands = [];
private $description = null;
/** @var string */
private $description = "";
/** @var string[] */
private $authors = [];
private $website = null;
private $prefix = null;
/** @var string */
private $website = "";
/** @var string */
private $prefix = "";
private $order = PluginLoadOrder::POSTWORLD;
/**
@ -131,42 +136,42 @@ class PluginDescription{
/**
* @return string
*/
public function getFullName(){
public function getFullName() : string{
return $this->name . " v" . $this->version;
}
/**
* @return array
*/
public function getCompatibleApis(){
public function getCompatibleApis() : array{
return $this->api;
}
/**
* @return array
* @return string[]
*/
public function getAuthors(){
public function getAuthors() : array{
return $this->authors;
}
/**
* @return string
*/
public function getPrefix(){
public function getPrefix() : string{
return $this->prefix;
}
/**
* @return array
*/
public function getCommands(){
public function getCommands() : array{
return $this->commands;
}
/**
* @return array
*/
public function getRequiredExtensions(){
public function getRequiredExtensions() : array{
return $this->extensions;
}
@ -210,49 +215,49 @@ class PluginDescription{
/**
* @return array
*/
public function getDepend(){
public function getDepend() : array{
return $this->depend;
}
/**
* @return string
*/
public function getDescription(){
public function getDescription() : string{
return $this->description;
}
/**
* @return array
*/
public function getLoadBefore(){
public function getLoadBefore() : array{
return $this->loadBefore;
}
/**
* @return string
*/
public function getMain(){
public function getMain() : string{
return $this->main;
}
/**
* @return string
*/
public function getName(){
public function getName() : string{
return $this->name;
}
/**
* @return int
*/
public function getOrder(){
public function getOrder() : int{
return $this->order;
}
/**
* @return Permission[]
*/
public function getPermissions(){
public function getPermissions() : array{
return $this->permissions;
}
@ -266,14 +271,14 @@ class PluginDescription{
/**
* @return string
*/
public function getVersion(){
public function getVersion() : string{
return $this->version;
}
/**
* @return string
*/
public function getWebsite(){
public function getWebsite() : string{
return $this->website;
}
}

View File

@ -33,25 +33,25 @@ interface PluginLoader{
*
* @param string $file
*
* @return Plugin
* @return Plugin|null
*/
public function loadPlugin($file);
public function loadPlugin(string $file);
/**
* Gets the PluginDescription from the file
*
* @param string $file
*
* @return PluginDescription
* @return null|PluginDescription
*/
public function getPluginDescription($file);
public function getPluginDescription(string $file);
/**
* Returns the filename regex patterns that this loader accepts
*
* @return string
*/
public function getPluginFilters();
public function getPluginFilters() : string;
/**
* @param Plugin $plugin

View File

@ -119,7 +119,7 @@ class PluginManager{
*
* @return bool
*/
public function registerInterface($loaderName){
public function registerInterface(string $loaderName) : bool{
if(is_subclass_of($loaderName, PluginLoader::class)){
$loader = new $loaderName($this->server);
}else{
@ -134,7 +134,7 @@ class PluginManager{
/**
* @return Plugin[]
*/
public function getPlugins(){
public function getPlugins() : array{
return $this->plugins;
}
@ -372,7 +372,7 @@ class PluginManager{
*
* @return bool
*/
public function addPermission(Permission $permission){
public function addPermission(Permission $permission) : bool{
if(!isset($this->permissions[$permission->getName()])){
$this->permissions[$permission->getName()] = $permission;
$this->calculatePermissionDefault($permission);
@ -471,9 +471,9 @@ class PluginManager{
/**
* @param string $permission
*
* @return Permissible[]
* @return array|Permissible[]
*/
public function getPermissionSubscriptions($permission){
public function getPermissionSubscriptions(string $permission) : array{
if(isset($this->permSubs[$permission])){
return $this->permSubs[$permission];
$subs = [];
@ -522,7 +522,7 @@ class PluginManager{
*
* @return Permissible[]
*/
public function getDefaultPermSubscriptions($op){
public function getDefaultPermSubscriptions(bool $op) : array{
$subs = [];
if($op === true){
@ -555,7 +555,7 @@ class PluginManager{
/**
* @return Permission[]
*/
public function getPermissions(){
public function getPermissions() : array{
return $this->permissions;
}
@ -594,7 +594,7 @@ class PluginManager{
*
* @return PluginCommand[]
*/
protected function parseYamlCommands(Plugin $plugin){
protected function parseYamlCommands(Plugin $plugin) : array{
$pluginCmds = [];
foreach($plugin->getDescription()->getCommands() as $key => $data){
@ -799,7 +799,7 @@ class PluginManager{
*
* @return HandlerList
*/
private function getEventListeners($event){
private function getEventListeners($event) : HandlerList{
if($event::$handlerList === null){
$event::$handlerList = new HandlerList();
}
@ -810,15 +810,15 @@ class PluginManager{
/**
* @return bool
*/
public function useTimings(){
public function useTimings() : bool{
return self::$useTimings;
}
/**
* @param bool $use
*/
public function setUseTimings($use){
self::$useTimings = (bool) $use;
public function setUseTimings(bool $use){
self::$useTimings = $use;
}
}

View File

@ -69,21 +69,21 @@ class RegisteredListener{
/**
* @return Listener
*/
public function getListener(){
public function getListener() : Listener{
return $this->listener;
}
/**
* @return Plugin
*/
public function getPlugin(){
public function getPlugin() : Plugin{
return $this->plugin;
}
/**
* @return int
*/
public function getPriority(){
public function getPriority() : int{
return $this->priority;
}
@ -106,7 +106,7 @@ class RegisteredListener{
/**
* @return bool
*/
public function isIgnoringCancelled(){
public function isIgnoringCancelled() : bool{
return $this->ignoreCancelled === true;
}
}

View File

@ -49,10 +49,8 @@ class ScriptPluginLoader implements PluginLoader{
* @param string $file
*
* @return Plugin|null
*
* @throws \Exception
*/
public function loadPlugin($file){
public function loadPlugin(string $file){
if(($description = $this->getPluginDescription($file)) instanceof PluginDescription){
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.plugin.load", [$description->getFullName()]));
$dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
@ -82,9 +80,9 @@ class ScriptPluginLoader implements PluginLoader{
*
* @param string $file
*
* @return PluginDescription|null
* @return null|PluginDescription
*/
public function getPluginDescription($file){
public function getPluginDescription(string $file){
$content = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$data = [];
@ -122,7 +120,7 @@ class ScriptPluginLoader implements PluginLoader{
*
* @return string
*/
public function getPluginFilters(){
public function getPluginFilters() : string{
return "/\\.php$/i";
}