Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

Merge #118: php functions in braced namespaces

EL4CTEO9391af751b7b
diff --cc src/php.rs
index e9d1b2c,c42a0b2..73a3271
--- a/src/php.rs
+++ b/src/php.rs
@@@ -355,63 -360,57 +361,114 @@@ pub fn disable(ctx: &Ctx, port: &Port)
mod tests {
use super::*;
+ #[test]
+ fn ports_functions_in_braced_namespaces() {
+ if !Command::new("php").arg("-v").output().is_ok_and(|o| o.status.success()) {
+ eprintln!("no php on PATH, skipping");
+ return;
+ }
+ let dir = std::env::temp_dir().join(format!("fa-php-braced-test-{}", std::process::id()));
+ fs::create_dir_all(&dir).unwrap();
+ let app = "<?php\nnamespace Text {\n function width(string $s): int\n {\n return strlen($s);\n }\n}\n\nnamespace {\n echo \\Text\\width('h\u{e9}llo'), \"\\n\";\n}\n";
+ fs::write(dir.join("app.php"), app).unwrap();
+ let mut ctx = Ctx::open(&dir).unwrap();
+ let found = profile(&ctx, "php", &["app.php".into()]).unwrap();
+ assert!(found.functions.iter().any(|f| f.name == "width" && f.blockers.is_empty()), "{:?}", found.functions);
+ let hs = Hotspot {
+ id: "Text.width".into(),
+ file: "app.php".into(),
+ line: 3,
+ name: "width".into(),
+ ..Hotspot::default()
+ };
+ ctx.state.hotspots.push(hs);
+ let port = Port {
+ id: "Text.width".into(),
+ file: "app.php".into(),
+ module: "Text".into(),
+ func: "width".into(),
+ ext: "fa_text_width".into(),
+ crate_dir: ".fast/ports/Text.width".into(),
+ recorded: 1,
+ calls_seen: 1,
+ verify: None,
+ enabled: false,
+ lang: backend::Lang::Php,
+ owner: None,
+ line: 3,
+ sources: vec![],
+ };
+ enable(&ctx, &port).unwrap();
+ // without the port's library, the bridge doesn't load and the PHP version runs
+ let lint = Command::new("php").args(["-l", "app.php"]).current_dir(&dir).output().unwrap();
+ let run = Command::new("php").arg("app.php").current_dir(&dir).output().unwrap();
+ let enabled = fs::read_to_string(dir.join("app.php")).unwrap();
+ disable(&ctx, &port).unwrap();
+ let after = fs::read_to_string(dir.join("app.php")).unwrap();
+ let _ = fs::remove_dir_all(&dir);
+ assert!(lint.status.success(), "{}{enabled}", String::from_utf8_lossy(&lint.stdout));
+ assert_eq!(String::from_utf8_lossy(&run.stdout), "6\n", "{}", String::from_utf8_lossy(&run.stderr));
+ assert!(enabled.contains("namespace {\nfinal class"), "{enabled}");
+ assert_eq!(after, app);
+ }
+
+ #[test]
+ fn the_recorder_adds_edge_cases() {
+ use serde_json::json;
+ if !Command::new("php").arg("-v").output().is_ok_and(|o| o.status.success()) {
+ eprintln!("no php on PATH, skipping");
+ return;
+ }
+ let dir = std::env::temp_dir().join(format!("fa-php-edges-test-{}", std::process::id()));
+ fs::create_dir_all(&dir).unwrap();
+ fs::write(dir.join("rec.php"), REC).unwrap();
+ // width throws on an emoji: inputs it throws on aren't fair tests and must be left out
+ let check = r#"<?php
+ require __DIR__ . '/rec.php';
+ function width(string $s, array $xs): int
+ {
+ if (str_contains($s, "\u{1f44d}")) throw new InvalidArgumentException('emoji');
+ return preg_match_all('/./su', $s) + count($xs);
+ }
+ $s = str_repeat('word ', 40);
+ for ($k = 0; $k < 3; $k++) {
+ FastAnythingRec::record('width', [$s, array_fill(0, 20 + $k, 0)], 'width');
+ }
+ "#;
+ fs::write(dir.join("check.php"), check).unwrap();
+ let out = dir.join("rec.jsonl");
+ let run = Command::new("php")
+ .arg("check.php")
+ .current_dir(&dir)
+ .env("FAST_ANYTHING_RECORD_OUT", &out)
+ .env("FAST_ANYTHING_RECORD_META", dir.join("meta.json"))
+ .output()
+ .unwrap();
+ let text = fs::read_to_string(&out).unwrap_or_default();
+ let _ = fs::remove_dir_all(&dir);
+ assert!(
+ run.status.success(),
+ "{}{}",
+ String::from_utf8_lossy(&run.stdout),
+ String::from_utf8_lossy(&run.stderr)
+ );
+ let edges: Vec<Value> = text
+ .lines()
+ .map(|l| serde_json::from_str::<Value>(l).unwrap())
+ .filter(|c| c["derived"] == "edge")
+ .collect();
+ let has_arg = |want: &Value, i: usize| edges.iter().any(|c| &c["args"][i] == want);
+ assert!(has_arg(&json!(""), 0) && has_arg(&json!([]), 1), "empty inputs: {text}");
+ // non-ASCII text, added to the short version of the recorded input (64 characters, 8 items)
+ let short: String = "word ".repeat(40).chars().take(64).collect();
+ assert!(has_arg(&json!(format!("{short}\u{65e5}\u{672c}")), 0), "{text}");
+ assert!(!text.contains('\u{1f44d}'), "an input the original throws on");
+ assert!(edges.iter().any(|c| c["args"][1].as_array().is_some_and(|a| a.len() == 8 * 16)), "bigger arrays");
+ assert!(edges.iter().any(|c| c["args"][1][0] == json!(-1)), "special numbers in arrays");
+ assert!(edges.iter().all(|c| c["result"].is_i64()), "every edge has the original's answer");
+ assert!(edges.len() <= 100);
+ }
+
#[test]
fn php_bits() {
assert!(is_php("php") && is_php("php8.3") && is_php("C:\\php\\php.exe") && !is_php("phpunit"));