mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 10:53:05 +00:00
Move code & docblock generation methods out of RegistryTrait
this allows generating a docblock / code for a registry without the class needing to exist or be populated yet, which makes code generation significantly less cumbersome.
This commit is contained in:
@ -112,55 +112,4 @@ trait RegistryTrait{
|
||||
return self::preprocessMember($o);
|
||||
}, self::$members);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates code for static methods for all known registry members.
|
||||
*/
|
||||
public static function _generateGetters() : string{
|
||||
$lines = [];
|
||||
|
||||
static $fnTmpl = '
|
||||
public static function %1$s() : %2$s{
|
||||
return self::fromString("%1$s");
|
||||
}';
|
||||
|
||||
foreach(self::_registryGetAll() as $name => $member){
|
||||
$lines[] = sprintf($fnTmpl, $name, '\\' . get_class($member));
|
||||
}
|
||||
return "//region auto-generated code\n" . implode("\n", $lines) . "\n\n//endregion\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a block of @ method annotations for accessors for this registry's known members.
|
||||
*/
|
||||
public static function _generateMethodAnnotations() : string{
|
||||
$traitName = (new \ReflectionClass(__TRAIT__))->getShortName();
|
||||
$fnName = (new \ReflectionMethod(__METHOD__))->getShortName();
|
||||
$lines = ["/**"];
|
||||
$lines[] = " * This doc-block is generated automatically, do not modify it manually.";
|
||||
$lines[] = " * This must be regenerated whenever registry members are added, removed or changed.";
|
||||
$lines[] = " * @see $traitName::$fnName()";
|
||||
$lines[] = " *";
|
||||
static $lineTmpl = " * @method static %2\$s %s()";
|
||||
|
||||
$thisNamespace = (new \ReflectionClass(__CLASS__))->getNamespaceName();
|
||||
foreach(self::_registryGetAll() as $name => $member){
|
||||
$reflect = new \ReflectionClass($member);
|
||||
while($reflect !== false and $reflect->isAnonymous()){
|
||||
$reflect = $reflect->getParentClass();
|
||||
}
|
||||
if($reflect === false){
|
||||
$typehint = "object";
|
||||
}elseif($reflect->getName() === __CLASS__){
|
||||
$typehint = "self";
|
||||
}elseif(strpos($reflect->getName(), $thisNamespace) === 0){
|
||||
$typehint = substr($reflect->getName(), strlen($thisNamespace . '\\'));
|
||||
}else{
|
||||
$typehint = '\\' . $reflect->getName();
|
||||
}
|
||||
$lines[] = sprintf($lineTmpl, $name, $typehint);
|
||||
}
|
||||
$lines[] = " */\n";
|
||||
return implode("\n", $lines);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user