Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save othyn/700230a372eb35a4a42b851744432e19 to your computer and use it in GitHub Desktop.
Save othyn/700230a372eb35a4a42b851744432e19 to your computer and use it in GitHub Desktop.
PHPDocs facades laravel generator
<?php
/**
* Class FacadePHPDocs
*
* @author Ardhana <ardzz@indoxploit.or.id>
*/
class FacadePHPDocs{
/**
* @var ReflectionClass
*/
private ReflectionClass $reflectionClass;
/**
* FacadePHPDocs constructor.
* @throws ReflectionException
*/
public function __construct(string $classname)
{
$this->reflectionClass = new ReflectionClass($classname);
}
/**
* @return ReflectionClass
*/
protected function getReflectionClass(): ReflectionClass
{
return $this->reflectionClass;
}
/**
* @param ReflectionMethod $method
* @return string|null
*/
protected function getReturnType(ReflectionMethod $method): ?string
{
$type = $method->getReturnType();
if(class_exists($type)){
return '\\' . $type . ' ';
}
elseif(is_null($type)){
return '';
}
else{
return $type . ' ';
}
}
/**
* @param ReflectionParameter[] $parameters
* @throws ReflectionException
*/
protected function processParameter(array $parameters): string
{
$output = [];
$processValue = function (mixed $value) {
return var_export($value, true);
};
foreach ($parameters as $parameter){
if ($parameter->isOptional()){
if ($parameter->isDefaultValueConstant()){
$output[] = (string) $parameter->getType() . ' $' . $parameter->getName() . ' = ' . $parameter->getDefaultValueConstantName();
}else{
$output[] = (string) $parameter->getType() . ' $' . $parameter->getName() . ' = ' . $processValue($parameter->getDefaultValue());
}
}else{
$output[] = (string) $parameter->getType() . ' $' . $parameter->getName();
}
}
return '(' . implode(', ', $output) . ')';
}
/**
* @throws ReflectionException
*/
function generate(){
$methods = $this->getReflectionClass()->getMethods();
foreach ($methods as $method){
echo $this->arrayToString([
' * @method static',
$this->getReturnType($method) . $method->getName() . $this->processParameter($method->getParameters()),
PHP_EOL
]);
}
}
/**
* @throws ReflectionException
*/
static function make(string $classname): static
{
return new static($classname);
}
/**
* @param array $array
* @return string
*/
protected function arrayToString(array $array): string
{
return implode(' ', $array);
}
}
try {
FacadePHPDocs::make(FacadePHPDocs::class)->generate();
} catch (ReflectionException $e) {
echo $e->getMessage() . PHP_EOL;
}
// example output
// * @method static __construct(string $classname)
// * @method static \ReflectionClass getReflectionClass()
// * @method static ?string getReturnType(ReflectionMethod $method)
// * @method static string processParameter(array $parameters)
// * @method static generate()
// * @method static static make(string $classname)
// * @method static string arrayToString(array $array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment