mir_analyzer/file_analyzer.rs
1//! Per-file analysis entry point for incremental analysis.
2//!
3//! [`FileAnalyzer`] runs a **single** body-analysis pass against an
4//! [`AnalysisSession`] snapshot. In the eager-static-input model the workspace
5//! symbol index is built up front by the background indexer
6//! ([`AnalysisSession::index_batch`]), so `find_class_like` resolves vendor
7//! classes directly — there is no lazy-load / retry loop. The only on-demand
8//! work is [`AnalysisSession::priority_index_for_ast`], which faults in the
9//! open file's *direct* references if the background walk hasn't reached them
10//! yet, keeping warm-up free of transient false positives.
11//!
12//! For batch multi-file analysis, use [`BatchFileAnalyzer::analyze_batch`]
13//! which parallelizes analysis across multiple pre-parsed files.
14
15use std::sync::Arc;
16
17use mir_issues::Issue;
18use php_ast::owned::Program;
19use php_rs_parser::source_map::SourceMap;
20use rayon::prelude::*;
21
22use crate::body_analysis::BodyAnalyzer;
23use crate::db::MirDatabase;
24use crate::session::AnalysisSession;
25use crate::symbol::ResolvedSymbol;
26
27/// Result of a single-file analysis.
28pub struct FileAnalysis {
29 pub issues: Vec<Issue>,
30 pub symbols: Vec<ResolvedSymbol>,
31}
32
33impl FileAnalysis {
34 /// Return the innermost resolved symbol whose span contains `byte_offset`,
35 /// or `None` if no symbol was recorded at that position.
36 ///
37 /// Entry point for hover / go-to-definition flows: callers map
38 /// (line, column) → byte offset → resolved symbol, then look up the
39 /// symbol's definition via [`crate::AnalysisSession::definition_of`] or
40 /// type info via [`ResolvedSymbol::resolved_type`].
41 pub fn symbol_at(&self, byte_offset: u32) -> Option<&ResolvedSymbol> {
42 self.symbols
43 .iter()
44 .filter(|s| s.span.start <= byte_offset && byte_offset < s.span.end)
45 .min_by_key(|s| s.span.end - s.span.start)
46 }
47}
48
49/// Per-file body analysis analyzer bound to an [`AnalysisSession`]. Cheap to
50/// construct — typically held transiently per analysis call.
51pub struct FileAnalyzer<'a> {
52 session: &'a AnalysisSession,
53}
54
55impl<'a> FileAnalyzer<'a> {
56 pub fn new(session: &'a AnalysisSession) -> Self {
57 Self { session }
58 }
59
60 /// Run a single body-analysis pass against a frozen db snapshot.
61 ///
62 /// `priority_index_for_ast` runs first to fault in any of this file's
63 /// direct class references not yet reached by the background indexer; then
64 /// one snapshot is analyzed and its reference locations committed. The lock
65 /// is not held during analysis, so concurrent edits and reads proceed.
66 pub fn analyze(
67 &self,
68 file: Arc<str>,
69 source: &str,
70 program: &Program,
71 source_map: &SourceMap,
72 ) -> FileAnalysis {
73 crate::metrics::record_file_analysis();
74
75 // Priority-index the buffer's direct class references so any not yet
76 // reached by the background indexer resolve in this single pass (no
77 // transient false UndefinedClass during warm-up). Once indexing
78 // completes this is a no-op.
79 self.session
80 .prepare_ast_for_analysis(program, file.as_ref());
81
82 let _scope = crate::metrics::BodyAnalysisScope::new();
83
84 // Single pass against a frozen snapshot. With the eager-static-input
85 // model the workspace index is complete (or priority-indexed for this
86 // file's direct refs), so there are no body-analysis "misses" to fault
87 // in — no retry loop, no whole-file re-analysis.
88 let db = self.session.snapshot_db();
89 let driver = BodyAnalyzer::new(&db, self.session.php_version());
90 let (issues, symbols) = driver.analyze_bodies(program, file.clone(), source, source_map);
91 self.session
92 .commit_ref_locs_batch(db.take_pending_ref_locs());
93 FileAnalysis { issues, symbols }
94 }
95}
96
97/// Batch file analyzer for parallel multi-file analysis.
98///
99/// `BatchFileAnalyzer` processes pre-parsed files in parallel using rayon,
100/// making it efficient for analyzing many files at once (e.g., cold-start analysis).
101pub struct BatchFileAnalyzer<'a> {
102 session: &'a AnalysisSession,
103}
104
105/// A pre-parsed file ready for batch analysis.
106pub struct ParsedFile {
107 pub(crate) file: Arc<str>,
108 pub(crate) source: Arc<str>,
109 pub(crate) program: Program,
110 pub(crate) source_map: SourceMap,
111}
112
113impl ParsedFile {
114 /// File path this `ParsedFile` represents.
115 pub fn file(&self) -> &Arc<str> {
116 &self.file
117 }
118
119 /// Source text for this file.
120 pub fn source(&self) -> &Arc<str> {
121 &self.source
122 }
123
124 /// Create a `ParsedFile` from an owned program and source map.
125 pub fn new(file: Arc<str>, source: Arc<str>, program: Program, source_map: SourceMap) -> Self {
126 Self {
127 file,
128 source,
129 program,
130 source_map,
131 }
132 }
133}
134
135impl<'a> BatchFileAnalyzer<'a> {
136 pub fn new(session: &'a AnalysisSession) -> Self {
137 Self { session }
138 }
139
140 /// Analyze multiple pre-parsed files in parallel.
141 ///
142 /// Each rayon worker gets its own cloned database snapshot, so concurrent
143 /// analysis proceeds without lock contention on the session.
144 pub fn analyze_batch(&self, files: Vec<ParsedFile>) -> Vec<(Arc<str>, FileAnalysis)> {
145 // First pass: collect all ASTs and auto-discover stubs.
146 files.iter().for_each(|file| {
147 self.session.ensure_stubs_for_ast(&file.program);
148 });
149
150 // Second pass: analyze files in parallel.
151 // Each rayon worker gets its own database clone (Salsa is Send but !Sync).
152 let db = self.session.snapshot_db();
153 let results: Vec<(Arc<str>, FileAnalysis, Vec<crate::db::RefLoc>)> = files
154 .into_par_iter()
155 .map_with(db, |db, file| {
156 let driver = BodyAnalyzer::new(db as &dyn MirDatabase, self.session.php_version());
157 let (issues, symbols) = driver.analyze_bodies(
158 &file.program,
159 file.file.clone(),
160 &file.source,
161 &file.source_map,
162 );
163 let pending = db.take_pending_ref_locs();
164 let analysis = FileAnalysis { issues, symbols };
165 (file.file, analysis, pending)
166 })
167 .collect();
168 let mut all_ref_locs = Vec::new();
169 let mut out = Vec::with_capacity(results.len());
170 for (file, analysis, ref_locs) in results {
171 all_ref_locs.extend(ref_locs);
172 out.push((file, analysis));
173 }
174 self.session.commit_ref_locs_batch(all_ref_locs);
175 out
176 }
177}