winzik/tools/generate-stubs.php
David Nurdin ffefc796f8 JumPHP: PHP framework for retro 2D games (desktop + WebAssembly)
Engine (tiles, collisions, entities, sound, menus, network), skeleton, Pong and
Breakout, Docker-only CLI bin/jumphp, FrankenPHP web server, AGENTS.md guide.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 18:11:08 +01:00

44 lines
1.9 KiB
PHP

<?php
// Writes engine/stubs/sdl.stub.php: signatures of the SDL extensions (functions, classes, constants) read
// by reflection, for phpstan and IDE completion. Run in the desktop image: bin/jumphp php . tools/generate-stubs.php
$out = "<?php\n\n// Generated by tools/generate-stubs.php from the SDL extensions of the desktop image. Do not edit.\n"
. "// The browser engine has the same extensions (same commits), plus wasmnet (php-wasm-network).\n\n";
foreach (['sdl', 'sdl_image', 'sdl_mixer', 'sdl_ttf'] as $name) {
$ext = new ReflectionExtension($name);
$out .= "// --- extension $name " . $ext->getVersion() . "\n\n";
foreach ($ext->getConstants() as $const => $value) {
$out .= 'const ' . $const . ' = ' . var_export($value, true) . ";\n";
}
foreach ($ext->getClasses() as $class) {
$out .= "\nclass {$class->getName()}\n{\n";
foreach ($class->getProperties() as $p) {
$out .= " public mixed \${$p->getName()};\n";
}
foreach ($class->getMethods() as $m) {
$out .= ' public ' . ($m->isStatic() ? 'static ' : '') . 'function ' . $m->getName() . '(' . params($m) . ") {}\n";
}
$out .= "}\n";
}
foreach ($ext->getFunctions() as $f) {
$out .= "\nfunction {$f->getName()}(" . params($f) . ')' . ($f->hasReturnType() ? ': ' . $f->getReturnType() : '') . " {}";
}
$out .= "\n\n";
}
function params(ReflectionFunctionAbstract $f): string
{
$list = [];
foreach ($f->getParameters() as $p) {
$s = ($p->hasType() ? $p->getType() . ' ' : '') . ($p->isPassedByReference() ? '&' : '') . ($p->isVariadic() ? '...' : '') . '$' . $p->getName();
if ($p->isOptional() && !$p->isVariadic()) {
$s .= ' = null';
}
$list[] = $s;
}
return implode(', ', $list);
}
@mkdir(__DIR__ . '/../engine/stubs');
file_put_contents(__DIR__ . '/../engine/stubs/sdl.stub.php', $out);
echo "engine/stubs/sdl.stub.php\n";