1use std::path::{Path, PathBuf};
15use std::sync::Arc;
16
17use rayon::prelude::*;
18use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
19
20use mir_issues::Issue;
21use mir_types::{Atomic, Type};
22
23use crate::body_analysis::BodyAnalyzer;
24use crate::cache::hash_content;
25use crate::db::{
26 collect_file_definitions, FileDefinitions, MirDatabase, MirDbStorage, RefLoc, SourceFile,
27};
28use crate::php_version::PhpVersion;
29use crate::session::AnalysisSession;
30use crate::stub_cache::{hash_source, prepare_for_ingest};
31
32pub fn dead_code_issue_kinds() -> &'static [&'static str] {
39 &[
40 "UnusedMethod",
41 "UnusedProperty",
42 "UnusedFunction",
43 "UnusedClass",
44 ]
45}
46
47#[derive(Clone, Default)]
53pub struct BatchOptions {
54 pub suppressed_issue_kinds: HashSet<String>,
59 pub on_file_done: Option<Arc<dyn Fn() + Send + Sync>>,
61 pub php_version_override: Option<PhpVersion>,
64 pub skip_symbols: bool,
71}
72
73impl BatchOptions {
74 pub fn new() -> Self {
75 Self::default()
76 }
77
78 pub fn with_suppressed<I, S>(mut self, kinds: I) -> Self
79 where
80 I: IntoIterator<Item = S>,
81 S: Into<String>,
82 {
83 self.suppressed_issue_kinds = kinds.into_iter().map(Into::into).collect();
84 self
85 }
86
87 pub fn with_progress_callback(mut self, callback: Arc<dyn Fn() + Send + Sync>) -> Self {
88 self.on_file_done = Some(callback);
89 self
90 }
91
92 pub fn with_php_version(mut self, version: PhpVersion) -> Self {
93 self.php_version_override = Some(version);
94 self
95 }
96
97 pub fn without_symbols(mut self) -> Self {
101 self.skip_symbols = true;
102 self
103 }
104
105 fn should_run_dead_code(&self) -> bool {
108 dead_code_issue_kinds()
109 .iter()
110 .any(|k| !self.suppressed_issue_kinds.contains(*k))
111 }
112
113 fn apply(&self, issues: &mut Vec<Issue>) {
116 if self.suppressed_issue_kinds.is_empty() {
117 return;
118 }
119 issues.retain(|i| !self.suppressed_issue_kinds.contains(i.kind.name()));
120 }
121}
122
123struct ParsedProjectFile {
124 file: Arc<str>,
125 source: Arc<str>,
126 parsed: php_rs_parser::ParseResult,
127}
128
129impl ParsedProjectFile {
130 fn new(file: Arc<str>, source: Arc<str>) -> Self {
131 let parsed = php_rs_parser::parse(source.as_ref());
132 Self {
133 file,
134 source,
135 parsed,
136 }
137 }
138
139 fn source(&self) -> &str {
140 self.source.as_ref()
141 }
142
143 fn source_map(&self) -> &php_rs_parser::source_map::SourceMap {
144 &self.parsed.source_map
145 }
146
147 fn errors(&self) -> &[php_rs_parser::diagnostics::ParseError] {
148 &self.parsed.errors
149 }
150
151 fn owned(&self) -> &php_ast::owned::Program {
152 &self.parsed.program
153 }
154}
155
156impl AnalysisSession {
157 #[doc(hidden)]
160 pub fn stub_cache_stats(&self) -> (u64, u64) {
161 match self.db.stub_cache.as_deref() {
162 Some(c) => (c.hits(), c.misses()),
163 None => (0, 0),
164 }
165 }
166
167 fn batch_php_version(&self, opts: &BatchOptions) -> PhpVersion {
168 opts.php_version_override.unwrap_or(self.php_version)
169 }
170
171 fn apply_suppressions_and_emit_unused(
189 &self,
190 issues: &mut Vec<Issue>,
191 analyzed_files: &[Arc<str>],
192 ) {
193 use crate::suppression::SuppressionMap;
194 let db = self.snapshot_db();
195 let mut cache: HashMap<Arc<str>, Option<SuppressionMap>> = HashMap::default();
196 for issue in issues.iter_mut() {
197 if issue.suppressed {
198 continue;
199 }
200 let map = cache.entry(issue.location.file.clone()).or_insert_with(|| {
201 db.lookup_source_file(&issue.location.file)
202 .map(|sf| SuppressionMap::from_source(&sf.text(&db)))
203 });
204 if let Some(map) = map.as_ref() {
205 if map.is_suppressed(issue.location.line, issue.kind.name(), issue.kind.code()) {
206 issue.suppressed = true;
207 }
208 }
209 }
210 for file in analyzed_files {
214 cache.entry(file.clone()).or_insert_with(|| {
215 db.lookup_source_file(file)
216 .map(|sf| SuppressionMap::from_source(&sf.text(&db)))
217 });
218 }
219 let files: Vec<Arc<str>> = cache
221 .iter()
222 .filter_map(|(f, m)| m.as_ref().map(|_| f.clone()))
223 .collect();
224 let mut new_issues: Vec<Issue> = Vec::new();
225 for file in files {
226 if let Some(Some(map)) = cache.get(&file) {
227 if map.named_suppressions.is_empty() {
228 continue;
229 }
230 let file_issues: Vec<Issue> = issues
231 .iter()
232 .filter(|i| i.location.file == file)
233 .cloned()
234 .collect();
235 let pre_suppressed: Vec<&Issue> =
240 file_issues.iter().filter(|i| i.suppressed).collect();
241 let unused = map.unused_named(&file_issues, &pre_suppressed);
246 for (line, kind) in unused {
247 let loc = mir_types::Location::new(file.clone(), line, line, 0, 0);
248 let mut issue = Issue::new(mir_issues::IssueKind::UnusedSuppress { kind }, loc);
249 if map.is_suppressed(line, issue.kind.name(), issue.kind.code()) {
250 issue.suppressed = true;
251 }
252 new_issues.push(issue);
253 }
254 }
255 }
256 issues.extend(new_issues);
257 }
258
259 fn type_exists(&self, fqcn: &str) -> bool {
260 let db = self.snapshot_db();
261 crate::db::class_exists(&db, fqcn)
262 }
263
264 fn collect_and_ingest_source(
265 &self,
266 file: Arc<str>,
267 src: &str,
268 php_version: PhpVersion,
269 ) -> FileDefinitions {
270 self.db.collect_and_ingest_file(file, src, php_version)
271 }
272
273 fn refresh_workspace_index(&self) {
281 let mut guard = self.db.salsa.write();
282 guard.rebuild_workspace_symbol_index();
283 }
284
285 fn load_batch_stubs(&self, php_version: PhpVersion) {
289 {
292 let version_str = Arc::from(php_version.to_string().as_str());
293 self.db.salsa.write().set_php_version(version_str);
294 }
295
296 let paths: Vec<&'static str> = crate::stubs::stub_files().iter().map(|&(p, _)| p).collect();
298 self.db.ingest_stub_paths(&paths, php_version);
299
300 self.db
302 .ingest_user_stubs(&self.user_stub_files, &self.user_stub_dirs);
303
304 let mut guard = self.db.salsa.write();
307 if guard.current_resolver().is_none() {
308 let resolver: Arc<dyn crate::ClassResolver> = Arc::new(crate::StubClassResolver);
309 guard.set_resolver(Some(resolver));
310 }
311 }
312}
313
314mod lazy;
315mod run;
316
317pub fn analyze_source(source: &str) -> AnalysisResult {
321 let php_version = PhpVersion::LATEST;
322 let file: Arc<str> = Arc::from("<source>");
323 let mut db = MirDbStorage::default();
324 db.set_php_version(Arc::from(php_version.to_string().as_str()));
325 crate::stubs::load_stubs_for_version(&mut db, php_version);
326 let salsa_file = db.upsert_source_file(file.clone(), Arc::from(source));
332 let file_defs = collect_file_definitions(&db, salsa_file);
333 let suppressions = crate::suppression::SuppressionMap::from_source(source);
334 let mut all_issues = Arc::unwrap_or_clone(file_defs.issues);
335 if all_issues.iter().any(|issue| {
336 matches!(issue.kind, mir_issues::IssueKind::ParseError { .. })
337 && issue.severity == mir_issues::Severity::Error
338 }) {
339 mark_suppressed(&mut all_issues, &suppressions);
340 return AnalysisResult::build(all_issues, rustc_hash::FxHashMap::default(), Vec::new());
341 }
342 let mut type_envs = rustc_hash::FxHashMap::default();
343 let mut all_symbols = Vec::new();
344 let result = php_rs_parser::parse(source);
345
346 let driver = BodyAnalyzer::new(&db, php_version);
347 all_issues.extend(driver.analyze_bodies_typed(
348 &result.program,
349 file.clone(),
350 source,
351 &result.source_map,
352 &mut type_envs,
353 &mut all_symbols,
354 ));
355 mark_suppressed(&mut all_issues, &suppressions);
356 emit_unused_suppressions(&mut all_issues, &suppressions, &file);
357 AnalysisResult::build(all_issues, type_envs, all_symbols)
358}
359
360fn mark_suppressed(issues: &mut [Issue], suppressions: &crate::suppression::SuppressionMap) {
364 if suppressions.is_empty() {
365 return;
366 }
367 for issue in issues.iter_mut() {
368 if !issue.suppressed
369 && suppressions.is_suppressed(issue.location.line, issue.kind.name(), issue.kind.code())
370 {
371 issue.suppressed = true;
372 }
373 }
374}
375
376fn emit_unused_suppressions(
380 all_issues: &mut Vec<Issue>,
381 suppressions: &crate::suppression::SuppressionMap,
382 file: &std::sync::Arc<str>,
383) {
384 let pre_suppressed_cloned: Vec<Issue> = all_issues
385 .iter()
386 .filter(|i| i.suppressed)
387 .cloned()
388 .collect();
389 let pre_suppressed: Vec<&Issue> = pre_suppressed_cloned.iter().collect();
390 let unused = suppressions.unused_named(all_issues, &pre_suppressed);
391 for (line, kind) in unused {
392 let loc = mir_types::Location::new(file.clone(), line, line, 0, 0);
393 let mut issue = Issue::new(mir_issues::IssueKind::UnusedSuppress { kind }, loc);
394 if suppressions.is_suppressed(line, issue.kind.name(), issue.kind.code()) {
395 issue.suppressed = true;
396 }
397 all_issues.push(issue);
398 }
399}
400
401pub fn discover_files(root: &Path) -> Vec<PathBuf> {
403 if root.is_file() {
404 return vec![root.to_path_buf()];
405 }
406 let mut files = Vec::new();
407 collect_php_files(root, &mut files);
408 files
409}
410
411pub(crate) fn collect_php_files(dir: &Path, out: &mut Vec<PathBuf>) {
412 if let Ok(entries) = std::fs::read_dir(dir) {
413 for entry in entries.flatten() {
414 if entry.file_type().map(|ft| ft.is_symlink()).unwrap_or(false) {
415 continue;
416 }
417 let path = entry.path();
418 if path.is_dir() {
419 let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
420 if matches!(
421 name,
422 "vendor" | ".git" | "node_modules" | ".cache" | ".pnpm-store"
423 ) {
424 continue;
425 }
426 collect_php_files(&path, out);
427 } else if path.extension().and_then(|e| e.to_str()) == Some("php") {
428 out.push(path);
429 }
430 }
431 }
432}
433
434pub(crate) fn collect_class_referenced_fqcns(class: &crate::db::ClassLike, out: &mut Vec<String>) {
441 if let Some(p) = class.parent() {
442 out.push(p.to_string());
443 }
444 for i in class.interfaces() {
445 out.push(i.to_string());
446 }
447 for e in class.extends() {
448 out.push(e.to_string());
449 }
450 for t in class.class_traits() {
451 out.push(t.to_string());
452 }
453 for m in class.mixins() {
454 out.push(m.to_string());
455 }
456 for u in class.extends_type_args() {
457 collect_fqcns_in_union(u, out);
458 }
459 for (iface, args) in class.implements_type_args() {
460 out.push(iface.to_string());
461 for u in args {
462 collect_fqcns_in_union(u, out);
463 }
464 }
465 for (_, m) in class.own_methods().iter() {
466 for p in m.params.iter() {
467 if let Some(t) = &p.ty {
468 collect_fqcns_in_union(t, out);
469 }
470 }
471 if let Some(t) = &m.return_type {
472 collect_fqcns_in_union(t, out);
473 }
474 for thrown in m.throws.iter() {
475 out.push(thrown.to_string());
476 }
477 }
478 if let Some(props) = class.own_properties() {
479 for (_, p) in props.iter() {
480 if let Some(t) = &p.ty {
481 collect_fqcns_in_union(t, out);
482 }
483 }
484 }
485 for (_, c) in class.own_constants().iter() {
486 collect_fqcns_in_union(&c.ty, out);
487 }
488}
489
490pub(crate) fn collect_fqcns_in_union(u: &Type, out: &mut Vec<String>) {
491 for atom in u.types.iter() {
492 collect_fqcns_in_atomic(atom, out);
493 }
494}
495
496fn collect_fqcns_in_simple(t: &mir_types::compact::SimpleType, out: &mut Vec<String>) {
497 if let mir_types::compact::SimpleType::Complex(u) = t {
498 collect_fqcns_in_union(u, out);
499 }
500}
501
502pub(crate) fn collect_fqcns_in_atomic(a: &Atomic, out: &mut Vec<String>) {
503 match a {
504 Atomic::TNamedObject { fqcn, type_params } => {
505 out.push(fqcn.to_string());
506 for tp in type_params.iter() {
507 collect_fqcns_in_union(tp, out);
508 }
509 }
510 Atomic::TStaticObject { fqcn } | Atomic::TSelf { fqcn } | Atomic::TParent { fqcn } => {
511 out.push(fqcn.to_string());
512 }
513 Atomic::TLiteralEnumCase { enum_fqcn, .. } => {
514 out.push(enum_fqcn.to_string());
515 }
516 Atomic::TClassString(Some(s)) => {
517 out.push(s.to_string());
518 }
519 Atomic::TArray { key, value } | Atomic::TNonEmptyArray { key, value } => {
520 collect_fqcns_in_union(key, out);
521 collect_fqcns_in_union(value, out);
522 }
523 Atomic::TList { value } | Atomic::TNonEmptyList { value } => {
524 collect_fqcns_in_union(value, out);
525 }
526 Atomic::TKeyedArray { properties, .. } => {
527 for (_, kp) in properties.iter() {
528 collect_fqcns_in_union(&kp.ty, out);
529 }
530 }
531 Atomic::TClosure {
532 params,
533 return_type,
534 this_type,
535 } => {
536 for p in params {
537 if let Some(t) = &p.ty {
538 collect_fqcns_in_simple(t, out);
539 }
540 }
541 collect_fqcns_in_union(return_type, out);
542 if let Some(t) = this_type {
543 collect_fqcns_in_union(t, out);
544 }
545 }
546 Atomic::TCallable {
547 params,
548 return_type,
549 } => {
550 if let Some(ps) = params {
551 for p in ps {
552 if let Some(t) = &p.ty {
553 collect_fqcns_in_simple(t, out);
554 }
555 }
556 }
557 if let Some(rt) = return_type {
558 collect_fqcns_in_union(rt, out);
559 }
560 }
561 Atomic::TIntersection { parts } => {
562 for p in parts.iter() {
563 collect_fqcns_in_union(p, out);
564 }
565 }
566 Atomic::TConditional {
567 param_name: _,
568 subject,
569 if_true,
570 if_false,
571 } => {
572 collect_fqcns_in_union(subject, out);
573 collect_fqcns_in_union(if_true, out);
574 collect_fqcns_in_union(if_false, out);
575 }
576 Atomic::TTemplateParam { as_type, .. } => {
577 collect_fqcns_in_union(as_type, out);
578 }
579 _ => {}
580 }
581}
582
583fn build_reverse_deps(db: &dyn crate::db::MirDatabase) -> HashMap<String, HashSet<String>> {
584 let mut reverse: HashMap<String, HashSet<String>> = HashMap::default();
585
586 let mut add_edge = |symbol: &str, dependent_file: &str| {
587 if let Some(defining_file) = db.symbol_defining_file(symbol) {
588 let def = defining_file.as_ref().to_string();
589 if def != dependent_file {
590 reverse
591 .entry(def)
592 .or_default()
593 .insert(dependent_file.to_string());
594 }
595 }
596 };
597
598 for (file, imports) in db.file_import_snapshots() {
599 let file = file.as_ref().to_string();
600 for fqcn in imports.values() {
601 add_edge(fqcn.as_str(), &file);
602 }
603 }
604
605 let extract_named_objects = |union: &mir_types::Type| {
606 union
607 .types
608 .iter()
609 .filter_map(|atomic| match atomic {
610 mir_types::atomic::Atomic::TNamedObject { fqcn, .. } => Some(*fqcn),
611 _ => None,
612 })
613 .collect::<Vec<_>>()
614 };
615
616 for fqcn in crate::db::workspace_classes(db).iter() {
617 let here = crate::db::Fqcn::from_str(db, fqcn.as_ref());
618 let Some(class) = crate::db::find_class_like(db, here) else {
619 continue;
620 };
621 if class.is_interface() || class.is_trait() || class.is_enum() {
622 continue;
623 }
624 let Some(file) = db
625 .symbol_defining_file(fqcn.as_ref())
626 .map(|f| f.as_ref().to_string())
627 .or_else(|| class.location().map(|l| l.file.as_ref().to_string()))
628 else {
629 continue;
630 };
631
632 if let Some(parent) = class.parent() {
633 add_edge(parent.as_ref(), &file);
634 }
635 for iface in class.interfaces().iter() {
636 add_edge(iface.as_ref(), &file);
637 }
638 for tr in class.class_traits().iter() {
639 add_edge(tr.as_ref(), &file);
640 }
641 if let Some(props) = class.own_properties() {
642 for (_, p) in props.iter() {
643 if let Some(ty) = &p.ty {
644 for named in extract_named_objects(ty) {
645 add_edge(named.as_ref(), &file);
646 }
647 }
648 }
649 }
650 for (_, method) in class.own_methods().iter() {
651 for param in method.params.iter() {
652 if let Some(ty) = ¶m.ty {
653 for named in extract_named_objects(ty.as_ref()) {
654 add_edge(named.as_ref(), &file);
655 }
656 }
657 }
658 if let Some(rt) = method.return_type.as_deref() {
659 for named in extract_named_objects(rt) {
660 add_edge(named.as_ref(), &file);
661 }
662 }
663 }
664 }
665
666 for fqn in crate::db::workspace_functions(db).iter() {
667 let here = crate::db::Fqcn::from_str(db, fqn.as_ref());
668 let Some(f) = crate::db::find_function(db, here) else {
669 continue;
670 };
671 let Some(file) = db
672 .symbol_defining_file(fqn.as_ref())
673 .map(|f| f.as_ref().to_string())
674 .or_else(|| f.location.as_ref().map(|l| l.file.as_ref().to_string()))
675 else {
676 continue;
677 };
678
679 for param in f.params.iter() {
680 if let Some(ty) = ¶m.ty {
681 for named in extract_named_objects(ty.as_ref()) {
682 add_edge(named.as_ref(), &file);
683 }
684 }
685 }
686 if let Some(rt) = f.return_type.as_deref() {
687 for named in extract_named_objects(rt) {
688 add_edge(named.as_ref(), &file);
689 }
690 }
691 }
692
693 for (ref_file, symbol_key) in db.all_reference_location_pairs() {
694 let file_str = ref_file.as_ref().to_string();
695 let lookup: &str = match symbol_key.split_once("::") {
696 Some((class, _)) => class,
697 None => &symbol_key,
698 };
699 add_edge(lookup, &file_str);
700 }
701
702 reverse
703}
704
705fn extract_reference_locations(
706 db: &dyn crate::db::MirDatabase,
707 file: &Arc<str>,
708) -> Vec<(String, u32, u16, u16)> {
709 db.extract_file_reference_locations(file.as_ref())
710 .into_iter()
711 .map(|(sym, line, col_start, col_end)| (sym.to_string(), line, col_start, col_end))
712 .collect()
713}
714
715pub struct AnalysisResult {
716 pub issues: Vec<Issue>,
717 #[doc(hidden)]
718 pub type_envs: rustc_hash::FxHashMap<crate::type_env::ScopeId, crate::type_env::TypeEnv>,
719 pub symbols: Vec<crate::symbol::ResolvedSymbol>,
721 symbols_by_file: HashMap<Arc<str>, std::ops::Range<usize>>,
724}
725
726impl AnalysisResult {
727 fn build(
728 issues: Vec<Issue>,
729 type_envs: rustc_hash::FxHashMap<crate::type_env::ScopeId, crate::type_env::TypeEnv>,
730 mut symbols: Vec<crate::symbol::ResolvedSymbol>,
731 ) -> Self {
732 symbols.sort_unstable_by(|a, b| a.file.as_ref().cmp(b.file.as_ref()));
733 let mut symbols_by_file: HashMap<Arc<str>, std::ops::Range<usize>> = HashMap::default();
734 let mut i = 0;
735 while i < symbols.len() {
736 let file = Arc::clone(&symbols[i].file);
737 let start = i;
738 while i < symbols.len() && symbols[i].file == file {
739 i += 1;
740 }
741 symbols_by_file.insert(file, start..i);
742 }
743 Self {
744 issues,
745 type_envs,
746 symbols,
747 symbols_by_file,
748 }
749 }
750
751 pub fn error_count(&self) -> usize {
752 self.issues
753 .iter()
754 .filter(|i| i.severity == mir_issues::Severity::Error)
755 .count()
756 }
757
758 pub fn warning_count(&self) -> usize {
759 self.issues
760 .iter()
761 .filter(|i| i.severity == mir_issues::Severity::Warning)
762 .count()
763 }
764
765 pub fn issues_by_file(&self) -> HashMap<Arc<str>, Vec<&Issue>> {
766 let mut map: HashMap<Arc<str>, Vec<&Issue>> = HashMap::default();
767 for issue in &self.issues {
768 map.entry(issue.location.file.clone())
769 .or_default()
770 .push(issue);
771 }
772 map
773 }
774
775 pub fn count_by_severity(&self) -> Vec<(mir_issues::Severity, usize)> {
776 let mut counts: std::collections::BTreeMap<mir_issues::Severity, usize> =
777 std::collections::BTreeMap::new();
778 for issue in &self.issues {
779 *counts.entry(issue.severity).or_insert(0) += 1;
780 }
781 counts.into_iter().collect()
782 }
783
784 pub fn total_issue_count(&self) -> usize {
785 self.issues.len()
786 }
787
788 pub fn filter_issues<'a, F>(&'a self, predicate: F) -> impl Iterator<Item = &'a Issue>
789 where
790 F: Fn(&Issue) -> bool + 'a,
791 {
792 self.issues.iter().filter(move |i| predicate(i))
793 }
794
795 pub fn symbol_at(
796 &self,
797 file: &str,
798 byte_offset: u32,
799 ) -> Option<&crate::symbol::ResolvedSymbol> {
800 let range = self.symbols_by_file.get(file)?;
801 let symbols = &self.symbols[range.clone()];
802
803 if let Some(sym) = symbols
805 .iter()
806 .filter(|s| s.span.start <= byte_offset && byte_offset < s.span.end)
807 .min_by_key(|s| s.span.end - s.span.start)
808 {
809 return Some(sym);
810 }
811
812 symbols
818 .iter()
819 .filter(|s| {
820 s.expr_span
821 .is_some_and(|es| es.start <= byte_offset && byte_offset < es.end)
822 })
823 .min_by_key(|s| {
824 let es = s.expr_span.unwrap();
825 es.end - es.start
826 })
827 }
828}