Generate builtin stubs (+ add script to do so)
ci/woodpecker/push/woodpecker Pipeline failed Details

WIP-cache
Hugo Thunnissen 9 months ago
parent 5eae689f09
commit 9f7026455a

@ -10,11 +10,17 @@ cask: $(CASK_DIR)
.PHONY: compile
compile: cask
bash ./compile.bash
compile: generate-stubs
bash ./scripts/compile.bash
.PHONY: compile-native
compile-native: cask
bash ./native-compile.bash
compile-native: generate-stubs
bash ./scripts/native-compile.bash
.PHONY: generate-stubs
generate-stubs: cask
php ./scripts/generate-builtin-stubs.php > ./stubs/builtins.php
.PHONY: test
test: compile

@ -30,11 +30,17 @@
(current-buffer)
(point-max)))
(let ((here (file-name-directory (macroexp-file-name)))
result)
(let* ((here (file-name-directory (macroexp-file-name)))
(benchmark-file (or (getenv "PHPINSPECT_BENCHMARK_FILE")
(expand-file-name "Response.php" here)))
result)
(with-temp-buffer
(insert-file-contents (expand-file-name "Response.php" here))
(insert-file-contents benchmark-file)
(message "Incremental parse (warmup):")
(phpinspect-with-parse-context (phpinspect-make-pctx :incremental t :bmap (phpinspect-make-bmap))
@ -116,7 +122,7 @@
)))
(with-temp-buffer
(insert-file-contents (expand-file-name "Response.php" here))
(insert-file-contents benchmark-file)
(garbage-collect)
(message "Bare (no token reuse) parse (warmup):")

@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
function generateClassStub(\ReflectionClass $class): string
{
$stub = '';
$hasNamespace = '' !== $class->getNamespaceName();
if ($hasNamespace) {
$stub = 'namespace ' . $class->getNamespaceName() . ' {' . PHP_EOL;
}
if ($class->isFinal()) {
$stub .= 'final ';
}
if ($class->isAbstract() && !$class->isInterface()) {
$stub .= 'abstract ';
}
if ($class->isInterface()) {
$stub .= 'interface ';
} else if ($class->isTrait()) {
$stub .= 'trait ';
} else {
$stub .= 'class ';
}
$stub .= $class->getShortName() . ' {' . PHP_EOL;
foreach ($class->getMethods() as $method) {
if ($method->isFinal()) {
$stub .= 'final ';
}
if ($method->isPublic()) {
$stub .= 'public ';
} else if ($method->isPrivate()) {
$stub .= 'private ';
} else if ($method->isProtected()) {
$stub .= 'protected ';
}
if ($method->isStatic()) {
$stub .= 'static ';
}
$stub .= generateFunctionStub($method);
}
$stub .= '}' . PHP_EOL;
if ($hasNamespace) {
$stub .= '}' . PHP_EOL; // Close namespace block
}
return $stub;
}
function generateFunctionStub(\ReflectionFunctionAbstract $function): string
{
$stub = 'function ' . $function->getName() . '(';
$parameters = [];
foreach ($function->getParameters() as $ref_parameter) {
$parameter = '';
if ($ref_parameter->hasType()) {
$parameter .= $ref_parameter->getType()->__toString() . ' ';
}
$parameter .= '$' . $ref_parameter->getName();
if ($ref_parameter->isDefaultValueAvailable()) {
$parameter .= ' = ' . var_export($ref_parameter->getDefaultValue(), true);
}
$parameters[] = $parameter;
}
$stub .= implode(', ', $parameters);
$stub .= ')';
if ($function->hasReturnType()) {
$stub .= ': ' . $function->getReturnType()->__toString();
} else if ($function->hasTentativeReturnType()) {
$stub .= ': ?' . $function->getTentativeReturnType();
}
$stub .= ' {}' . PHP_EOL;
return $stub;
}
foreach (get_defined_functions(false)['internal'] as $function_name) {
echo generateFunctionStub(new ReflectionFunction($function_name));
}
foreach ([...get_declared_classes(), ...get_declared_interfaces(), ...get_declared_traits()] as $class_name) {
echo generateClassStub(new ReflectionClass($class_name));
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save