Skip to content

Instantly share code, notes, and snippets.

@cspray
Created June 26, 2016 01:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspray/3a3c50847b6a473b81cb05df0253cae2 to your computer and use it in GitHub Desktop.
Save cspray/3a3c50847b6a473b81cb05df0253cae2 to your computer and use it in GitHub Desktop.
A gist to create a PHP class for stubbing from a classname
<?php
declare(strict_types = 1);
/**
* @license See LICENSE file in project root
*/
require_once '/spraybot-app/vendor/autoload.php';
$classes = [
'Ds\\Vector',
'Ds\\Deque',
'Ds\\Stack',
'Ds\\Map',
'Ds\\Set',
'Ds\\PriorityQueue',
'DS\\Queue',
'Ds\\Pair',
'ds'
];
$classInfos = [];
foreach ($classes as $class) {
$classInfo = new \Cspray\CodeAnvil\Info\ClassInfo();
$r = new ReflectionClass($class);
$classInfo->declareStrict();
$classInfo->setNamespace($r->getNamespaceName());
$classInfo->setName($r->getShortName());
foreach ($r->getMethods() as $method) {
$methodInfo = new \Cspray\CodeAnvil\Info\MethodInfo();
if ($method->isStatic()) {
$methodInfo->makeStatic();
}
$methodInfo->setName($method->getName());
if ($returnType = $method->getReturnType()) {
$methodInfo->setReturnType((string) $returnType);
}
foreach ($method->getParameters() as $parameter) {
$paramInfo = new \Cspray\CodeAnvil\Info\ParameterInfo();
$paramInfo->setName($parameter->getName());
if ($parameter->hasType()) {
$paramInfo->setTypeDeclaration((string) $parameter->getType());
}
if ($parameter->isDefaultValueAvailable()) {
$paramInfo->setDefaultValue($parameter->getDefaultValue());
}
$methodInfo->addParameter($paramInfo);
}
$classInfo->addMethod($methodInfo);
}
$classInfos[$class] = $classInfo;
}
$generator = new \Cspray\CodeAnvil\CodeGenerator();
foreach ($classInfos as $class => $classInfo) {
$path = '/spraybot-app/src/tmp/php-ds/' . str_replace('\\', '/', $class) . '.php';
echo var_export(file_put_contents($path, $generator->generate($classInfo))), PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment