Lutra/crates/testkit/src/php.rs
danny 053441d066 M2.5: symbol index and go-to commands
crates/index: walks the project with the search/VFS rules (vendor as
library), parses .php files from tree-sitter outlines on a worker pool
off the engine lock, updates incrementally from disk and buffers through
an updater thread, caps at 200,000 files, and answers fuzzy file, class
and symbol queries (nucleo-matcher, MPL-2.0 as a separate crate).

Engine: index.status, goto.file, goto.class, goto.symbol; events
indexProgress and indexReady drained by the pump; the index starts on
project.open and is cancelled on close. Tests: unit, incremental,
property (random queries and sources), engine and TCP control tests,
release timing on a generated 20k-file project (3.9-4.2 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:53:33 +02:00

176 lines
6.9 KiB
Rust

//! Generated PHP projects for index tests and measurements.
//!
//! [`generate_php_project`] writes a deterministic Composer-style project of
//! any size, almost all PHP: classes, interfaces, traits, enums and function
//! files in `src/`, library classes in `vendor/` (gitignored, indexed as
//! library code), a few templates and config files, an ignored `var/cache/`
//! and an excluded `node_modules/`. Class files are about 5 KB, the size of
//! typical framework classes, so 20,000 files are about 100 MB of PHP.
use std::fs;
use std::path::Path;
/// What [`generate_php_project`] wrote.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PhpProject {
/// Files the index lists (everything but `var/` and `node_modules/`).
pub indexed_files: usize,
/// PHP files the index parses.
pub php_files: usize,
/// Of those, files under `vendor/`.
pub vendor_files: usize,
/// Classes, interfaces, traits and enums declared in indexed files.
pub classes: usize,
/// Bytes of indexed PHP.
pub php_bytes: u64,
}
/// Methods per generated class.
const METHODS: usize = 14;
/// Writes a project of about `files` files below `root`.
///
/// Shares: 80 % PHP in `src/` (one in ten an interface, one in twenty a
/// trait, one in twenty an enum, one in fifty a file of functions, the rest
/// classes), 15 % PHP in `vendor/acme/framework/src/`, 3 % Twig templates,
/// 2 % YAML config; plus `var/cache/` (ignored) and `node_modules/`
/// (excluded) with a few PHP files each that the index must skip.
///
/// Class `src/Module{m}/Service{i}.php` declares `App\Module{m}\Service{i}`
/// with methods `handle0`…, properties `$items` and `$name`, and constant
/// `MARKER`; vendor class `i` is `Acme\Framework\Component{m}\Widget{i}`.
///
/// # Panics
/// If a file cannot be written (test helper).
pub fn generate_php_project(root: &Path, files: usize) -> PhpProject {
let mut out = PhpProject::default();
write(root, ".gitignore", "/var/\n/vendor/\n", &mut out, false);
out.indexed_files += 1;
let src = files.saturating_mul(80) / 100;
for i in 0..src {
let module = i / 100;
let (name, text) = src_file(i, module);
let rel = format!("src/Module{module:03}/{name}.php");
write(root, &rel, &text, &mut out, true);
if i % 50 != 7 {
out.classes += 1;
}
}
let vendor = files.saturating_mul(15) / 100;
for i in 0..vendor {
let module = i / 100;
let rel = format!("vendor/acme/framework/src/Component{module:03}/Widget{i}.php");
let text = class_text(
&format!("Acme\\Framework\\Component{module:03}"),
&format!("Widget{i}"),
"class",
);
write(root, &rel, &text, &mut out, true);
out.vendor_files += 1;
out.classes += 1;
}
for i in 0..files.saturating_mul(3) / 100 {
let rel = format!("templates/page{i}.html.twig");
write(
root,
&rel,
"{% block body %}<p>{{ content }}</p>{% endblock %}\n",
&mut out,
false,
);
out.indexed_files += 1;
}
for i in 0..files.saturating_mul(2) / 100 {
let rel = format!("config/packages/package{i}.yaml");
write(
root,
&rel,
&format!("package{i}:\n enabled: true\n"),
&mut out,
false,
);
out.indexed_files += 1;
}
for i in 0..3 {
let text = class_text("Cache", &format!("Cached{i}"), "class");
fs::create_dir_all(root.join("var/cache")).unwrap();
fs::write(root.join(format!("var/cache/Cached{i}.php")), &text).unwrap();
fs::create_dir_all(root.join("node_modules/pkg")).unwrap();
fs::write(root.join(format!("node_modules/pkg/Hidden{i}.php")), &text).unwrap();
}
out
}
/// Name and text of the `i`th source file in module `module`.
fn src_file(i: usize, module: usize) -> (String, String) {
let namespace = format!("App\\Module{module:03}");
match i % 50 {
7 => (
format!("functions{i}"),
format!(
"<?php\n\nnamespace {namespace};\n\nfunction helper{i}(int $id): string\n{{\n return (string) $id;\n}}\n\nfunction format{i}(string $text): string\n{{\n return trim($text);\n}}\n\nconst LIMIT{i} = {i};\n"
),
),
n if n % 10 == 3 => (
format!("Contract{i}"),
class_text(&namespace, &format!("Contract{i}"), "interface"),
),
n if n % 20 == 5 => (
format!("Helps{i}"),
class_text(&namespace, &format!("Helps{i}"), "trait"),
),
n if n % 20 == 15 => (
format!("Status{i}"),
enum_text(&namespace, &format!("Status{i}")),
),
_ => (
format!("Service{i}"),
class_text(&namespace, &format!("Service{i}"), "class"),
),
}
}
/// A class, interface or trait of about 5 KB.
fn class_text(namespace: &str, name: &str, keyword: &str) -> String {
let mut s = format!(
"<?php\n\ndeclare(strict_types=1);\n\nnamespace {namespace};\n\nuse Psr\\Log\\LoggerInterface;\nuse App\\Contracts\\Handler;\n\n/**\n * {name} handles generated work for measurements.\n */\n{keyword} {name}\n{{\n"
);
if keyword != "interface" {
s.push_str(" public const MARKER = 'plain';\n\n private array $items = [];\n\n protected ?string $name = null;\n\n");
}
for m in 0..METHODS {
if keyword == "interface" {
s.push_str(&format!(
" /**\n * Handles step {m}.\n */\n public function handle{m}(int $id, string $name): ?string;\n\n"
));
continue;
}
s.push_str(&format!(
" /**\n * Handles step {m} of the generated workflow.\n */\n public function handle{m}(int $id, string $name): ?string\n {{\n $key = sprintf('%s-%d', $name, $id + {m});\n if (isset($this->items[$key])) {{\n return $this->items[$key];\n }}\n $this->items[$key] = strtoupper($name);\n return null;\n }}\n\n"
));
}
s.push_str("}\n");
s
}
/// A backed enum with a few cases and a method.
fn enum_text(namespace: &str, name: &str) -> String {
format!(
"<?php\n\nnamespace {namespace};\n\nenum {name}: string\n{{\n case Draft = 'draft';\n case Published = 'published';\n case Archived = 'archived';\n\n public function label(): string\n {{\n return ucfirst($this->value);\n }}\n}}\n"
)
}
/// Writes `text` to `root/rel`, creating directories, and counts it as an
/// indexed PHP file if `php`.
fn write(root: &Path, rel: &str, text: &str, out: &mut PhpProject, php: bool) {
let path = root.join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(path, text).unwrap();
if php {
out.indexed_files += 1;
out.php_files += 1;
out.php_bytes += text.len() as u64;
}
}