Skip to content

Instantly share code, notes, and snippets.

@bulton-fr
Created February 22, 2020 19:22
Show Gist options
  • Save bulton-fr/b6a4b2d09538514555d25e89f76cd32d to your computer and use it in GitHub Desktop.
Save bulton-fr/b6a4b2d09538514555d25e89f76cd32d to your computer and use it in GitHub Desktop.
(cli only) To generate all @method annotations for a class
<?php
$cliArgs = getopt(
'',
[
'help::',
'class-name:',
'class-path:',
'vendor-path::'
]
);
if (isset($cliArgs['help'])) {
echo 'HELP Command :'."\n"
.'php '.__FILE__.' --class-name=<name> --class-path=<path> [--vendor-path=<path>]'."\n"
."\n"
.'Generate all @method annotation for a class docblock'."\n"
."\n"
.'options list :'."\n"
.' --help : Display this help'."\n"
.' --class-name=<name> : The class name for which @method annotation will be generated.'."\n"
.' --class-path=<path> : The class path.'."\n"
.' --vendor-path=<path> : If the composer autoload file must be loaded, the path to the vendor folder.'."\n"
;
exit;
}
//Check required arguments
if (isset($cliArgs['class-name']) === false) {
echo "\033[1;31mMissing parameter \"class-name\".\033[0m\n";
exit(1);
} elseif (isset($cliArgs['class-path']) === false) {
echo "\033[1;31mMissing parameter \"class-path\".\033[0m\n";
exit(1);
}
//Include composer autoload if asked
if (isset($cliArgs['vendor-path'])) {
$vendorPath = $cliArgs['vendor-path'];
if (file_exists($vendorPath) === false) {
echo "\033[1;31mVendor path $vendorPath not found.\033[0m\n";
exit(1);
}
require_once($vendorPath.'/autoload.php');
}
$classPath = $cliArgs['class-path'];
if (file_exists($classPath) === false) {
echo "\033[1;31mFile $classPath not found.\033[0m\n";
exit(1);
}
require_once($classPath);
$className = $cliArgs['class-name'];
if (class_exists($className) === false) {
echo "\033[1;31mUnknown class $className\033[0m\n";
exit(1);
}
$reflClass = new ReflectionClass($className);
$reflProperties = $reflClass->getProperties();
$methodsAnnotList = [];
foreach ($reflProperties as $reflProperty) {
$name = $reflProperty->getName();
$type = '';
$propClassName = $reflProperty->getDeclaringClass()->getName();
if ($propClassName !== $className) {
//Not add properties from parents classes.
continue;
}
if (method_exists($reflProperty, 'getType')) {
//PHP >= 7.4
$type = $reflProperty->getType();
} else {
//Get property type from @var docblock
$docComment = $reflProperty->getDocComment();
$regex = '/\@var ([a-zA-Z0-9\|\\\\]+)/m';
$matches = [];
if (preg_match($regex, $docComment, $matches) !== 1) {
echo "\033[1;33mProperty $name : Not found @var in docblock or unreadable value.\033[0m\n";
continue;
}
$type = $matches[1];
}
$methodsAnnotList[] = '@method '.$type.' get'.ucfirst($name).'()';
$methodsAnnotList[] = '@method self set'.ucfirst($name).'('.$type.' $'.$name.')';
}
//Display all @method annotations
echo "\n";
foreach ($methodsAnnotList as $methodAnnot) {
echo ' * '.$methodAnnot."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment