Skip to content

Instantly share code, notes, and snippets.

@ElfSundae
Forked from ardzz/phpdocs_facade_generator.php
Last active March 17, 2022 08:02
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 ElfSundae/3b57bf0c374bef6a29e58a36f04b7040 to your computer and use it in GitHub Desktop.
Save ElfSundae/3b57bf0c374bef6a29e58a36f04b7040 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(is_null($type)){
return '';
}
elseif(class_exists($type)){
return '\\' . $type . ' ';
}
else{
return $type . ' ';
}
}
/**
* @param ReflectionParameter[] $parameters
* @throws ReflectionException
*/
protected function processParameter(array $parameters): string
{
$output = [];
$processValue = function (mixed $value) {
$var = var_export($value, true);
if (gettype($value) === 'array') {
$var = substr($var, strlen('array ('), -2);
$var = preg_replace(['#\n\s+#', '#\d+\s=>\s#'], [' ', ''], $var);
$var = '['.trim($var, ', ').']';
}
return $var;
};
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();
}
}
$output = array_map('trim', $output);
return '(' . implode(', ', $output) . ')';
}
/**
* @throws ReflectionException
*/
function generate(int $filter = ReflectionMethod::IS_PUBLIC){
$methods = $this->getReflectionClass()->getMethods($filter);
$docs = '';
foreach ($methods as $method){
$docs .= $this->arrayToString([
' * @method static',
$this->getReturnType($method) . $method->getName() . $this->processParameter($method->getParameters()),
PHP_EOL
]);
}
return $docs;
}
/**
* @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);
}
public function passArray($a = [], $b = [1, 2], $c = ['foo' => 'bar'])
{
//
}
}
try {
echo FacadePHPDocs::make(FacadePHPDocs::class)->generate(-1);
} 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(int $filter = ReflectionMethod::IS_PUBLIC)
// * @method static static make(string $classname)
// * @method static string arrayToString(array $array)
// * @method static passArray($a = [], $b = [1, 2], $c = ['foo' => 'bar'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment