Skip to main content

mir_analyzer/batch/
run.rs

1use super::*;
2
3impl AnalysisSession {
4    /// Run the full batch analysis pipeline on a set of file paths.
5    pub fn analyze_paths(&mut self, paths: &[PathBuf], opts: &BatchOptions) -> AnalysisResult {
6        let php_version = self.batch_php_version(opts);
7        let mut all_issues = Vec::new();
8        let _t0 = std::time::Instant::now();
9
10        // ---- Load PHP built-in stubs (before definition collection so user code can override)
11        self.load_batch_stubs(php_version);
12        // Index vendor autoload.files (global function/constant helpers such as
13        // Laravel's `confirm()`, `select()`, etc.) before body analysis so
14        // calls to these functions resolve rather than emitting UndefinedFunction.
15        self.ensure_vendor_eager_functions();
16        let _t_stubs = _t0.elapsed();
17
18        // ---- Read files in parallel ----------------------------------
19        let parsed_files: Vec<ParsedProjectFile> = paths
20            .par_iter()
21            .filter_map(|path| match std::fs::read_to_string(path) {
22                Ok(src) => {
23                    let file = Arc::from(path.to_string_lossy().as_ref());
24                    Some(ParsedProjectFile::new(file, Arc::from(src)))
25                }
26                Err(e) => {
27                    eprintln!("Cannot read {}: {}", path.display(), e);
28                    None
29                }
30            })
31            .collect();
32        let _t_read = _t0.elapsed();
33
34        let file_data: Vec<(Arc<str>, Arc<str>)> = parsed_files
35            .iter()
36            .map(|parsed| (parsed.file.clone(), parsed.source.clone()))
37            .collect();
38
39        // ---- Detect files deleted since the last run ------------------------
40        // A file analyzed previously but now gone from disk leaves dependents
41        // holding results that assume its definitions still exist. (A file
42        // merely absent from this run's path set but still on disk is NOT a
43        // deletion — checking disk existence avoids evicting during partial-path
44        // analysis.) Drop their own entries here; dependent eviction happens
45        // below, once per-file surface fingerprints are known.
46        //
47        // `topology_changed` tracks whether this run touched the cache (any file
48        // changed, added, or removed). When nothing changed, the reverse-dep
49        // graph loaded from disk is still accurate, so the rebuild + full
50        // cache.bin rewrite at the end of this pass is skipped.
51        let mut topology_changed = false;
52        let mut removed_files: Vec<String> = Vec::new();
53        if let Some(cache) = &self.cache {
54            let current: rustc_hash::FxHashSet<&str> =
55                file_data.iter().map(|(f, _)| f.as_ref()).collect();
56            removed_files = cache
57                .cached_files()
58                .into_iter()
59                .filter(|f| !current.contains(f.as_str()) && !std::path::Path::new(f).exists())
60                .collect();
61            for f in &removed_files {
62                cache.evict(f);
63            }
64            topology_changed = !removed_files.is_empty();
65        }
66
67        // ---- Register Salsa source inputs for incremental follow-up calls ----
68        {
69            let db = &mut self.db.salsa;
70            for parsed in &parsed_files {
71                db.upsert_source_file(parsed.file.clone(), parsed.source.clone());
72            }
73        }
74        let _t_salsa_reg = _t0.elapsed();
75
76        // ---- Definition collection from the already-parsed AST -------
77        // Returns (FileDefinitions, content_hash, has_hard_parse_errors) so we
78        // can prime the parse cache before the pre-warm loop below.
79        type Pass1Entry = (FileDefinitions, [u8; 32], bool, String);
80        let file_defs: Vec<Pass1Entry> = parsed_files
81            .par_iter()
82            .map(|parsed| {
83                let content_hash = hash_source(parsed.source());
84                let has_hard_parse_errors = parsed
85                    .errors()
86                    .iter()
87                    .any(crate::parser::is_hard_parse_error);
88                let mut all_issues: Vec<Issue> = parsed
89                    .errors()
90                    .iter()
91                    .filter(|err| !crate::parser::is_spurious_reserved_class_error(err))
92                    .map(|err| {
93                        crate::parser::parse_error_to_issue(
94                            err,
95                            &parsed.file,
96                            parsed.source(),
97                            parsed.source_map(),
98                        )
99                    })
100                    .collect();
101                let collector = crate::collector::DefinitionCollector::new_for_slice(
102                    parsed.file.clone(),
103                    parsed.source(),
104                    parsed.source_map(),
105                );
106                let (mut slice, collector_issues) = collector.collect_slice(parsed.owned());
107                all_issues.extend(collector_issues);
108                mir_codebase::definitions::deduplicate_params_in_slice(&mut slice);
109                let defs = FileDefinitions {
110                    slice: Arc::new(slice),
111                    issues: Arc::new(all_issues),
112                };
113                let surface_hash = surface_fingerprint(parsed.source(), parsed.owned());
114                (defs, content_hash, has_hard_parse_errors, surface_hash)
115            })
116            .collect();
117        let _t_collect_defs = _t0.elapsed();
118
119        // Pair each file with its cross-file surface fingerprint (par_iter().map
120        // preserves order, so file_defs aligns with parsed_files).
121        let surface_hashes: HashMap<Arc<str>, String> = parsed_files
122            .iter()
123            .zip(file_defs.iter())
124            .map(|(parsed, (_defs, _h, _e, surface))| (parsed.file.clone(), surface.clone()))
125            .collect();
126
127        // Hex content hashes derived from the pass-1 digests, so the
128        // content-changed pass and the body pass below don't re-hash every
129        // source file (BLAKE3 over all bytes, twice).
130        let content_hexes: HashMap<Arc<str>, String> = parsed_files
131            .iter()
132            .zip(file_defs.iter())
133            .map(|(parsed, (_defs, hash, _e, _surface))| {
134                (
135                    parsed.file.clone(),
136                    blake3::Hash::from(*hash).to_hex().to_string(),
137                )
138            })
139            .collect();
140        let transient_replay = self
141            .cache
142            .is_none()
143            .then(|| {
144                self.transient_batch_replay(
145                    php_version.cache_byte(),
146                    opts.skip_symbols,
147                    &content_hexes,
148                )
149            })
150            .flatten();
151
152        // ---- Cross-file invalidation: evict dependents whose surface changed --
153        // A file whose content changed re-analyzes itself regardless (its body
154        // pass misses the cache below). It cascades to dependents only when its
155        // declaration-level surface changed: a body-only edit to a declared-
156        // return callable leaves every dependent's result intact. A missing or
157        // pre-firewall (empty) stored surface is treated as unknown and cascades.
158        if let Some(cache) = &self.cache {
159            let content_changed: Vec<String> = file_data
160                .iter()
161                .filter_map(|(f, _src)| {
162                    let valid = content_hexes.get(f).is_some_and(|h| cache.is_valid(f, h));
163                    if valid {
164                        None
165                    } else {
166                        Some(f.to_string())
167                    }
168                })
169                .collect();
170            if !content_changed.is_empty() {
171                topology_changed = true;
172            }
173            let mut seeds: Vec<String> = content_changed
174                .into_iter()
175                .filter(|f| {
176                    let new_surface = surface_hashes
177                        .get(f.as_str())
178                        .map(String::as_str)
179                        .unwrap_or("");
180                    // Keep (cascade) unless the stored surface is known and equal.
181                    !matches!(
182                        cache.surface_hash(f),
183                        Some(stored) if !stored.is_empty() && stored == new_surface
184                    )
185                })
186                .collect();
187            seeds.extend(removed_files.iter().cloned());
188            if !seeds.is_empty() {
189                cache.evict_with_dependents(&seeds);
190            }
191        }
192
193        // Prime the in-process parse cache so the pre-warm loop below avoids
194        // re-parsing every project file through collect_file_definitions.
195        {
196            let db = &self.db.salsa;
197            let php_v = php_version.cache_byte();
198            for (defs, hash, has_hard_parse_errors, _surface) in &file_defs {
199                if !*has_hard_parse_errors {
200                    db.prime_parse_cache(
201                        *hash,
202                        php_v,
203                        Arc::clone(&defs.slice),
204                        Arc::clone(&defs.issues),
205                    );
206                }
207            }
208        }
209
210        let mut files_with_parse_errors: HashSet<Arc<str>> = HashSet::default();
211        {
212            // Commit subtype-index class edges alongside issue collection —
213            // parity with `ingest_file`'s single-file path, so goto-implementation
214            // sees implementors from a batch/vendor run without waiting for
215            // each file to be individually touched by an on-demand commit path.
216            let db = &self.db.salsa;
217            for (parsed, (defs, _hash, _hard_err, _surface)) in parsed_files.iter().zip(file_defs) {
218                for issue in defs.issues.iter() {
219                    if matches!(issue.kind, mir_issues::IssueKind::ParseError { .. })
220                        && issue.severity == mir_issues::Severity::Error
221                    {
222                        files_with_parse_errors.insert(issue.location.file.clone());
223                    }
224                }
225                let entries = crate::db::subtype_index::entries_from_slice(&defs.slice);
226                let file_no = db.locked_ref_index().intern_path(&parsed.file);
227                db.set_file_class_edges(file_no, entries);
228                all_issues.extend(Arc::unwrap_or_clone(defs.issues));
229            }
230        }
231        let _t_ingest = _t0.elapsed();
232
233        // ---- Pre-warm collect_file_definitions for project files -------------
234        {
235            let db_prewarm = self.db.snapshot_db();
236            let project_source_files: Vec<SourceFile> = parsed_files
237                .iter()
238                .filter_map(|p| self.db.lookup_source_file(&p.file))
239                .collect();
240            project_source_files
241                .into_par_iter()
242                .for_each_with(db_prewarm, |db, sf| {
243                    let _ = collect_file_definitions(db as &dyn MirDatabase, sf);
244                });
245        }
246        let _t_prewarm_ms = (_t0.elapsed() - _t_ingest).as_secs_f64() * 1000.0;
247
248        // Fold the freshly-registered project files into the workspace symbol
249        // index singleton. It may predate this run (the CLI indexes vendor
250        // before analyze_paths), and adding files doesn't rebuild it.
251        self.refresh_workspace_index();
252
253        // ---- Lazy-load unknown classes via PSR-4 ----------------------------
254        let _t_before_lazy = _t0.elapsed();
255        if let Some(psr4) = self.psr4.clone() {
256            self.lazy_load_missing_classes(psr4, php_version, &mut all_issues);
257        }
258        let _t_lazyload_ms = (_t0.elapsed() - _t_before_lazy).as_secs_f64() * 1000.0;
259
260        // ---- Class-level checks ---------------------------------------------
261        let analyzed_file_set: HashSet<Arc<str>> =
262            file_data.iter().map(|(f, _)| f.clone()).collect();
263
264        // Definitions are all collected and indexed — fire the plugin hook
265        // (Psalm's AfterCodebasePopulated) before any body/class analysis.
266        if let Some(plugins) = mir_plugin::snapshot() {
267            if plugins.hooks().after_codebase_populated {
268                let files: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
269                plugins.after_codebase_populated(&mut mir_plugin::AfterCodebasePopulatedEvent {
270                    files: &files,
271                });
272            }
273        }
274        let _t_class_analyzer = std::time::Instant::now();
275        {
276            let class_db = self.db.snapshot_db();
277            let class_issues = crate::class::ClassAnalyzer::with_files(
278                &class_db,
279                analyzed_file_set.clone(),
280                &file_data,
281            )
282            .analyze_all();
283            all_issues.extend(class_issues);
284        }
285        let _t_class_analyzer_ms = _t_class_analyzer.elapsed().as_secs_f64() * 1000.0;
286
287        let _t_class_checks = _t0.elapsed();
288
289        let mut db_main = self.db.snapshot_db();
290        // All index mutation for the body pass is done (lazy_load_missing_classes
291        // + refresh ran above; lazy_load_from_body_issues runs *after* this pass
292        // on a separate db). Freeze the index on this ephemeral clone so each
293        // find_class_like borrows it instead of cloning the singleton's three
294        // Arcs per call — the per-worker `map_with` clone bumps the refcount once.
295        db_main.freeze_workspace_index();
296
297        // ---- Body analysis: function/method bodies in parallel --------------
298        type BodyResult = (
299            Arc<str>,
300            Vec<Issue>,
301            Vec<crate::symbol::ResolvedSymbol>,
302            Vec<RefLoc>,
303        );
304        let body_results: Vec<BodyResult> = parsed_files
305            .par_iter()
306            .filter(|parsed| !files_with_parse_errors.contains(&parsed.file))
307            .map_with((db_main, self.cache.clone()), |(db, cache), parsed| {
308                let mut driver = BodyAnalyzer::new(&*db as &dyn MirDatabase, php_version);
309                // Diagnostics-only consumers never read the symbol vecs —
310                // don't build them (a Type clone per reference) at all.
311                driver.collect_symbols = !opts.skip_symbols;
312                if let Some(replay) = transient_replay.as_ref() {
313                    if let Some(hit) = replay.files.get(&parsed.file) {
314                        let locs: Vec<RefLoc> = hit
315                            .ref_locs
316                            .iter()
317                            .map(|(symbol, line, col_start, col_end)| RefLoc {
318                                symbol_key: Arc::clone(symbol),
319                                file: parsed.file.clone(),
320                                line: *line,
321                                col_start: *col_start,
322                                col_end: *col_end,
323                            })
324                            .collect();
325                        return (
326                            parsed.file.clone(),
327                            hit.issues.to_vec(),
328                            hit.symbols.to_vec(),
329                            locs,
330                        );
331                    }
332                }
333                let (issues, symbols) = if let Some(cache) = cache.as_ref() {
334                    let h = content_hexes
335                        .get(parsed.file.as_ref())
336                        .cloned()
337                        .unwrap_or_else(|| hash_content(parsed.source()));
338                    if let Some((cached_issues, ref_locs)) = cache.get(&parsed.file, &h) {
339                        // Cache replay: rebuild the file's complete reference
340                        // set straight from the cached tuples — no pending-
341                        // buffer detour. Symbol keys are Arc-shared with the
342                        // cache entry, so this allocates no strings.
343                        let locs: Vec<RefLoc> = ref_locs
344                            .iter()
345                            .map(|(symbol, line, col_start, col_end)| RefLoc {
346                                symbol_key: Arc::clone(symbol),
347                                file: parsed.file.clone(),
348                                line: *line,
349                                col_start: *col_start,
350                                col_end: *col_end,
351                            })
352                            .collect();
353                        return (
354                            parsed.file.clone(),
355                            cached_issues.to_vec(),
356                            Vec::new(),
357                            locs,
358                        );
359                    }
360                    let (issues, symbols) = driver.analyze_bodies(
361                        parsed.owned(),
362                        parsed.file.clone(),
363                        parsed.source(),
364                        parsed.source_map(),
365                    );
366                    let pending = db.take_pending_ref_locs();
367                    let cache_locs: Arc<[crate::cache::CachedRefLoc]> = pending
368                        .iter()
369                        .map(|r| (Arc::clone(&r.symbol_key), r.line, r.col_start, r.col_end))
370                        .collect();
371                    let surface = surface_hashes
372                        .get(parsed.file.as_ref())
373                        .cloned()
374                        .unwrap_or_default();
375                    cache.put(
376                        &parsed.file,
377                        h,
378                        surface,
379                        issues.as_slice().into(),
380                        cache_locs,
381                    );
382                    if let Some(cb) = &opts.on_file_done {
383                        cb();
384                    }
385                    let symbols = if opts.skip_symbols {
386                        Vec::new()
387                    } else {
388                        symbols
389                    };
390                    return (parsed.file.clone(), issues, symbols, pending);
391                } else {
392                    driver.analyze_bodies(
393                        parsed.owned(),
394                        parsed.file.clone(),
395                        parsed.source(),
396                        parsed.source_map(),
397                    )
398                };
399                let pending = db.take_pending_ref_locs();
400                if let Some(cb) = &opts.on_file_done {
401                    cb();
402                }
403                // Drop the per-file symbol vec inside the worker when the
404                // consumer opted out — the orchestrator never accumulates.
405                let symbols = if opts.skip_symbols {
406                    Vec::new()
407                } else {
408                    symbols
409                };
410                (parsed.file.clone(), issues, symbols, pending)
411            })
412            .collect();
413
414        let _t_body_analysis = _t0.elapsed();
415
416        if self.cache.is_none() {
417            let replay_files: HashMap<Arc<str>, crate::session::BatchReplayFile> = body_results
418                .iter()
419                .map(|(file, issues, symbols, ref_locs)| {
420                    let cached_ref_locs: Arc<[crate::cache::CachedRefLoc]> = ref_locs
421                        .iter()
422                        .map(|r| (Arc::clone(&r.symbol_key), r.line, r.col_start, r.col_end))
423                        .collect();
424                    (
425                        file.clone(),
426                        crate::session::BatchReplayFile {
427                            issues: issues.as_slice().into(),
428                            ref_locs: cached_ref_locs,
429                            symbols: symbols.as_slice().into(),
430                        },
431                    )
432                })
433                .collect();
434            self.store_transient_batch_replay(
435                php_version.cache_byte(),
436                opts.skip_symbols,
437                &content_hexes,
438                replay_files,
439            );
440        }
441
442        // Serial commit with replace semantics: each file's output (or cache
443        // replay) is its complete reference set, so stale entries from a
444        // prior run cannot survive an append.
445        let mut all_symbols = Vec::new();
446        {
447            let db = &self.db.salsa;
448            for (file, issues, symbols, ref_locs) in body_results {
449                all_issues.extend(issues);
450                all_symbols.extend(symbols);
451                let file_no = db.locked_ref_index().intern_path(&file);
452                db.set_file_reference_locations(file_no, ref_locs);
453            }
454        }
455
456        // ---- Post-analysis lazy loading: FQCNs used without `use` imports ------
457        if let Some(psr4) = self.psr4.clone() {
458            self.lazy_load_from_body_issues(
459                psr4,
460                php_version,
461                &file_data,
462                &files_with_parse_errors,
463                &mut all_issues,
464                &mut all_symbols,
465                opts.skip_symbols,
466            );
467        }
468
469        // ---- Build reverse dep graph and persist it for the next run ---------
470        // Must run AFTER `commit_reference_locations_batch` (above): the graph's
471        // call-site / instantiation / inferred-return edges are derived from the
472        // committed reference-location map. Built any earlier (the salsa db is
473        // fresh each session) that map is empty, so only structural edges
474        // (parent/interface/trait/declared types) survive — and any dependent
475        // reachable only through a call site or inferred type goes stale.
476        if topology_changed {
477            if let Some(cache) = &self.cache {
478                let db_snapshot = self.db.snapshot_db();
479                let rev = build_reverse_deps(&db_snapshot);
480                cache.set_reverse_deps(rev);
481            }
482        }
483
484        // Persist cache hits/misses to disk
485        if let Some(cache) = &self.cache {
486            cache.flush();
487        }
488
489        // ---- Dead-code detection -------------------------------------------
490        if opts.should_run_dead_code() {
491            let salsa = self.snapshot_db();
492            let _t_dead_code = std::time::Instant::now();
493            let dead_code_issues =
494                crate::dead_code::DeadCodeAnalyzer::with_files(&salsa, analyzed_file_set.clone())
495                    .analyze();
496            all_issues.extend(dead_code_issues);
497            if std::env::var("MIR_TIMING").is_ok() {
498                eprintln!(
499                    "[timing] dead_code_analyzer={:.0}ms",
500                    _t_dead_code.elapsed().as_secs_f64() * 1000.0
501                );
502            }
503        }
504
505        let _t_total = _t0.elapsed();
506        if std::env::var("MIR_TIMING").is_ok() {
507            eprintln!(
508                "[timing] stubs={:.0}ms read={:.0}ms salsa_reg={:.0}ms collect_defs={:.0}ms ingest={:.0}ms class_checks={:.0}ms (prewarm={:.0}ms lazy_load={:.0}ms class_analyzer={:.0}ms) body_analysis={:.0}ms total={:.0}ms",
509                _t_stubs.as_secs_f64() * 1000.0,
510                (_t_read - _t_stubs).as_secs_f64() * 1000.0,
511                (_t_salsa_reg - _t_read).as_secs_f64() * 1000.0,
512                (_t_collect_defs - _t_salsa_reg).as_secs_f64() * 1000.0,
513                (_t_ingest - _t_collect_defs).as_secs_f64() * 1000.0,
514                (_t_class_checks - _t_ingest).as_secs_f64() * 1000.0,
515                _t_prewarm_ms,
516                _t_lazyload_ms,
517                _t_class_analyzer_ms,
518                (_t_body_analysis - _t_class_checks).as_secs_f64() * 1000.0,
519                _t_total.as_secs_f64() * 1000.0,
520            );
521        }
522
523        // Plugin issue veto (Psalm's BeforeAddIssue) — applied to the final
524        // set before config-level suppression so a veto can't be shadowed.
525        if let Some(plugins) = mir_plugin::snapshot() {
526            if plugins.hooks().before_add_issue {
527                all_issues.retain(|i| plugins.before_add_issue(i));
528            }
529        }
530
531        opts.apply(&mut all_issues);
532        let analyzed_files_vec: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
533        self.apply_suppressions_and_emit_unused(&mut all_issues, &analyzed_files_vec);
534        if let Some(dump) = crate::metrics::dump() {
535            eprintln!("{dump}");
536        }
537
538        // ---- Build workspace symbol index singleton -------------------------
539        self.db.salsa.rebuild_workspace_symbol_index();
540
541        AnalysisResult::build(all_issues, rustc_hash::FxHashMap::default(), all_symbols)
542    }
543    /// Re-analyze a single file (definition collection + body analysis) within
544    /// the batch context, consulting the disk cache.
545    ///
546    /// Use [`Self::reanalyze_dependents`] for LSP-style per-file flows that
547    /// don't need batch options.
548    pub fn re_analyze_file(
549        &mut self,
550        file_path: &str,
551        new_content: &str,
552        opts: &BatchOptions,
553    ) -> AnalysisResult {
554        self.index.clear_transient_batch_replay();
555        let php_version = self.batch_php_version(opts);
556
557        // Mirror-only workspace files may be waiting in the pending index
558        // set. Reconcile them before the content-hash fast path; otherwise a
559        // cached result computed against a partial workspace could be replayed
560        // before `settle_workspace_index()` gets a chance to invalidate it.
561        self.settle_workspace_index();
562
563        // Fast path: content unchanged and cache has a valid entry.
564        if let Some(cache) = &self.cache {
565            let h = hash_content(new_content);
566            if let Some((cached_issues, ref_locs)) = cache.get(file_path, &h) {
567                let mut issues = cached_issues.to_vec();
568                let file: Arc<str> = Arc::from(file_path);
569                // Replace semantics: the cached set is the file's complete
570                // reference set, so stale entries from a prior version are
571                // cleared rather than appended over.
572                let locs: Vec<RefLoc> = ref_locs
573                    .iter()
574                    .map(|(symbol, line, col_start, col_end)| RefLoc {
575                        symbol_key: Arc::clone(symbol),
576                        file: file.clone(),
577                        line: *line,
578                        col_start: *col_start,
579                        col_end: *col_end,
580                    })
581                    .collect();
582                // Retire first so in-flight snapshot commits for the old
583                // text are refused, as on the analysis path below.
584                self.index.retire_references(&self.db.salsa, file_path);
585                let db = &self.db.salsa;
586                let file_no = db.locked_ref_index().intern_path(&file);
587                db.set_file_reference_locations(file_no, locs);
588                self.index.clear_dependency_graph_cache();
589                opts.apply(&mut issues);
590                self.apply_suppressions_and_emit_unused(&mut issues, std::slice::from_ref(&file));
591                return AnalysisResult::build(issues, HashMap::default(), Vec::new());
592            }
593        }
594
595        let file: Arc<str> = Arc::from(file_path);
596
597        self.index.retire_references(&self.db.salsa, file_path);
598
599        let collected =
600            self.db
601                .collect_and_ingest_file_with_parsed(file.clone(), new_content, php_version);
602        let file_defs = collected.file_defs;
603
604        let mut all_issues: Vec<Issue> = Arc::unwrap_or_clone(file_defs.issues.clone());
605
606        {
607            let db = &mut self.db.salsa;
608            if db.workspace_symbol_index_singleton().is_some() {
609                if let Some(sf) = db.lookup_source_file(file.as_ref()) {
610                    if db.file_declarations_changed(sf) {
611                        db.rebuild_workspace_symbol_index();
612                    }
613                }
614            }
615        }
616
617        let (symbols, surface_hash) = {
618            let db = &mut self.db.salsa;
619            let parsed = collected
620                .parsed
621                .unwrap_or_else(|| php_rs_parser::parse(new_content));
622            let surface_hash = surface_fingerprint(new_content, &parsed.program);
623
624            let has_hard_errors = parsed.errors.iter().any(crate::parser::is_hard_parse_error);
625            let symbols = if !has_hard_errors {
626                let db_ref: &dyn MirDatabase = &*db;
627                let mut driver = BodyAnalyzer::new(db_ref, php_version);
628                driver.collect_symbols = !opts.skip_symbols;
629                let (body_issues, symbols) = driver.analyze_bodies(
630                    &parsed.program,
631                    file.clone(),
632                    new_content,
633                    &parsed.source_map,
634                );
635                all_issues.extend(body_issues);
636                let pending = db.take_pending_ref_locs();
637                let file_no = db.locked_ref_index().intern_path(&file);
638                db.set_file_reference_locations(file_no, pending);
639                if opts.skip_symbols {
640                    Vec::new()
641                } else {
642                    symbols
643                }
644            } else {
645                Vec::new()
646            };
647            (symbols, surface_hash)
648        };
649
650        // Bake inline-suppression marks in *before* caching: suppression is a
651        // pure function of file content (and the cache key hashes content), so
652        // the cached issues should already carry their marks. The cache-hit
653        // branch above replays this file's source without re-registering the
654        // `SourceFile` input, so the db-backed post-filter cannot recompute
655        // marks there — caching the canonical result is what keeps a fresh
656        // process honoring `@mir-ignore` on an unchanged file.
657        mark_suppressed(
658            &mut all_issues,
659            &crate::suppression::SuppressionMap::from_source(new_content),
660        );
661
662        if let Some(cache) = &self.cache {
663            let h = hash_content(new_content);
664            // Cascade to dependents only when this file's cross-file surface
665            // changed; a body-only edit to a declared-return callable leaves
666            // their results valid. Unknown (absent/empty) stored surface cascades.
667            let surface_changed = match cache.surface_hash(file_path) {
668                Some(stored) if !stored.is_empty() => stored != surface_hash,
669                _ => true,
670            };
671            if surface_changed {
672                cache.evict_with_dependents(&[file_path.to_string()]);
673            }
674            let db = self.snapshot_db();
675            let ref_locs = extract_reference_locations(&db, &file);
676            cache.put(
677                file_path,
678                h,
679                surface_hash,
680                all_issues.as_slice().into(),
681                ref_locs,
682            );
683        }
684
685        opts.apply(&mut all_issues);
686        // Emit `UnusedSuppress` for named suppressions that matched nothing —
687        // present on the cache-hit branch above and on `analyze_paths`, but
688        // missing here, so every non-cached re-analysis (the actual
689        // incremental/LSP-edit pipeline) silently dropped this diagnostic.
690        // `mark_suppressed` above already baked suppression marks into what
691        // gets cached; this only adds the (uncached, always-fresh) unused-
692        // suppression pass on top, mirroring the cache-hit branch's order.
693        self.apply_suppressions_and_emit_unused(&mut all_issues, std::slice::from_ref(&file));
694        AnalysisResult::build(all_issues, HashMap::default(), symbols)
695    }
696
697    /// Collect type definitions only from `paths` into the codebase
698    /// without analyzing method bodies or emitting issues. Used to load
699    /// vendor types.
700    ///
701    /// When a disk-backed cache is attached, per-file `StubSlice` results
702    /// from previous runs are reused on a content-hash match, eliminating
703    /// the parse + definition-collection step. Cache misses run the normal
704    /// pipeline and write back so subsequent runs hit.
705    pub fn collect_definitions(&mut self, paths: &[PathBuf]) {
706        self.index.clear_transient_batch_replay();
707        let _timing = std::env::var("MIR_TIMING").is_ok();
708        let _t0 = std::time::Instant::now();
709
710        let php_v = self.php_version.cache_byte();
711
712        struct FileEntry {
713            file: Arc<str>,
714            src: Arc<str>,
715            hash: [u8; 32],
716            cached: Option<mir_codebase::definitions::StubSlice>,
717        }
718        let stub_cache = self.db.stub_cache.clone();
719        let entries: Vec<FileEntry> = paths
720            .par_iter()
721            .filter_map(|path| {
722                let src = std::fs::read_to_string(path).ok()?;
723                let file: Arc<str> = Arc::from(path.to_string_lossy().as_ref());
724                let src: Arc<str> = Arc::from(src);
725                let hash = hash_source(&src);
726                let cached = stub_cache.as_ref().and_then(|c| {
727                    let (mut slice, _issues) = c.get(&file, &hash, php_v)?;
728                    prepare_for_ingest(&mut slice);
729                    Some(slice)
730                });
731                Some(FileEntry {
732                    file,
733                    src,
734                    hash,
735                    cached,
736                })
737            })
738            .collect();
739        let _t_read = _t0.elapsed();
740
741        let source_files: Vec<SourceFile> = {
742            let db = &mut self.db.salsa;
743            entries
744                .iter()
745                .map(|e| {
746                    db.upsert_source_file_with_durability(
747                        e.file.clone(),
748                        e.src.clone(),
749                        salsa::Durability::HIGH,
750                    )
751                })
752                .collect()
753        };
754        let _t_reg = _t0.elapsed();
755
756        let db_pass1 = self.db.snapshot_db();
757        let stub_cache = self.db.stub_cache.clone();
758        let prepared: Vec<(Arc<str>, mir_codebase::definitions::StubSlice)> = entries
759            .into_par_iter()
760            .zip(source_files.into_par_iter())
761            .map_with(db_pass1, |db, (mut entry, salsa_file)| {
762                if let Some(slice) = entry.cached.take() {
763                    let slice_arc = Arc::new(slice);
764                    db.parse_cache().insert(
765                        entry.hash,
766                        php_v,
767                        Arc::clone(&slice_arc),
768                        Arc::new(Vec::new()),
769                    );
770                    return (entry.file.clone(), (*slice_arc).clone());
771                }
772                let defs = collect_file_definitions(&*db, salsa_file);
773                if let Some(cache) = stub_cache.as_ref() {
774                    cache.put(&entry.file, &entry.hash, php_v, &defs.slice, &defs.issues);
775                }
776                (entry.file.clone(), (*defs.slice).clone())
777            })
778            .collect();
779        let _t_collect = _t0.elapsed();
780        // Commit subtype-index class edges for the vendor tree: without this,
781        // goto-implementation can't surface implementors that live only in
782        // vendor/ (index_batch's project-file warm sweep has the same gap for
783        // project files, but no StubSlice is cheaply available there — see
784        // the persistence work tracked separately).
785        {
786            let db = &self.db.salsa;
787            for (file, slice) in &prepared {
788                let entries = crate::db::subtype_index::entries_from_slice(slice);
789                let file_no = db.locked_ref_index().intern_path(file);
790                db.set_file_class_edges(file_no, entries);
791            }
792        }
793        drop(prepared);
794        let _t_ingest = _t0.elapsed();
795
796        if _timing {
797            let (hits, misses) = self.stub_cache_stats();
798            eprintln!(
799                "[vendor] read={:.0}ms reg={:.0}ms collect={:.0}ms ingest={:.0}ms total={:.0}ms (cache hits={hits} misses={misses})",
800                _t_read.as_secs_f64() * 1000.0,
801                (_t_reg - _t_read).as_secs_f64() * 1000.0,
802                (_t_collect - _t_reg).as_secs_f64() * 1000.0,
803                (_t_ingest - _t_collect).as_secs_f64() * 1000.0,
804                _t_ingest.as_secs_f64() * 1000.0,
805            );
806        }
807
808        self.db.salsa.rebuild_workspace_symbol_index();
809
810        crate::collector::print_collector_stats();
811    }
812}
813
814#[cfg(test)]
815mod tests {
816    use rustc_hash::FxHashMap as HashMap;
817    use std::fs;
818    use std::sync::Arc;
819
820    use crate::db::Work;
821    use crate::{AnalysisSession, BatchOptions, PhpVersion};
822
823    fn write_php(dir: &tempfile::TempDir, name: &str, contents: &str) -> std::path::PathBuf {
824        let path = dir.path().join(name);
825        fs::write(&path, contents).expect("write fixture");
826        path
827    }
828
829    #[test]
830    fn identical_batch_rerun_replays_without_symbols() {
831        let dir = tempfile::tempdir().expect("tempdir");
832        let file = write_php(
833            &dir,
834            "Example.php",
835            "<?php\nclass Example {\n    public function answer(): int { return 42; }\n}\n(new Example())->answer();\n",
836        );
837        let mut session = AnalysisSession::new(PhpVersion::LATEST);
838        let opts = BatchOptions::new().without_symbols();
839        let hash = crate::cache::hash_content(
840            "<?php\nclass Example {\n    public function answer(): int { return 42; }\n}\n(new Example())->answer();\n",
841        );
842        let hashes = HashMap::from_iter([(Arc::from(file.to_string_lossy().as_ref()), hash)]);
843
844        let first = session.analyze_paths(std::slice::from_ref(&file), &opts);
845        assert!(first.issues.is_empty());
846        assert!(
847            session
848                .transient_batch_replay(PhpVersion::LATEST.cache_byte(), true, &hashes)
849                .is_some(),
850            "expected first run to populate transient replay"
851        );
852
853        let walks = session.work_count(Work::WholeFileWalk);
854        let symbols = session.work_count(Work::SymbolAllocated);
855        let second = session.analyze_paths(std::slice::from_ref(&file), &opts);
856        assert!(second.issues.is_empty());
857        assert!(second.symbols.is_empty());
858
859        assert_eq!(
860            session.work_count(Work::WholeFileWalk) - walks,
861            0,
862            "expected replay to skip body walks"
863        );
864        assert_eq!(
865            session.work_count(Work::SymbolAllocated) - symbols,
866            0,
867            "expected diagnostics-only replay to avoid symbol allocation"
868        );
869    }
870
871    #[test]
872    fn identical_batch_rerun_replays_with_symbols() {
873        let dir = tempfile::tempdir().expect("tempdir");
874        let file = write_php(
875            &dir,
876            "Example.php",
877            "<?php\nclass Example {\n    public function answer(): int { return 42; }\n}\n(new Example())->answer();\n",
878        );
879        let mut session = AnalysisSession::new(PhpVersion::LATEST);
880        let opts = BatchOptions::new();
881        let hash = crate::cache::hash_content(
882            "<?php\nclass Example {\n    public function answer(): int { return 42; }\n}\n(new Example())->answer();\n",
883        );
884        let hashes = HashMap::from_iter([(Arc::from(file.to_string_lossy().as_ref()), hash)]);
885
886        let first = session.analyze_paths(std::slice::from_ref(&file), &opts);
887        assert!(
888            !first.symbols.is_empty(),
889            "expected first run to collect symbols"
890        );
891        assert!(
892            session
893                .transient_batch_replay(PhpVersion::LATEST.cache_byte(), false, &hashes)
894                .is_some(),
895            "expected first run to populate transient replay"
896        );
897
898        let walks = session.work_count(Work::WholeFileWalk);
899        let second = session.analyze_paths(std::slice::from_ref(&file), &opts);
900        assert_eq!(second.symbols.len(), first.symbols.len());
901
902        assert_eq!(
903            session.work_count(Work::WholeFileWalk) - walks,
904            0,
905            "expected replay to skip body walks"
906        );
907    }
908
909    #[test]
910    fn session_mutation_clears_batch_replay() {
911        let dir = tempfile::tempdir().expect("tempdir");
912        let file = write_php(
913            &dir,
914            "Example.php",
915            "<?php\nclass Example {\n    public function answer(): int { return 42; }\n}\n(new Example())->answer();\n",
916        );
917        let file_arc: Arc<str> = Arc::from(file.to_string_lossy().as_ref());
918        let mut session = AnalysisSession::new(PhpVersion::LATEST);
919        let opts = BatchOptions::new().without_symbols();
920
921        let _ = session.analyze_paths(std::slice::from_ref(&file), &opts);
922        session.ingest_file(
923            Arc::clone(&file_arc),
924            Arc::from(
925                "<?php\nclass Example {\n    public function answer(): string { return 'forty two'; }\n}\n(new Example())->answer();\n",
926            ),
927        );
928
929        let walks = session.work_count(Work::WholeFileWalk);
930        let result = session.analyze_paths(std::slice::from_ref(&file), &opts);
931        assert!(result.issues.is_empty());
932
933        assert_eq!(
934            session.work_count(Work::WholeFileWalk) - walks,
935            1,
936            "expected session mutation to invalidate replay"
937        );
938    }
939}