Skip to content

Instantly share code, notes, and snippets.

@ralphschindler
Last active March 14, 2020 20:52
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ralphschindler/4757829 to your computer and use it in GitHub Desktop.
Save ralphschindler/4757829 to your computer and use it in GitHub Desktop.
IDE code-completion stub generation script that utilizes reflection. (Primary use would be for extension stubs.)
<?php
define('T', ' ');
define('N', PHP_EOL);
$functions = array();
$classes = array();
$constant_prefix = 'X_';
$php = '<?php' . N;
$php .= '/**' . N . ' * Generated stub file for code completion purposes' . N . ' */';
$php .= N . N;
foreach (get_defined_constants() as $cname => $cvalue) {
if (strpos($cname, $constant_prefix) === 0) {
$php .= 'define(\'' . $cname . '\', ' . $cvalue . ');' . N;
}
}
$php .= N;
foreach ($functions as $function) {
$refl = new ReflectionFunction($function);
$php .= 'function ' . $refl->getName() . '(';
foreach ($refl->getParameters() as $i => $parameter) {
if ($i >= 1) {
$php .= ', ';
}
if ($typehint = $parameter->getClass()) {
$php .= $typehint->getName() . ' ';
}
$php .= '$' . $parameter->getName();
if ($parameter->isDefaultValueAvailable()) {
$php .= ' = ' . $parameter->getDefaultValue();
}
}
$php .= ') {}' . N;
}
$php .= N;
foreach ($classes as $class) {
$refl = new ReflectionClass($class);
$php .= 'class ' . $refl->getName();
if ($parent = $refl->getParentClass()) {
$php .= ' extends ' . $parent->getName();
}
$php .= N . '{' . N;
foreach ($refl->getProperties() as $property) {
$php .= T . '$' . $property->getName() . ';' . N;
}
foreach ($refl->getMethods() as $method) {
if ($method->isPublic()) {
if ($method->getDocComment()) {
$php .= T . $method->getDocComment() . N;
}
$php .= T . 'public function ';
if ($method->returnsReference()) {
$php .= '&';
}
$php .= $method->getName() . '(';
foreach ($method->getParameters() as $i => $parameter) {
if ($i >= 1) {
$php .= ', ';
}
if ($parameter->isArray()) {
$php .= 'array ';
}
if ($typehint = $parameter->getClass()) {
$php .= $typehint->getName() . ' ';
}
$php .= '$' . $parameter->getName();
if ($parameter->isDefaultValueAvailable()) {
$php .= ' = ' . $parameter->getDefaultValue();
}
}
$php .= ') {}' . N;
}
}
$php .= '}';
}
echo $php . N;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment