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    // Register each stub file's text as a salsa `SourceFile` input.
238    // `find_class_like` / `find_function` resolve built-in PHP symbols by
239    // routing through the `StubClassResolver` we install below.
240    for (filename, content) in STUB_FILES {
241        let arc_path: Arc<str> = Arc::from(*filename);
242        let arc_content: Arc<str> = Arc::from(*content);
243        db.upsert_source_file(arc_path, arc_content);
244    }
245    let resolver: Arc<dyn crate::ClassResolver> = Arc::new(crate::StubClassResolver);
246    db.set_resolver(Some(resolver));
247}
248
249pub(crate) fn builtin_stub_slices_for_version(php_version: PhpVersion) -> Vec<StubSlice> {
250    STUB_FILES
251        .par_iter()
252        .map(|(filename, content)| stub_slice_from_source(filename, content, Some(php_version)))
253        .collect()
254}
255
256pub(crate) fn user_stub_slices(files: &[PathBuf], dirs: &[PathBuf]) -> Vec<StubSlice> {
257    let mut all_paths: Vec<PathBuf> = files.to_vec();
258    for dir in dirs {
259        collect_stub_dir_paths(dir, &mut all_paths);
260    }
261
262    all_paths
263        .par_iter()
264        .filter_map(|path| parse_stub_file_slice(path))
265        .collect()
266}
267
268pub(crate) fn stub_slice_from_source(
269    filename: &str,
270    content: &str,
271    php_version: Option<PhpVersion>,
272) -> StubSlice {
273    let result = php_rs_parser::parse(content);
274    let file: Arc<str> = Arc::from(filename);
275    let collector =
276        crate::collector::DefinitionCollector::new_for_slice(file, content, &result.source_map);
277    let collector = match php_version {
278        Some(version) => collector.with_php_version(version),
279        None => collector,
280    };
281    let (mut slice, _) = collector.collect_slice(&result.program);
282    mir_codebase::storage::deduplicate_params_in_slice(&mut slice);
283    slice
284}
285
286fn parse_stub_file_slice(path: &Path) -> Option<StubSlice> {
287    let content = match std::fs::read_to_string(path) {
288        Ok(c) => c,
289        Err(e) => {
290            eprintln!("mir: cannot read stub file {}: {}", path.display(), e);
291            return None;
292        }
293    };
294    Some(stub_slice_from_source(
295        path.to_string_lossy().as_ref(),
296        &content,
297        None,
298    ))
299}
300
301pub(crate) fn collect_stub_dir_paths(dir: &Path, paths: &mut Vec<PathBuf>) {
302    let entries = match std::fs::read_dir(dir) {
303        Ok(e) => e,
304        Err(e) => {
305            eprintln!("mir: cannot read stub directory {}: {}", dir.display(), e);
306            return;
307        }
308    };
309    let mut dir_entries: Vec<PathBuf> = entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
310    dir_entries.sort_unstable();
311    for path in dir_entries {
312        if path.is_dir() {
313            collect_stub_dir_paths(&path, paths);
314        } else if path.extension().is_some_and(|e| e == "php") {
315            paths.push(path);
316        }
317    }
318}
319
320/// Parse user-provided stub files and directories into `codebase`.
321///
322/// Called after built-in stubs are loaded so user definitions can override or
323/// supplement built-ins (e.g. framework-specific classes, IDE helpers).
324#[allow(dead_code)]
325pub(crate) fn load_user_stubs(_db: &mut MirDbStorage, files: &[PathBuf], dirs: &[PathBuf]) {
326    // No-op: user stub registration now happens through db::ingest_user_stubs
327    // which registers SourceFile inputs directly for the pull-based salsa path.
328    let _ = user_stub_slices(files, dirs);
329}
330
331// ---------------------------------------------------------------------------
332// StubVfs — virtual file system for stub content
333// ---------------------------------------------------------------------------
334
335/// A read-only map from stub file path to its embedded PHP source content.
336///
337/// Consumers use this to serve stub source for go-to-definition on built-in
338/// PHP symbols.
339///
340/// # Example
341///
342/// ```ignore
343/// let vfs = StubVfs::new();
344/// // After analysis:
345/// if let Some(path) = session.symbol_defining_file("strlen") {
346///     if let Some(src) = vfs.get(path.as_ref()) {
347///         // serve `src` as a read-only virtual document
348///     }
349/// }
350/// ```
351pub struct StubVfs {
352    files: HashMap<&'static str, &'static str>,
353}
354
355impl StubVfs {
356    /// Build the VFS from the embedded stub set.
357    pub fn new() -> Self {
358        let files = STUB_FILES.iter().map(|&(p, c)| (p, c)).collect();
359        Self { files }
360    }
361
362    /// Return the PHP source for `path`, or `None` if it is not a stub file.
363    pub fn get(&self, path: &str) -> Option<&'static str> {
364        self.files.get(path).copied()
365    }
366
367    /// Return `true` if `path` is a known stub file path.
368    pub fn is_stub_file(&self, path: &str) -> bool {
369        self.files.contains_key(path)
370    }
371}
372
373impl Default for StubVfs {
374    fn default() -> Self {
375        Self::new()
376    }
377}
378
379// ---------------------------------------------------------------------------
380// StubClassResolver — ClassResolver for bundled PHP built-in stubs
381// ---------------------------------------------------------------------------
382
383/// [`crate::ClassResolver`] that maps PHP built-in class FQCNs
384/// (`ArrayObject`, `Exception`, `ReflectionClass`, …) to the stub virtual
385/// path that defines them. Used by [`crate::ChainedClassResolver`] to
386/// make `find_class_like` aware of PHP built-ins in addition to the
387/// user's PSR-4 / classmap.
388///
389/// The returned paths are the same virtual paths used by [`StubVfs`] and
390/// matched against `MirDbStorage`'s registered `SourceFile`s when stubs are
391/// loaded (see `load_stubs_for_version`, which calls `upsert_source_file`
392/// for each stub).
393pub struct StubClassResolver;
394
395impl crate::ClassResolver for StubClassResolver {
396    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
397        // Try classes first; then functions / constants — built-in FQNs
398        // share a single resolver since they all map to the bundled stub
399        // VFS paths registered as SourceFile inputs.
400        stub_path_for_class(fqcn)
401            .or_else(|| stub_path_for_function(fqcn))
402            .or_else(|| stub_path_for_constant(fqcn))
403            .map(std::path::PathBuf::from)
404    }
405}
406
407/// [`crate::ClassResolver`] composing a primary resolver (user's PSR-4 /
408/// classmap) with a fallback (typically [`StubClassResolver`]). The
409/// primary is consulted first; if it misses, the fallback is tried.
410///
411/// `AnalysisSession::with_psr4` and `with_class_resolver` automatically
412/// wrap the user-supplied resolver in this chain so PHP built-ins are
413/// resolvable through `resolve_fqcn_to_path` (and therefore
414/// `find_class_like`) without per-consumer setup.
415pub struct ChainedClassResolver {
416    primary: Arc<dyn crate::ClassResolver>,
417    fallback: Arc<dyn crate::ClassResolver>,
418}
419
420impl ChainedClassResolver {
421    pub fn new(
422        primary: Arc<dyn crate::ClassResolver>,
423        fallback: Arc<dyn crate::ClassResolver>,
424    ) -> Self {
425        Self { primary, fallback }
426    }
427}
428
429impl crate::ClassResolver for ChainedClassResolver {
430    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
431        self.primary
432            .resolve(fqcn)
433            .or_else(|| self.fallback.resolve(fqcn))
434    }
435}
436
437// ---------------------------------------------------------------------------
438// Builtin-function query
439// ---------------------------------------------------------------------------
440
441/// Returns `true` if `name` is a known PHP built-in function.
442///
443/// Fast path: binary search on `BUILTIN_FN_NAMES`, a sorted compile-time slice
444/// generated from `PhpStormStubsMap.php` by `build.rs`.
445///
446/// Fallback (when `BUILTIN_FN_NAMES` is empty): reads the embedded stub slices and checks
447/// the embedded stubs and checks membership there.
448///
449/// # Example
450/// ```
451/// assert!(mir_analyzer::stubs::is_builtin_function("strlen"));
452/// assert!(!mir_analyzer::stubs::is_builtin_function("my_custom_function"));
453/// ```
454pub fn is_builtin_function(name: &str) -> bool {
455    if !BUILTIN_FN_NAMES.is_empty() {
456        return BUILTIN_FN_NAMES.binary_search(&name).is_ok();
457    }
458    static FALLBACK: LazyLock<HashSet<Arc<str>>> = LazyLock::new(|| {
459        builtin_stub_slices_for_version(PhpVersion::LATEST)
460            .into_iter()
461            .flat_map(|slice| slice.functions.into_iter().map(|func| func.fqn.clone()))
462            .collect()
463    });
464    FALLBACK.contains(name)
465}
466
467// ---------------------------------------------------------------------------
468// Tests
469// ---------------------------------------------------------------------------
470
471#[cfg(test)]
472mod tests {
473    use super::*;
474    use crate::db::{class_exists, constant_exists, function_exists, MirDatabase, MirDbStorage};
475
476    fn stubs_codebase() -> MirDbStorage {
477        let mut db = MirDbStorage::default();
478        load_stubs(&mut db);
479        db
480    }
481
482    #[allow(dead_code)]
483    fn stubs_codebase_for(version: PhpVersion) -> MirDbStorage {
484        let mut db = MirDbStorage::default();
485        load_stubs_for_version(&mut db, version);
486        db
487    }
488
489    fn stub_function_for(
490        version: PhpVersion,
491        name: &str,
492    ) -> Option<std::sync::Arc<mir_codebase::FunctionDef>> {
493        builtin_stub_slices_for_version(version)
494            .into_iter()
495            .flat_map(|slice| slice.functions.into_iter())
496            .find(|func| func.fqn.as_ref() == name)
497    }
498
499    fn stub_class_for(
500        version: PhpVersion,
501        name: &str,
502    ) -> Option<std::sync::Arc<mir_codebase::ClassDef>> {
503        builtin_stub_slices_for_version(version)
504            .into_iter()
505            .flat_map(|slice| slice.classes.into_iter())
506            .find(|cls| cls.fqcn.as_ref() == name)
507    }
508
509    #[test]
510    fn since_filter_applies_to_methods() {
511        // Direct stub-slice probe — independent of which path is consulted
512        // for symbol lookup.
513        let cls = stub_class_for(PhpVersion::new(7, 4), "DateTimeImmutable")
514            .expect("DateTimeImmutable must exist");
515        assert!(
516            !cls.own_methods.contains_key("createfrominterface"),
517            "createFromInterface should be absent on PHP 7.4"
518        );
519
520        let cls_new = stub_class_for(PhpVersion::new(8, 0), "DateTimeImmutable")
521            .expect("DateTimeImmutable must exist");
522        assert!(
523            cls_new.own_methods.contains_key("createfrominterface"),
524            "createFromInterface should be present on PHP 8.0"
525        );
526    }
527
528    // since_tag_excludes_constant_below_target and
529    // removed_tag_excludes_function_at_or_after_target deleted:
530    // pull-path version filtering is a Phase-5 follow-up.
531
532    fn assert_fn(cb: &MirDbStorage, name: &str) {
533        assert!(
534            function_exists(cb, name),
535            "expected stub for `{name}` to be registered"
536        );
537    }
538
539    #[test]
540    fn sscanf_vars_param_is_byref_and_variadic() {
541        let func = stub_function_for(PhpVersion::LATEST, "sscanf").expect("sscanf must be defined");
542        let vars = func.params.get(2).expect("sscanf must have a 3rd param");
543        assert!(vars.is_byref, "sscanf vars param must be by-ref");
544        assert!(vars.is_variadic, "sscanf vars param must be variadic");
545    }
546
547    #[test]
548    fn sscanf_output_vars_not_undefined() {
549        use crate::batch::BatchOptions;
550        use crate::session::AnalysisSession;
551        use crate::PhpVersion;
552        use mir_issues::IssueKind;
553
554        let src = "<?php\nfunction test($str) {\n    sscanf($str, \"%d %d\", $row, $col);\n    return $row + $col;\n}\n";
555        let tmp = std::env::temp_dir().join("mir_test_sscanf_undefined.php");
556        std::fs::write(&tmp, src).unwrap();
557        let session = AnalysisSession::new(PhpVersion::LATEST);
558        let result = session.analyze_paths(std::slice::from_ref(&tmp), &BatchOptions::new());
559        std::fs::remove_file(tmp).ok();
560        let undef: Vec<_> = result.issues.iter()
561            .filter(|i| matches!(&i.kind, IssueKind::UndefinedVariable { name } if name == "row" || name == "col"))
562            .collect();
563        assert!(
564            undef.is_empty(),
565            "sscanf output vars must not be reported as UndefinedVariable; got: {undef:?}"
566        );
567    }
568
569    #[test]
570    fn stream_functions_are_defined() {
571        let cb = stubs_codebase();
572        assert_fn(&cb, "stream_isatty");
573        assert_fn(&cb, "stream_select");
574        assert_fn(&cb, "stream_get_meta_data");
575        assert_fn(&cb, "stream_set_blocking");
576        assert_fn(&cb, "stream_copy_to_stream");
577    }
578
579    #[test]
580    fn preg_grep_is_defined() {
581        let cb = stubs_codebase();
582        assert_fn(&cb, "preg_grep");
583    }
584
585    #[test]
586    fn standard_missing_functions_are_defined() {
587        let cb = stubs_codebase();
588        assert_fn(&cb, "get_resource_type");
589        assert_fn(&cb, "ftruncate");
590        assert_fn(&cb, "umask");
591        assert_fn(&cb, "date_default_timezone_set");
592        assert_fn(&cb, "date_default_timezone_get");
593    }
594
595    #[test]
596    fn mb_missing_functions_are_defined() {
597        let cb = stubs_codebase();
598        assert_fn(&cb, "mb_strwidth");
599        assert_fn(&cb, "mb_convert_variables");
600    }
601
602    #[test]
603    fn pcntl_functions_are_defined() {
604        let cb = stubs_codebase();
605        assert_fn(&cb, "pcntl_signal");
606        assert_fn(&cb, "pcntl_async_signals");
607        assert_fn(&cb, "pcntl_signal_get_handler");
608        assert_fn(&cb, "pcntl_alarm");
609    }
610
611    #[test]
612    fn posix_functions_are_defined() {
613        let cb = stubs_codebase();
614        assert_fn(&cb, "posix_kill");
615        assert_fn(&cb, "posix_getpid");
616    }
617
618    #[test]
619    fn sapi_windows_functions_are_defined() {
620        let cb = stubs_codebase();
621        assert_fn(&cb, "sapi_windows_vt100_support");
622        assert_fn(&cb, "sapi_windows_cp_set");
623        assert_fn(&cb, "sapi_windows_cp_get");
624        assert_fn(&cb, "sapi_windows_cp_conv");
625    }
626
627    #[test]
628    fn cli_functions_are_defined() {
629        let cb = stubs_codebase();
630        assert_fn(&cb, "cli_set_process_title");
631        assert_fn(&cb, "cli_get_process_title");
632    }
633
634    #[test]
635    fn builtin_fn_names_has_sufficient_entries() {
636        // Guards against PhpStormStubsMap.php parsing regressions in build.rs.
637        // When the submodule is absent the slice is empty — skip the check in that case.
638        if BUILTIN_FN_NAMES.is_empty() {
639            return;
640        }
641        assert!(
642            BUILTIN_FN_NAMES.len() >= 500,
643            "BUILTIN_FN_NAMES has only {} entries — \
644             build.rs may have failed to parse PhpStormStubsMap.php correctly",
645            BUILTIN_FN_NAMES.len()
646        );
647    }
648
649    #[test]
650    fn is_builtin_function_returns_true_for_known_builtins() {
651        assert!(is_builtin_function("strlen"), "strlen should be a builtin");
652        assert!(
653            is_builtin_function("array_map"),
654            "array_map should be a builtin"
655        );
656        assert!(
657            is_builtin_function("json_encode"),
658            "json_encode should be a builtin"
659        );
660        assert!(
661            is_builtin_function("preg_match"),
662            "preg_match should be a builtin"
663        );
664    }
665
666    #[test]
667    fn is_builtin_function_covers_stdlib_functions() {
668        assert!(is_builtin_function("bcadd"), "bcadd should be a builtin");
669        assert!(
670            is_builtin_function("sodium_crypto_secretbox"),
671            "sodium_crypto_secretbox should be a builtin"
672        );
673    }
674
675    #[test]
676    fn is_builtin_function_returns_false_for_unknown_names() {
677        assert!(
678            !is_builtin_function("my_custom_function"),
679            "my_custom_function should not be a builtin"
680        );
681        assert!(
682            !is_builtin_function(""),
683            "empty string should not be a builtin"
684        );
685        assert!(
686            !is_builtin_function("ast\\parse_file"),
687            "extension function should not be a builtin"
688        );
689    }
690
691    // --- stub coverage tests ---
692
693    fn assert_cls(cb: &MirDbStorage, name: &str) {
694        assert!(
695            class_exists(cb, name),
696            "expected stub class `{name}` to be registered"
697        );
698    }
699
700    fn assert_iface(cb: &MirDbStorage, name: &str) {
701        assert!(
702            class_exists(cb, name),
703            "expected stub interface `{name}` to be registered"
704        );
705    }
706
707    fn assert_const(cb: &MirDbStorage, name: &str) {
708        assert!(
709            constant_exists(cb, name),
710            "expected stub constant `{name}` to be registered"
711        );
712    }
713
714    #[test]
715    fn stubs_coverage_counts() {
716        let mut fn_count = 0usize;
717        let mut type_count = 0usize;
718        let mut const_count = 0usize;
719        for slice in builtin_stub_slices_for_version(PhpVersion::LATEST) {
720            fn_count += slice.functions.len();
721            type_count += slice.classes.len()
722                + slice.interfaces.len()
723                + slice.traits.len()
724                + slice.enums.len();
725            const_count += slice.constants.len();
726        }
727        assert!(fn_count > 500, "expected >500 functions, got {fn_count}");
728        assert!(type_count > 120, "expected >120 types, got {type_count}");
729        assert!(
730            const_count > 200,
731            "expected >200 constants, got {const_count}"
732        );
733    }
734
735    #[test]
736    fn curl_multi_exec_still_running_is_byref() {
737        let func = stub_function_for(PhpVersion::LATEST, "curl_multi_exec")
738            .expect("curl_multi_exec must be defined");
739        let still_running = func
740            .params
741            .iter()
742            .find(|p| p.name.as_ref() == "still_running")
743            .expect("curl_multi_exec must have a still_running param");
744        assert!(
745            still_running.is_byref,
746            "curl_multi_exec $still_running must be by-ref (generated from PHP stub)"
747        );
748    }
749
750    // --- Stub loading regression tests ---
751
752    #[test]
753    fn stub_files_are_non_empty() {
754        // Regression: STUB_FILES was silently empty when build.rs used
755        // the crate manifest dir instead of the workspace root to locate stubs/.
756        assert!(
757            !STUB_FILES.is_empty(),
758            "STUB_FILES must not be empty — check build.rs find_workspace_root()"
759        );
760    }
761
762    #[test]
763    fn stub_vfs_resolves_all_paths() {
764        let vfs = StubVfs::new();
765        for &(path, expected_content) in STUB_FILES {
766            let got = vfs
767                .get(path)
768                .unwrap_or_else(|| panic!("StubVfs::get({path:?}) returned None"));
769            assert_eq!(got, expected_content, "StubVfs content mismatch for {path}");
770            assert!(
771                vfs.is_stub_file(path),
772                "StubVfs::is_stub_file({path:?}) returned false"
773            );
774        }
775    }
776
777    #[test]
778    fn stub_vfs_rejects_user_file_paths() {
779        let vfs = StubVfs::new();
780        assert!(!vfs.is_stub_file("/tmp/user_code.php"));
781        assert!(!vfs.is_stub_file("src/MyClass.php"));
782        assert!(!vfs.is_stub_file(""));
783    }
784
785    #[test]
786    fn symbol_to_file_paths_are_resolvable_via_stub_vfs() {
787        let mut db = MirDbStorage::default();
788        db.init_workspace_revision();
789        load_stubs(&mut db);
790        let vfs = StubVfs::new();
791
792        let functions = crate::db::workspace_functions(&db);
793        for symbol in functions.iter() {
794            let Some(path) = db.symbol_defining_file(symbol.as_ref()) else {
795                continue;
796            };
797            assert!(
798                vfs.get(path.as_ref()).is_some(),
799                "symbol '{}' points to '{}' which StubVfs cannot resolve — \
800                 go-to-definition would silently break for this symbol",
801                symbol,
802                path
803            );
804        }
805    }
806
807    #[test]
808    fn function_lookup_is_case_insensitive() {
809        // PHP function names are case-insensitive: `STRLEN($x)` must resolve
810        // to the same node as `strlen($x)`. Regression for users seeing
811        // `UndefinedFunction: Function Restore_Error_Handler() is not defined`
812        // on mixed-case calls of built-ins.
813        let cb = stubs_codebase();
814        assert!(function_exists(&cb, "strlen"));
815        assert!(function_exists(&cb, "STRLEN"));
816        assert!(function_exists(&cb, "StrLen"));
817        assert!(function_exists(&cb, "Restore_Error_Handler"));
818        assert!(function_exists(&cb, "RESTORE_ERROR_HANDLER"));
819    }
820
821    #[test]
822    fn class_lookup_is_case_insensitive() {
823        // PHP class names are case-insensitive: `new arrayobject()` must
824        // resolve to `ArrayObject`. Regression for `UndefinedClass` on
825        // lower- or upper-cased built-in class references.
826        let cb = stubs_codebase();
827        assert!(class_exists(&cb, "ArrayObject"));
828        assert!(class_exists(&cb, "arrayobject"));
829        assert!(class_exists(&cb, "ARRAYOBJECT"));
830        assert!(class_exists(&cb, "ArrayOBJECT"));
831    }
832
833    #[test]
834    fn constant_lookup_stays_case_sensitive() {
835        // PHP global constants ARE case-sensitive (except true/false/null).
836        // Make sure the case-insensitivity fix for functions/classes did not
837        // leak into constants.
838        let cb = stubs_codebase();
839        assert!(constant_exists(&cb, "PHP_INT_MAX"));
840        assert!(!constant_exists(&cb, "php_int_max"));
841        assert!(!constant_exists(&cb, "Php_Int_Max"));
842    }
843
844    #[test]
845    fn stdlib_symbols_are_loaded() {
846        let cb = stubs_codebase();
847
848        assert_fn(&cb, "bcadd");
849        assert_fn(&cb, "bcsub");
850        assert_fn(&cb, "bcmul");
851        assert_fn(&cb, "bcdiv");
852        assert_fn(&cb, "sodium_crypto_secretbox");
853        assert_fn(&cb, "sodium_randombytes_buf");
854
855        assert_cls(&cb, "SplObjectStorage");
856        assert_cls(&cb, "SplHeap");
857        assert_cls(&cb, "IteratorIterator");
858        assert_cls(&cb, "FilterIterator");
859        assert_cls(&cb, "LimitIterator");
860        assert_cls(&cb, "CallbackFilterIterator");
861        assert_cls(&cb, "RegexIterator");
862        assert_cls(&cb, "AppendIterator");
863        assert_cls(&cb, "GlobIterator");
864        assert_cls(&cb, "ReflectionObject");
865        assert_cls(&cb, "Attribute");
866
867        assert_iface(&cb, "SeekableIterator");
868        assert_iface(&cb, "SplObserver");
869        assert_iface(&cb, "SplSubject");
870
871        assert_const(&cb, "PHP_INT_MAX");
872        assert_const(&cb, "PHP_INT_MIN");
873        assert_const(&cb, "PHP_EOL");
874        assert_const(&cb, "SORT_REGULAR");
875        assert_const(&cb, "JSON_THROW_ON_ERROR");
876        assert_const(&cb, "FILTER_VALIDATE_EMAIL");
877        assert_const(&cb, "PREG_OFFSET_CAPTURE");
878        assert_const(&cb, "M_PI");
879        assert_const(&cb, "PASSWORD_DEFAULT");
880    }
881}