Last active
March 15, 2024 18:23
-
-
Save ardzz/473f8b994714370b917d6232ce5146f0 to your computer and use it in GitHub Desktop.
PHPDocs facades laravel generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class FacadePHPDocs | |
* | |
* @author Ardhana <[email protected]> | |
*/ | |
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
Is there a way to also return the return type? example: