Skip to main content

mir_analyzer/
stubs.rs

1/// PHP built-in stubs — registered into the salsa db before user-code analysis.
2///
3/// Stubs are embedded at compile time from the `stubs/{ext}/` workspace directory via
4/// `build.rs`.  Each file gets a stable virtual path (e.g. `"stubs/standard/standard_9.php"`)
5/// so symbols are source-attributed and go-to-definition works for them.
6///
7/// # `StubVfs`
8///
9/// [`StubVfs`] maps every embedded stub file path to its `&'static str` content.
10/// Consumers use it to serve stub file content for go-to-definition on
11/// built-in PHP symbols:
12///
13/// ```ignore
14/// let file = db.symbol_defining_file("strlen"); // → "stubs/standard/standard_0.php"
15/// let src  = stub_vfs.get(&file).unwrap();           // → &'static str PHP source
16/// ```
17use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
18use std::ops::ControlFlow;
19use std::path::{Path, PathBuf};
20use std::sync::{Arc, LazyLock};
21
22use mir_codebase::storage::StubSlice;
23use php_ast::owned::visitor::{walk_owned_expr, walk_owned_program, OwnedVisitor};
24use php_ast::owned::ExprKind;
25use php_lexer::TokenKind;
26use rayon::prelude::*;
27
28use crate::db::MirDbStorage;
29use crate::php_version::PhpVersion;
30
31// Generated by build.rs: `static STUB_FILES: &[(&str, &str)]`
32include!(concat!(env!("OUT_DIR"), "/stub_files.rs"));
33
34// Generated by build.rs: `BUILTIN_FN_NAMES`, `STUB_FN_INDEX`,
35// `STUB_CLASS_INDEX`, `STUB_CONST_INDEX`. See build.rs for details.
36include!(concat!(env!("OUT_DIR"), "/phpstorm_builtin_fns.rs"));
37
38/// Look up the embedded stub file content for a virtual path (e.g.
39/// `"stubs/standard/standard_0.php"`). Returns `None` if the path isn't part
40/// of the embedded set.
41pub(crate) fn stub_content_for_path(path: &str) -> Option<&'static str> {
42    STUB_FILES
43        .iter()
44        .find_map(|&(p, c)| if p == path { Some(c) } else { None })
45}
46
47/// Look up the stub virtual path that defines a built-in PHP function.
48/// Lookup is case-insensitive (PHP function names are case-insensitive).
49/// Returns `None` if the function isn't a known built-in.
50pub(crate) fn stub_path_for_function(name: &str) -> Option<&'static str> {
51    let lower = name.to_ascii_lowercase();
52    STUB_FN_INDEX
53        .binary_search_by(|(k, _)| (*k).cmp(lower.as_str()))
54        .ok()
55        .map(|i| STUB_FN_INDEX[i].1)
56}
57
58/// Look up the stub virtual path that defines a built-in PHP class / interface
59/// / trait / enum. Lookup is case-insensitive. Strips a single leading
60/// backslash if present. Returns `None` if not a known built-in type.
61///
62/// Public so the [`StubClassResolver`] in this crate (used by
63/// `AnalysisSession` to make `find_class_like` aware of PHP built-ins)
64/// can reach it, and so LSP consumers building their own chained
65/// resolvers can compose it explicitly.
66pub fn stub_path_for_class(fqcn: &str) -> Option<&'static str> {
67    let trimmed = fqcn.strip_prefix('\\').unwrap_or(fqcn);
68    let lower = trimmed.to_ascii_lowercase();
69    STUB_CLASS_INDEX
70        .binary_search_by(|(k, _)| (*k).cmp(lower.as_str()))
71        .ok()
72        .map(|i| STUB_CLASS_INDEX[i].1)
73}
74
75/// Look up the stub virtual path that defines a built-in PHP constant.
76/// PHP constants are case-sensitive, so this lookup is exact.
77pub(crate) fn stub_path_for_constant(name: &str) -> Option<&'static str> {
78    let trimmed = name.strip_prefix('\\').unwrap_or(name);
79    STUB_CONST_INDEX
80        .binary_search_by(|(k, _)| (*k).cmp(trimmed))
81        .ok()
82        .map(|i| STUB_CONST_INDEX[i].1)
83}
84
85/// Scan PHP `source` for identifiers that match known built-in functions /
86/// classes / constants, and return the deduplicated list of stub virtual
87/// paths needed to cover them.
88///
89/// This is the core of the lazy-stub auto-discovery used by
90/// [`crate::AnalysisSession::ensure_stubs_for_source`]. Uses the PHP lexer
91/// to safely extract identifier tokens, avoiding manual byte-level scanning.
92/// False positives (e.g., `imagecreate` appearing inside a string literal)
93/// cost only one extra stub ingest — cheap and idempotent.
94pub(crate) fn collect_referenced_builtin_paths(source: &str) -> Vec<&'static str> {
95    use php_lexer::lex_all;
96
97    let mut tokens: HashSet<&str> = HashSet::default();
98    let (lexed, _errors) = lex_all(source);
99
100    let mut i = 0;
101    while i < lexed.len() {
102        let token = &lexed[i];
103        if token.kind == TokenKind::Identifier {
104            let start = token.span.start as usize;
105            let end = token.span.end as usize;
106            if let Some(mut text) = source.get(start..end) {
107                // Handle namespaced identifiers: Foo\Bar\Baz
108                let mut j = i + 1;
109                while j + 1 < lexed.len()
110                    && lexed[j].kind == TokenKind::Backslash
111                    && lexed[j + 1].kind == TokenKind::Identifier
112                {
113                    j += 2;
114                    if let Some(part) = source.get(start..(lexed[j - 1].span.end as usize)) {
115                        text = part;
116                    }
117                }
118                tokens.insert(text);
119                i = j;
120            } else {
121                i += 1;
122            }
123        } else {
124            i += 1;
125        }
126    }
127
128    let mut paths: HashSet<&'static str> = HashSet::default();
129    for token in tokens {
130        if let Some(p) = stub_path_for_function(token) {
131            paths.insert(p);
132        }
133        if let Some(p) = stub_path_for_class(token) {
134            paths.insert(p);
135        }
136        if let Some(p) = stub_path_for_constant(token) {
137            paths.insert(p);
138        }
139    }
140    paths.into_iter().collect()
141}
142
143/// Walk the parsed AST to find identifiers that match known built-in functions /
144/// classes / constants, and return the deduplicated list of stub virtual paths.
145///
146/// Unlike [`collect_referenced_builtin_paths`], this walks the real AST instead
147/// of doing a byte-level heuristic scan. This eliminates false positives from
148/// function names appearing inside string literals or comments. Used by
149/// [`crate::AnalysisSession::ensure_stubs_for_ast`] for single-file analysis
150/// where the AST is already available.
151pub(crate) fn collect_referenced_builtin_paths_from_ast(
152    program: &php_ast::owned::Program,
153) -> Vec<&'static str> {
154    let mut visitor = BuiltinRefVisitor {
155        paths: HashSet::default(),
156    };
157    let _ = walk_owned_program(&mut visitor, program);
158    visitor.paths.into_iter().collect()
159}
160
161/// Visitor for extracting function/class/constant references from the AST.
162struct BuiltinRefVisitor {
163    paths: HashSet<&'static str>,
164}
165
166impl OwnedVisitor for BuiltinRefVisitor {
167    fn visit_expr(&mut self, expr: &php_ast::owned::Expr) -> ControlFlow<()> {
168        match &expr.kind {
169            ExprKind::FunctionCall(call) => {
170                if let ExprKind::Identifier(name) = &call.name.kind {
171                    if let Some(p) = stub_path_for_function(name.as_ref()) {
172                        self.paths.insert(p);
173                    }
174                }
175            }
176            ExprKind::New(new_expr) => {
177                if let ExprKind::Identifier(name) = &new_expr.class.kind {
178                    if let Some(p) = stub_path_for_class(name.as_ref()) {
179                        self.paths.insert(p);
180                    }
181                }
182            }
183            ExprKind::StaticMethodCall(call) => {
184                if let ExprKind::Identifier(name) = &call.class.kind {
185                    if let Some(p) = stub_path_for_class(name.as_ref()) {
186                        self.paths.insert(p);
187                    }
188                }
189            }
190            ExprKind::ClassConstAccess(access) => {
191                if let ExprKind::Identifier(name) = &access.class.kind {
192                    if let Some(p) = stub_path_for_class(name.as_ref()) {
193                        self.paths.insert(p);
194                    }
195                }
196            }
197            ExprKind::Identifier(name) => {
198                if let Some(p) = stub_path_for_constant(name.as_ref()) {
199                    self.paths.insert(p);
200                }
201            }
202            _ => {}
203        }
204        walk_owned_expr(self, expr)
205    }
206}
207
208// ---------------------------------------------------------------------------
209// Public accessors for embedded stub data
210// ---------------------------------------------------------------------------
211
212/// Every PHP stub file from `stubs/{ext}/` embedded at compile time as `(path, content)`.
213///
214/// Path is workspace-relative (e.g. `"stubs/standard/standard_9.php"`).
215pub fn stub_files() -> &'static [(&'static str, &'static str)] {
216    STUB_FILES
217}
218
219// ---------------------------------------------------------------------------
220// Public entry point
221// ---------------------------------------------------------------------------
222
223/// Default-version entry point retained for callers (CLI, benches, tests) that
224/// don't track a target PHP version. Equivalent to
225/// [`load_stubs_for_version`] with `PhpVersion::LATEST`.
226#[allow(dead_code)]
227pub(crate) fn load_stubs(db: &mut MirDbStorage) {
228    load_stubs_for_version(db, PhpVersion::LATEST);
229}
230
231/// Load stubs filtered for `php_version`. Symbols whose `@since` post-dates
232/// the target, or whose `@removed` is at or before the target, are skipped —
233/// so multiple declarations of the same name (e.g. `each` on PHP 7 vs.
234/// PHP 8) gated by `@since`/`@removed` collapse to the one matching variant.
235#[allow(dead_code)]
236pub(crate) fn load_stubs_for_version(db: &mut MirDbStorage, php_version: PhpVersion) {
237    // Wire the target version before registering any SourceFile inputs so
238    // collect_file_definitions reads the right version for @since/@removed filtering.
239    db.set_php_version(Arc::from(php_version.to_string().as_str()));
240    // Register each stub file's text as a salsa `SourceFile` input.
241    // `find_class_like` / `find_function` resolve built-in PHP symbols by
242    // routing through the `StubClassResolver` we install below.
243    for (filename, content) in STUB_FILES {
244        let arc_path: Arc<str> = Arc::from(*filename);
245        let arc_content: Arc<str> = Arc::from(*content);
246        db.upsert_source_file(arc_path, arc_content);
247    }
248    let resolver: Arc<dyn crate::ClassResolver> = Arc::new(crate::StubClassResolver);
249    db.set_resolver(Some(resolver));
250}
251
252pub(crate) fn builtin_stub_slices_for_version(php_version: PhpVersion) -> Vec<StubSlice> {
253    STUB_FILES
254        .par_iter()
255        .map(|(filename, content)| stub_slice_from_source(filename, content, Some(php_version)))
256        .collect()
257}
258
259pub(crate) fn user_stub_slices(files: &[PathBuf], dirs: &[PathBuf]) -> Vec<StubSlice> {
260    let mut all_paths: Vec<PathBuf> = files.to_vec();
261    for dir in dirs {
262        collect_stub_dir_paths(dir, &mut all_paths);
263    }
264
265    all_paths
266        .par_iter()
267        .filter_map(|path| parse_stub_file_slice(path))
268        .collect()
269}
270
271pub(crate) fn stub_slice_from_source(
272    filename: &str,
273    content: &str,
274    php_version: Option<PhpVersion>,
275) -> StubSlice {
276    let result = php_rs_parser::parse(content);
277    let file: Arc<str> = Arc::from(filename);
278    let collector =
279        crate::collector::DefinitionCollector::new_for_slice(file, content, &result.source_map);
280    let collector = match php_version {
281        Some(version) => collector.with_php_version(version),
282        None => collector,
283    };
284    let (mut slice, _) = collector.collect_slice(&result.program);
285    mir_codebase::storage::deduplicate_params_in_slice(&mut slice);
286    slice
287}
288
289fn parse_stub_file_slice(path: &Path) -> Option<StubSlice> {
290    let content = match std::fs::read_to_string(path) {
291        Ok(c) => c,
292        Err(e) => {
293            eprintln!("mir: cannot read stub file {}: {}", path.display(), e);
294            return None;
295        }
296    };
297    Some(stub_slice_from_source(
298        path.to_string_lossy().as_ref(),
299        &content,
300        None,
301    ))
302}
303
304pub(crate) fn collect_stub_dir_paths(dir: &Path, paths: &mut Vec<PathBuf>) {
305    let entries = match std::fs::read_dir(dir) {
306        Ok(e) => e,
307        Err(e) => {
308            eprintln!("mir: cannot read stub directory {}: {}", dir.display(), e);
309            return;
310        }
311    };
312    let mut dir_entries: Vec<PathBuf> = entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
313    dir_entries.sort_unstable();
314    for path in dir_entries {
315        if path.is_dir() {
316            collect_stub_dir_paths(&path, paths);
317        } else if path.extension().is_some_and(|e| e == "php") {
318            paths.push(path);
319        }
320    }
321}
322
323/// Fingerprint the user-configured stub set (paths + contents) for the result
324/// cache epoch. User stubs are resolvable just like bundled ones, so editing,
325/// adding, or removing them changes analysis output for files that reference
326/// those symbols — and the per-file content hash can't see it. Returns 0 when
327/// no user stubs are configured (the common case), so the epoch is unaffected.
328pub(crate) fn user_stub_fingerprint(files: &[PathBuf], dirs: &[PathBuf]) -> u64 {
329    if files.is_empty() && dirs.is_empty() {
330        return 0;
331    }
332    let mut all_paths: Vec<PathBuf> = files.to_vec();
333    for dir in dirs {
334        collect_stub_dir_paths(dir, &mut all_paths);
335    }
336    // Sort for a path-order-independent fingerprint.
337    all_paths.sort_unstable();
338    let mut hasher = blake3::Hasher::new();
339    for path in &all_paths {
340        hasher.update(path.to_string_lossy().as_bytes());
341        hasher.update(&[0]);
342        // Content too, so an in-place edit of a stub flips the fingerprint.
343        // A read failure still contributes the path, so add/remove is caught.
344        if let Ok(src) = std::fs::read(path) {
345            hasher.update(&src);
346        }
347        hasher.update(&[0]);
348    }
349    let bytes = hasher.finalize();
350    u64::from_le_bytes(bytes.as_bytes()[..8].try_into().unwrap())
351}
352
353/// Parse user-provided stub files and directories into `codebase`.
354///
355/// Called after built-in stubs are loaded so user definitions can override or
356/// supplement built-ins (e.g. framework-specific classes, IDE helpers).
357#[allow(dead_code)]
358pub(crate) fn load_user_stubs(_db: &mut MirDbStorage, files: &[PathBuf], dirs: &[PathBuf]) {
359    // No-op: user stub registration now happens through db::ingest_user_stubs
360    // which registers SourceFile inputs directly for the pull-based salsa path.
361    let _ = user_stub_slices(files, dirs);
362}
363
364// ---------------------------------------------------------------------------
365// StubVfs — virtual file system for stub content
366// ---------------------------------------------------------------------------
367
368/// A read-only map from stub file path to its embedded PHP source content.
369///
370/// Consumers use this to serve stub source for go-to-definition on built-in
371/// PHP symbols.
372///
373/// # Example
374///
375/// ```ignore
376/// let vfs = StubVfs::new();
377/// // After analysis:
378/// if let Some(path) = session.symbol_defining_file("strlen") {
379///     if let Some(src) = vfs.get(path.as_ref()) {
380///         // serve `src` as a read-only virtual document
381///     }
382/// }
383/// ```
384pub struct StubVfs {
385    files: HashMap<&'static str, &'static str>,
386}
387
388impl StubVfs {
389    /// Build the VFS from the embedded stub set.
390    pub fn new() -> Self {
391        let files = STUB_FILES.iter().map(|&(p, c)| (p, c)).collect();
392        Self { files }
393    }
394
395    /// Return the PHP source for `path`, or `None` if it is not a stub file.
396    pub fn get(&self, path: &str) -> Option<&'static str> {
397        self.files.get(path).copied()
398    }
399
400    /// Return `true` if `path` is a known stub file path.
401    pub fn is_stub_file(&self, path: &str) -> bool {
402        self.files.contains_key(path)
403    }
404}
405
406impl Default for StubVfs {
407    fn default() -> Self {
408        Self::new()
409    }
410}
411
412// ---------------------------------------------------------------------------
413// StubClassResolver — ClassResolver for bundled PHP built-in stubs
414// ---------------------------------------------------------------------------
415
416/// [`crate::ClassResolver`] that maps PHP built-in class FQCNs
417/// (`ArrayObject`, `Exception`, `ReflectionClass`, …) to the stub virtual
418/// path that defines them. Used by [`crate::ChainedClassResolver`] to
419/// make `find_class_like` aware of PHP built-ins in addition to the
420/// user's PSR-4 / classmap.
421///
422/// The returned paths are the same virtual paths used by [`StubVfs`] and
423/// matched against `MirDbStorage`'s registered `SourceFile`s when stubs are
424/// loaded (see `load_stubs_for_version`, which calls `upsert_source_file`
425/// for each stub).
426pub struct StubClassResolver;
427
428impl crate::ClassResolver for StubClassResolver {
429    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
430        // Try classes first; then functions / constants — built-in FQNs
431        // share a single resolver since they all map to the bundled stub
432        // VFS paths registered as SourceFile inputs.
433        stub_path_for_class(fqcn)
434            .or_else(|| stub_path_for_function(fqcn))
435            .or_else(|| stub_path_for_constant(fqcn))
436            .map(std::path::PathBuf::from)
437    }
438}
439
440/// [`crate::ClassResolver`] composing a primary resolver (user's PSR-4 /
441/// classmap) with a fallback (typically [`StubClassResolver`]). The
442/// primary is consulted first; if it misses, the fallback is tried.
443///
444/// `AnalysisSession::with_psr4` and `with_class_resolver` automatically
445/// wrap the user-supplied resolver in this chain so PHP built-ins are
446/// resolvable through `resolve_fqcn_to_path` (and therefore
447/// `find_class_like`) without per-consumer setup.
448pub struct ChainedClassResolver {
449    primary: Arc<dyn crate::ClassResolver>,
450    fallback: Arc<dyn crate::ClassResolver>,
451}
452
453impl ChainedClassResolver {
454    pub fn new(
455        primary: Arc<dyn crate::ClassResolver>,
456        fallback: Arc<dyn crate::ClassResolver>,
457    ) -> Self {
458        Self { primary, fallback }
459    }
460}
461
462impl crate::ClassResolver for ChainedClassResolver {
463    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
464        self.primary
465            .resolve(fqcn)
466            .or_else(|| self.fallback.resolve(fqcn))
467    }
468}
469
470// ---------------------------------------------------------------------------
471// Builtin-function query
472// ---------------------------------------------------------------------------
473
474/// Returns `true` if `name` is a known PHP built-in function.
475///
476/// Fast path: binary search on `BUILTIN_FN_NAMES`, a sorted compile-time slice
477/// generated from `PhpStormStubsMap.php` by `build.rs`.
478///
479/// Fallback (when `BUILTIN_FN_NAMES` is empty): reads the embedded stub slices and checks
480/// the embedded stubs and checks membership there.
481///
482/// # Example
483/// ```
484/// assert!(mir_analyzer::stubs::is_builtin_function("strlen"));
485/// assert!(!mir_analyzer::stubs::is_builtin_function("my_custom_function"));
486/// ```
487pub fn is_builtin_function(name: &str) -> bool {
488    if !BUILTIN_FN_NAMES.is_empty() {
489        return BUILTIN_FN_NAMES.binary_search(&name).is_ok();
490    }
491    static FALLBACK: LazyLock<HashSet<Arc<str>>> = LazyLock::new(|| {
492        builtin_stub_slices_for_version(PhpVersion::LATEST)
493            .into_iter()
494            .flat_map(|slice| slice.functions.into_iter().map(|func| func.fqn.clone()))
495            .collect()
496    });
497    FALLBACK.contains(name)
498}
499
500// ---------------------------------------------------------------------------
501// Tests
502// ---------------------------------------------------------------------------
503
504#[cfg(test)]
505mod tests {
506    use super::*;
507    use crate::db::{class_exists, constant_exists, function_exists, MirDatabase, MirDbStorage};
508
509    fn stubs_codebase() -> MirDbStorage {
510        let mut db = MirDbStorage::default();
511        load_stubs(&mut db);
512        db
513    }
514
515    fn stubs_codebase_for(version: PhpVersion) -> MirDbStorage {
516        let mut db = MirDbStorage::default();
517        load_stubs_for_version(&mut db, version);
518        db
519    }
520
521    fn stub_function_for(
522        version: PhpVersion,
523        name: &str,
524    ) -> Option<std::sync::Arc<mir_codebase::FunctionDef>> {
525        builtin_stub_slices_for_version(version)
526            .into_iter()
527            .flat_map(|slice| slice.functions.into_iter())
528            .find(|func| func.fqn.as_ref() == name)
529    }
530
531    fn stub_class_for(
532        version: PhpVersion,
533        name: &str,
534    ) -> Option<std::sync::Arc<mir_codebase::ClassDef>> {
535        builtin_stub_slices_for_version(version)
536            .into_iter()
537            .flat_map(|slice| slice.classes.into_iter())
538            .find(|cls| cls.fqcn.as_ref() == name)
539    }
540
541    #[test]
542    fn since_filter_applies_to_methods() {
543        // Direct stub-slice probe — independent of which path is consulted
544        // for symbol lookup.
545        let cls = stub_class_for(PhpVersion::new(7, 4), "DateTimeImmutable")
546            .expect("DateTimeImmutable must exist");
547        assert!(
548            !cls.own_methods.contains_key("createfrominterface"),
549            "createFromInterface should be absent on PHP 7.4"
550        );
551
552        let cls_new = stub_class_for(PhpVersion::new(8, 0), "DateTimeImmutable")
553            .expect("DateTimeImmutable must exist");
554        assert!(
555            cls_new.own_methods.contains_key("createfrominterface"),
556            "createFromInterface should be present on PHP 8.0"
557        );
558    }
559
560    #[test]
561    fn since_tag_excludes_function_below_target() {
562        let cb = stubs_codebase_for(PhpVersion::new(7, 4));
563        assert!(
564            !function_exists(&cb, "str_contains"),
565            "str_contains (@since 8.0) must be absent on PHP 7.4"
566        );
567        assert!(
568            !function_exists(&cb, "str_starts_with"),
569            "str_starts_with (@since 8.0) must be absent on PHP 7.4"
570        );
571        assert!(
572            !function_exists(&cb, "array_is_list"),
573            "array_is_list (@since 8.1) must be absent on PHP 7.4"
574        );
575
576        let cb80 = stubs_codebase_for(PhpVersion::new(8, 0));
577        assert!(
578            function_exists(&cb80, "str_contains"),
579            "str_contains must be present on PHP 8.0"
580        );
581        assert!(
582            !function_exists(&cb80, "array_is_list"),
583            "array_is_list (@since 8.1) must be absent on PHP 8.0"
584        );
585
586        let cb81 = stubs_codebase_for(PhpVersion::new(8, 1));
587        assert!(
588            function_exists(&cb81, "array_is_list"),
589            "array_is_list must be present on PHP 8.1"
590        );
591    }
592
593    #[test]
594    fn removed_tag_excludes_function_at_or_after_target() {
595        // hebrevc() was removed in PHP 8.0 (@removed 8.0).
596        let cb74 = stubs_codebase_for(PhpVersion::new(7, 4));
597        assert!(
598            function_exists(&cb74, "hebrevc"),
599            "hebrevc must be present on PHP 7.4"
600        );
601
602        let cb80 = stubs_codebase_for(PhpVersion::new(8, 0));
603        assert!(
604            !function_exists(&cb80, "hebrevc"),
605            "hebrevc (@removed 8.0) must be absent on PHP 8.0"
606        );
607    }
608
609    fn assert_fn(cb: &MirDbStorage, name: &str) {
610        assert!(
611            function_exists(cb, name),
612            "expected stub for `{name}` to be registered"
613        );
614    }
615
616    #[test]
617    fn sscanf_vars_param_is_byref_and_variadic() {
618        let func = stub_function_for(PhpVersion::LATEST, "sscanf").expect("sscanf must be defined");
619        let vars = func.params.get(2).expect("sscanf must have a 3rd param");
620        assert!(vars.is_byref, "sscanf vars param must be by-ref");
621        assert!(vars.is_variadic, "sscanf vars param must be variadic");
622    }
623
624    #[test]
625    fn sscanf_output_vars_not_undefined() {
626        use crate::batch::BatchOptions;
627        use crate::session::AnalysisSession;
628        use crate::PhpVersion;
629        use mir_issues::IssueKind;
630
631        let src = "<?php\nfunction test($str) {\n    sscanf($str, \"%d %d\", $row, $col);\n    return $row + $col;\n}\n";
632        let tmp = std::env::temp_dir().join("mir_test_sscanf_undefined.php");
633        std::fs::write(&tmp, src).unwrap();
634        let session = AnalysisSession::new(PhpVersion::LATEST);
635        let result = session.analyze_paths(std::slice::from_ref(&tmp), &BatchOptions::new());
636        std::fs::remove_file(tmp).ok();
637        let undef: Vec<_> = result.issues.iter()
638            .filter(|i| matches!(&i.kind, IssueKind::UndefinedVariable { name } if name == "row" || name == "col"))
639            .collect();
640        assert!(
641            undef.is_empty(),
642            "sscanf output vars must not be reported as UndefinedVariable; got: {undef:?}"
643        );
644    }
645
646    #[test]
647    fn stream_functions_are_defined() {
648        let cb = stubs_codebase();
649        assert_fn(&cb, "stream_isatty");
650        assert_fn(&cb, "stream_select");
651        assert_fn(&cb, "stream_get_meta_data");
652        assert_fn(&cb, "stream_set_blocking");
653        assert_fn(&cb, "stream_copy_to_stream");
654    }
655
656    #[test]
657    fn preg_grep_is_defined() {
658        let cb = stubs_codebase();
659        assert_fn(&cb, "preg_grep");
660    }
661
662    #[test]
663    fn standard_missing_functions_are_defined() {
664        let cb = stubs_codebase();
665        assert_fn(&cb, "get_resource_type");
666        assert_fn(&cb, "ftruncate");
667        assert_fn(&cb, "umask");
668        assert_fn(&cb, "date_default_timezone_set");
669        assert_fn(&cb, "date_default_timezone_get");
670    }
671
672    #[test]
673    fn mb_missing_functions_are_defined() {
674        let cb = stubs_codebase();
675        assert_fn(&cb, "mb_strwidth");
676        assert_fn(&cb, "mb_convert_variables");
677    }
678
679    #[test]
680    fn pcntl_functions_are_defined() {
681        let cb = stubs_codebase();
682        assert_fn(&cb, "pcntl_signal");
683        assert_fn(&cb, "pcntl_async_signals");
684        assert_fn(&cb, "pcntl_signal_get_handler");
685        assert_fn(&cb, "pcntl_alarm");
686    }
687
688    #[test]
689    fn posix_functions_are_defined() {
690        let cb = stubs_codebase();
691        assert_fn(&cb, "posix_kill");
692        assert_fn(&cb, "posix_getpid");
693    }
694
695    #[test]
696    fn sapi_windows_functions_are_defined() {
697        let cb = stubs_codebase();
698        assert_fn(&cb, "sapi_windows_vt100_support");
699        assert_fn(&cb, "sapi_windows_cp_set");
700        assert_fn(&cb, "sapi_windows_cp_get");
701        assert_fn(&cb, "sapi_windows_cp_conv");
702    }
703
704    #[test]
705    fn cli_functions_are_defined() {
706        let cb = stubs_codebase();
707        assert_fn(&cb, "cli_set_process_title");
708        assert_fn(&cb, "cli_get_process_title");
709    }
710
711    #[test]
712    fn builtin_fn_names_has_sufficient_entries() {
713        // Guards against PhpStormStubsMap.php parsing regressions in build.rs.
714        // When the submodule is absent the slice is empty — skip the check in that case.
715        if BUILTIN_FN_NAMES.is_empty() {
716            return;
717        }
718        assert!(
719            BUILTIN_FN_NAMES.len() >= 500,
720            "BUILTIN_FN_NAMES has only {} entries — \
721             build.rs may have failed to parse PhpStormStubsMap.php correctly",
722            BUILTIN_FN_NAMES.len()
723        );
724    }
725
726    #[test]
727    fn preg_match_matches_param_is_byref_with_type() {
728        let func = stub_function_for(PhpVersion::LATEST, "preg_match")
729            .expect("preg_match should exist in stubs");
730        let matches_param = func
731            .params
732            .iter()
733            .find(|p| p.name.as_ref() == "matches")
734            .expect("preg_match should have a $matches param");
735        assert!(matches_param.is_byref, "$matches should be byref");
736        assert!(
737            matches_param.ty.is_some(),
738            "$matches should have a type annotation (string[] from PHPDoc)"
739        );
740    }
741
742    #[test]
743    fn is_builtin_function_returns_true_for_known_builtins() {
744        assert!(is_builtin_function("strlen"), "strlen should be a builtin");
745        assert!(
746            is_builtin_function("array_map"),
747            "array_map should be a builtin"
748        );
749        assert!(
750            is_builtin_function("json_encode"),
751            "json_encode should be a builtin"
752        );
753        assert!(
754            is_builtin_function("preg_match"),
755            "preg_match should be a builtin"
756        );
757    }
758
759    #[test]
760    fn is_builtin_function_covers_stdlib_functions() {
761        assert!(is_builtin_function("bcadd"), "bcadd should be a builtin");
762        assert!(
763            is_builtin_function("sodium_crypto_secretbox"),
764            "sodium_crypto_secretbox should be a builtin"
765        );
766    }
767
768    #[test]
769    fn is_builtin_function_returns_false_for_unknown_names() {
770        assert!(
771            !is_builtin_function("my_custom_function"),
772            "my_custom_function should not be a builtin"
773        );
774        assert!(
775            !is_builtin_function(""),
776            "empty string should not be a builtin"
777        );
778        assert!(
779            !is_builtin_function("ast\\parse_file"),
780            "extension function should not be a builtin"
781        );
782    }
783
784    // --- stub coverage tests ---
785
786    fn assert_cls(cb: &MirDbStorage, name: &str) {
787        assert!(
788            class_exists(cb, name),
789            "expected stub class `{name}` to be registered"
790        );
791    }
792
793    fn assert_iface(cb: &MirDbStorage, name: &str) {
794        assert!(
795            class_exists(cb, name),
796            "expected stub interface `{name}` to be registered"
797        );
798    }
799
800    fn assert_const(cb: &MirDbStorage, name: &str) {
801        assert!(
802            constant_exists(cb, name),
803            "expected stub constant `{name}` to be registered"
804        );
805    }
806
807    #[test]
808    fn stubs_coverage_counts() {
809        let mut fn_count = 0usize;
810        let mut type_count = 0usize;
811        let mut const_count = 0usize;
812        for slice in builtin_stub_slices_for_version(PhpVersion::LATEST) {
813            fn_count += slice.functions.len();
814            type_count += slice.classes.len()
815                + slice.interfaces.len()
816                + slice.traits.len()
817                + slice.enums.len();
818            const_count += slice.constants.len();
819        }
820        assert!(fn_count > 500, "expected >500 functions, got {fn_count}");
821        assert!(type_count > 120, "expected >120 types, got {type_count}");
822        assert!(
823            const_count > 200,
824            "expected >200 constants, got {const_count}"
825        );
826    }
827
828    #[test]
829    fn curl_multi_exec_still_running_is_byref() {
830        let func = stub_function_for(PhpVersion::LATEST, "curl_multi_exec")
831            .expect("curl_multi_exec must be defined");
832        let still_running = func
833            .params
834            .iter()
835            .find(|p| p.name.as_ref() == "still_running")
836            .expect("curl_multi_exec must have a still_running param");
837        assert!(
838            still_running.is_byref,
839            "curl_multi_exec $still_running must be by-ref (generated from PHP stub)"
840        );
841    }
842
843    // --- Stub loading regression tests ---
844
845    #[test]
846    fn stub_files_are_non_empty() {
847        // Regression: STUB_FILES was silently empty when build.rs used
848        // the crate manifest dir instead of the workspace root to locate stubs/.
849        assert!(
850            !STUB_FILES.is_empty(),
851            "STUB_FILES must not be empty — check build.rs find_workspace_root()"
852        );
853    }
854
855    #[test]
856    fn stub_vfs_resolves_all_paths() {
857        let vfs = StubVfs::new();
858        for &(path, expected_content) in STUB_FILES {
859            let got = vfs
860                .get(path)
861                .unwrap_or_else(|| panic!("StubVfs::get({path:?}) returned None"));
862            assert_eq!(got, expected_content, "StubVfs content mismatch for {path}");
863            assert!(
864                vfs.is_stub_file(path),
865                "StubVfs::is_stub_file({path:?}) returned false"
866            );
867        }
868    }
869
870    #[test]
871    fn stub_vfs_rejects_user_file_paths() {
872        let vfs = StubVfs::new();
873        assert!(!vfs.is_stub_file("/tmp/user_code.php"));
874        assert!(!vfs.is_stub_file("src/MyClass.php"));
875        assert!(!vfs.is_stub_file(""));
876    }
877
878    #[test]
879    fn symbol_to_file_paths_are_resolvable_via_stub_vfs() {
880        let mut db = MirDbStorage::default();
881        db.init_workspace_revision();
882        load_stubs(&mut db);
883        let vfs = StubVfs::new();
884
885        let functions = crate::db::workspace_functions(&db);
886        for symbol in functions.iter() {
887            let Some(path) = db.symbol_defining_file(symbol.as_ref()) else {
888                continue;
889            };
890            assert!(
891                vfs.get(path.as_ref()).is_some(),
892                "symbol '{}' points to '{}' which StubVfs cannot resolve — \
893                 go-to-definition would silently break for this symbol",
894                symbol,
895                path
896            );
897        }
898    }
899
900    #[test]
901    fn function_lookup_is_case_insensitive() {
902        // PHP function names are case-insensitive: `STRLEN($x)` must resolve
903        // to the same node as `strlen($x)`. Regression for users seeing
904        // `UndefinedFunction: Function Restore_Error_Handler() is not defined`
905        // on mixed-case calls of built-ins.
906        let cb = stubs_codebase();
907        assert!(function_exists(&cb, "strlen"));
908        assert!(function_exists(&cb, "STRLEN"));
909        assert!(function_exists(&cb, "StrLen"));
910        assert!(function_exists(&cb, "Restore_Error_Handler"));
911        assert!(function_exists(&cb, "RESTORE_ERROR_HANDLER"));
912    }
913
914    #[test]
915    fn class_lookup_is_case_insensitive() {
916        // PHP class names are case-insensitive: `new arrayobject()` must
917        // resolve to `ArrayObject`. Regression for `UndefinedClass` on
918        // lower- or upper-cased built-in class references.
919        let cb = stubs_codebase();
920        assert!(class_exists(&cb, "ArrayObject"));
921        assert!(class_exists(&cb, "arrayobject"));
922        assert!(class_exists(&cb, "ARRAYOBJECT"));
923        assert!(class_exists(&cb, "ArrayOBJECT"));
924    }
925
926    #[test]
927    fn constant_lookup_stays_case_sensitive() {
928        // PHP global constants ARE case-sensitive (except true/false/null).
929        // Make sure the case-insensitivity fix for functions/classes did not
930        // leak into constants.
931        let cb = stubs_codebase();
932        assert!(constant_exists(&cb, "PHP_INT_MAX"));
933        assert!(!constant_exists(&cb, "php_int_max"));
934        assert!(!constant_exists(&cb, "Php_Int_Max"));
935    }
936
937    #[test]
938    fn stdlib_symbols_are_loaded() {
939        let cb = stubs_codebase();
940
941        assert_fn(&cb, "bcadd");
942        assert_fn(&cb, "bcsub");
943        assert_fn(&cb, "bcmul");
944        assert_fn(&cb, "bcdiv");
945        assert_fn(&cb, "sodium_crypto_secretbox");
946        assert_fn(&cb, "sodium_randombytes_buf");
947
948        assert_cls(&cb, "SplObjectStorage");
949        assert_cls(&cb, "SplHeap");
950        assert_cls(&cb, "IteratorIterator");
951        assert_cls(&cb, "FilterIterator");
952        assert_cls(&cb, "LimitIterator");
953        assert_cls(&cb, "CallbackFilterIterator");
954        assert_cls(&cb, "RegexIterator");
955        assert_cls(&cb, "AppendIterator");
956        assert_cls(&cb, "GlobIterator");
957        assert_cls(&cb, "ReflectionObject");
958        assert_cls(&cb, "Attribute");
959
960        assert_iface(&cb, "SeekableIterator");
961        assert_iface(&cb, "SplObserver");
962        assert_iface(&cb, "SplSubject");
963
964        assert_const(&cb, "PHP_INT_MAX");
965        assert_const(&cb, "PHP_INT_MIN");
966        assert_const(&cb, "PHP_EOL");
967        assert_const(&cb, "SORT_REGULAR");
968        assert_const(&cb, "JSON_THROW_ON_ERROR");
969        assert_const(&cb, "FILTER_VALIDATE_EMAIL");
970        assert_const(&cb, "PREG_OFFSET_CAPTURE");
971        assert_const(&cb, "M_PI");
972        assert_const(&cb, "PASSWORD_DEFAULT");
973    }
974}