1use 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
31include!(concat!(env!("OUT_DIR"), "/stub_files.rs"));
33
34include!(concat!(env!("OUT_DIR"), "/phpstorm_builtin_fns.rs"));
37
38pub(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
47pub(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
58pub 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
75pub(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
85pub(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 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
143pub(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
161struct 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
208pub fn stub_files() -> &'static [(&'static str, &'static str)] {
216 STUB_FILES
217}
218
219#[allow(dead_code)]
227pub(crate) fn load_stubs(db: &mut MirDbStorage) {
228 load_stubs_for_version(db, PhpVersion::LATEST);
229}
230
231#[allow(dead_code)]
236pub(crate) fn load_stubs_for_version(db: &mut MirDbStorage, php_version: PhpVersion) {
237 db.set_php_version(Arc::from(php_version.to_string().as_str()));
240 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
323pub(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 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 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#[allow(dead_code)]
358pub(crate) fn load_user_stubs(_db: &mut MirDbStorage, files: &[PathBuf], dirs: &[PathBuf]) {
359 let _ = user_stub_slices(files, dirs);
362}
363
364pub struct StubVfs {
385 files: HashMap<&'static str, &'static str>,
386}
387
388impl StubVfs {
389 pub fn new() -> Self {
391 let files = STUB_FILES.iter().map(|&(p, c)| (p, c)).collect();
392 Self { files }
393 }
394
395 pub fn get(&self, path: &str) -> Option<&'static str> {
397 self.files.get(path).copied()
398 }
399
400 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
412pub struct StubClassResolver;
427
428impl crate::ClassResolver for StubClassResolver {
429 fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
430 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
440pub 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
470pub 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#[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 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 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 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 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 #[test]
846 fn stub_files_are_non_empty() {
847 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 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 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 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}