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(&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 mut guard = self.db.salsa.write();
70            for parsed in &parsed_files {
71                guard.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
141        // ---- Cross-file invalidation: evict dependents whose surface changed --
142        // A file whose content changed re-analyzes itself regardless (its body
143        // pass misses the cache below). It cascades to dependents only when its
144        // declaration-level surface changed: a body-only edit to a declared-
145        // return callable leaves every dependent's result intact. A missing or
146        // pre-firewall (empty) stored surface is treated as unknown and cascades.
147        if let Some(cache) = &self.cache {
148            let content_changed: Vec<String> = file_data
149                .iter()
150                .filter_map(|(f, _src)| {
151                    let valid = content_hexes.get(f).is_some_and(|h| cache.is_valid(f, h));
152                    if valid {
153                        None
154                    } else {
155                        Some(f.to_string())
156                    }
157                })
158                .collect();
159            if !content_changed.is_empty() {
160                topology_changed = true;
161            }
162            let mut seeds: Vec<String> = content_changed
163                .into_iter()
164                .filter(|f| {
165                    let new_surface = surface_hashes
166                        .get(f.as_str())
167                        .map(String::as_str)
168                        .unwrap_or("");
169                    // Keep (cascade) unless the stored surface is known and equal.
170                    !matches!(
171                        cache.surface_hash(f),
172                        Some(stored) if !stored.is_empty() && stored == new_surface
173                    )
174                })
175                .collect();
176            seeds.extend(removed_files.iter().cloned());
177            if !seeds.is_empty() {
178                cache.evict_with_dependents(&seeds);
179            }
180        }
181
182        // Prime the in-process parse cache so the pre-warm loop below avoids
183        // re-parsing every project file through collect_file_definitions.
184        {
185            let guard = self.db.salsa.read();
186            let php_v = php_version.cache_byte();
187            for (defs, hash, has_hard_parse_errors, _surface) in &file_defs {
188                if !*has_hard_parse_errors {
189                    guard.prime_parse_cache(*hash, php_v, Arc::clone(&defs.slice));
190                }
191            }
192        }
193
194        let mut files_with_parse_errors: HashSet<Arc<str>> = HashSet::default();
195        {
196            // Commit subtype-index class edges alongside issue collection —
197            // parity with `ingest_file`'s single-file path, so goto-implementation
198            // sees implementors from a batch/vendor run without waiting for
199            // each file to be individually touched by an on-demand commit path.
200            let guard = self.db.salsa.read();
201            for (parsed, (defs, _hash, _hard_err, _surface)) in parsed_files.iter().zip(file_defs) {
202                for issue in defs.issues.iter() {
203                    if matches!(issue.kind, mir_issues::IssueKind::ParseError { .. })
204                        && issue.severity == mir_issues::Severity::Error
205                    {
206                        files_with_parse_errors.insert(issue.location.file.clone());
207                    }
208                }
209                let entries = crate::db::subtype_index::entries_from_slice(&defs.slice);
210                guard.set_file_class_edges(&parsed.file, entries);
211                all_issues.extend(Arc::unwrap_or_clone(defs.issues));
212            }
213        }
214        let _t_ingest = _t0.elapsed();
215
216        // ---- Pre-warm collect_file_definitions for project files -------------
217        {
218            let db_prewarm = {
219                let guard = self.db.salsa.read();
220                (**guard).clone()
221            };
222            let project_source_files: Vec<SourceFile> = {
223                let guard = self.db.salsa.read();
224                parsed_files
225                    .iter()
226                    .filter_map(|p| (**guard).lookup_source_file(&p.file))
227                    .collect()
228            };
229            project_source_files
230                .into_par_iter()
231                .for_each_with(db_prewarm, |db, sf| {
232                    let _ = collect_file_definitions(db as &dyn MirDatabase, sf);
233                });
234        }
235        let _t_prewarm_ms = (_t0.elapsed() - _t_ingest).as_secs_f64() * 1000.0;
236
237        // Fold the freshly-registered project files into the workspace symbol
238        // index singleton. The singleton may have been built from vendor before
239        // this run (CLI indexes vendor before analyze_paths); since adding files
240        // no longer nulls it, project classes would otherwise be invisible to
241        // find_class_like and reported as false UndefinedClass.
242        self.refresh_workspace_index();
243
244        // ---- Lazy-load unknown classes via PSR-4 ----------------------------
245        let _t_before_lazy = _t0.elapsed();
246        if let Some(psr4) = self.psr4.clone() {
247            self.lazy_load_missing_classes(psr4, php_version, &mut all_issues);
248        }
249        let _t_lazyload_ms = (_t0.elapsed() - _t_before_lazy).as_secs_f64() * 1000.0;
250
251        // ---- Class-level checks ---------------------------------------------
252        let analyzed_file_set: HashSet<Arc<str>> =
253            file_data.iter().map(|(f, _)| f.clone()).collect();
254
255        // Definitions are all collected and indexed — fire the plugin hook
256        // (Psalm's AfterCodebasePopulated) before any body/class analysis.
257        if let Some(plugins) = mir_plugin::snapshot() {
258            if plugins.hooks().after_codebase_populated {
259                let files: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
260                plugins.after_codebase_populated(&mut mir_plugin::AfterCodebasePopulatedEvent {
261                    files: &files,
262                });
263            }
264        }
265        let _t_class_analyzer = std::time::Instant::now();
266        {
267            let class_db = {
268                let guard = self.db.salsa.read();
269                (**guard).clone()
270            };
271            let class_issues = crate::class::ClassAnalyzer::with_files(
272                &class_db,
273                analyzed_file_set.clone(),
274                &file_data,
275            )
276            .analyze_all();
277            all_issues.extend(class_issues);
278        }
279        let _t_class_analyzer_ms = _t_class_analyzer.elapsed().as_secs_f64() * 1000.0;
280
281        let _t_class_checks = _t0.elapsed();
282
283        let mut db_main = {
284            let guard = self.db.salsa.read();
285            (**guard).clone()
286        };
287        // All index mutation for the body pass is done (lazy_load_missing_classes
288        // + refresh ran above; lazy_load_from_body_issues runs *after* this pass
289        // on a separate db). Freeze the index on this ephemeral clone so each
290        // find_class_like borrows it instead of cloning the singleton's three
291        // Arcs per call — the per-worker `map_with` clone bumps the refcount once.
292        db_main.freeze_workspace_index();
293
294        // ---- Body analysis: function/method bodies in parallel --------------
295        type BodyResult = (
296            Arc<str>,
297            Vec<Issue>,
298            Vec<crate::symbol::ResolvedSymbol>,
299            Vec<RefLoc>,
300        );
301        let body_results: Vec<BodyResult> = parsed_files
302            .par_iter()
303            .filter(|parsed| !files_with_parse_errors.contains(&parsed.file))
304            .map_with(db_main, |db, parsed| {
305                let mut driver = BodyAnalyzer::new(&*db as &dyn MirDatabase, php_version);
306                // Diagnostics-only consumers never read the symbol vecs —
307                // don't build them (a Type clone per reference) at all.
308                driver.collect_symbols = !opts.skip_symbols;
309                let (issues, symbols) = if let Some(cache) = &self.cache {
310                    let h = content_hexes
311                        .get(parsed.file.as_ref())
312                        .cloned()
313                        .unwrap_or_else(|| hash_content(parsed.source()));
314                    if let Some((cached_issues, ref_locs)) = cache.get(&parsed.file, &h) {
315                        // Cache replay: rebuild the file's complete reference
316                        // set straight from the cached tuples — no pending-
317                        // buffer detour. Symbol keys are Arc-shared with the
318                        // cache entry, so this allocates no strings.
319                        let locs: Vec<RefLoc> = ref_locs
320                            .iter()
321                            .map(|(symbol, line, col_start, col_end)| RefLoc {
322                                symbol_key: Arc::clone(symbol),
323                                file: parsed.file.clone(),
324                                line: *line,
325                                col_start: *col_start,
326                                col_end: *col_end,
327                            })
328                            .collect();
329                        return (
330                            parsed.file.clone(),
331                            cached_issues.to_vec(),
332                            Vec::new(),
333                            locs,
334                        );
335                    }
336                    let (issues, symbols) = driver.analyze_bodies(
337                        parsed.owned(),
338                        parsed.file.clone(),
339                        parsed.source(),
340                        parsed.source_map(),
341                    );
342                    let pending = db.take_pending_ref_locs();
343                    let cache_locs: Arc<[crate::cache::CachedRefLoc]> = pending
344                        .iter()
345                        .map(|r| (Arc::clone(&r.symbol_key), r.line, r.col_start, r.col_end))
346                        .collect();
347                    let surface = surface_hashes
348                        .get(parsed.file.as_ref())
349                        .cloned()
350                        .unwrap_or_default();
351                    cache.put(
352                        &parsed.file,
353                        h,
354                        surface,
355                        issues.as_slice().into(),
356                        cache_locs,
357                    );
358                    if let Some(cb) = &opts.on_file_done {
359                        cb();
360                    }
361                    let symbols = if opts.skip_symbols {
362                        Vec::new()
363                    } else {
364                        symbols
365                    };
366                    return (parsed.file.clone(), issues, symbols, pending);
367                } else {
368                    driver.analyze_bodies(
369                        parsed.owned(),
370                        parsed.file.clone(),
371                        parsed.source(),
372                        parsed.source_map(),
373                    )
374                };
375                let pending = db.take_pending_ref_locs();
376                if let Some(cb) = &opts.on_file_done {
377                    cb();
378                }
379                // Drop the per-file symbol vec inside the worker when the
380                // consumer opted out — the orchestrator never accumulates.
381                let symbols = if opts.skip_symbols {
382                    Vec::new()
383                } else {
384                    symbols
385                };
386                (parsed.file.clone(), issues, symbols, pending)
387            })
388            .collect();
389
390        let _t_body_analysis = _t0.elapsed();
391
392        // Serial commit with replace semantics: each file's output (or cache
393        // replay) is its complete reference set, so stale entries from a
394        // prior run cannot survive an append.
395        let mut all_symbols = Vec::new();
396        {
397            let guard = self.db.salsa.read();
398            for (file, issues, symbols, ref_locs) in body_results {
399                all_issues.extend(issues);
400                all_symbols.extend(symbols);
401                guard.set_file_reference_locations(file.as_ref(), ref_locs);
402            }
403        }
404
405        // ---- Post-analysis lazy loading: FQCNs used without `use` imports ------
406        if let Some(psr4) = self.psr4.clone() {
407            self.lazy_load_from_body_issues(
408                psr4,
409                php_version,
410                &file_data,
411                &files_with_parse_errors,
412                &mut all_issues,
413                &mut all_symbols,
414                opts.skip_symbols,
415            );
416        }
417
418        // ---- Build reverse dep graph and persist it for the next run ---------
419        // Must run AFTER `commit_reference_locations_batch` (above): the graph's
420        // call-site / instantiation / inferred-return edges are derived from the
421        // committed reference-location map. Built any earlier (the salsa db is
422        // fresh each session) that map is empty, so only structural edges
423        // (parent/interface/trait/declared types) survive — and any dependent
424        // reachable only through a call site or inferred type goes stale.
425        if topology_changed {
426            if let Some(cache) = &self.cache {
427                let db_snapshot = {
428                    let guard = self.db.salsa.read();
429                    (**guard).clone()
430                };
431                let rev = build_reverse_deps(&db_snapshot);
432                cache.set_reverse_deps(rev);
433            }
434        }
435
436        // Persist cache hits/misses to disk
437        if let Some(cache) = &self.cache {
438            cache.flush();
439        }
440
441        // ---- Dead-code detection -------------------------------------------
442        if opts.should_run_dead_code() {
443            let salsa = self.snapshot_db();
444            let _t_dead_code = std::time::Instant::now();
445            let dead_code_issues =
446                crate::dead_code::DeadCodeAnalyzer::with_files(&salsa, analyzed_file_set.clone())
447                    .analyze();
448            all_issues.extend(dead_code_issues);
449            if std::env::var("MIR_TIMING").is_ok() {
450                eprintln!(
451                    "[timing] dead_code_analyzer={:.0}ms",
452                    _t_dead_code.elapsed().as_secs_f64() * 1000.0
453                );
454            }
455        }
456
457        let _t_total = _t0.elapsed();
458        if std::env::var("MIR_TIMING").is_ok() {
459            eprintln!(
460                "[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",
461                _t_stubs.as_secs_f64() * 1000.0,
462                (_t_read - _t_stubs).as_secs_f64() * 1000.0,
463                (_t_salsa_reg - _t_read).as_secs_f64() * 1000.0,
464                (_t_collect_defs - _t_salsa_reg).as_secs_f64() * 1000.0,
465                (_t_ingest - _t_collect_defs).as_secs_f64() * 1000.0,
466                (_t_class_checks - _t_ingest).as_secs_f64() * 1000.0,
467                _t_prewarm_ms,
468                _t_lazyload_ms,
469                _t_class_analyzer_ms,
470                (_t_body_analysis - _t_class_checks).as_secs_f64() * 1000.0,
471                _t_total.as_secs_f64() * 1000.0,
472            );
473        }
474
475        // Plugin issue veto (Psalm's BeforeAddIssue) — applied to the final
476        // set before config-level suppression so a veto can't be shadowed.
477        if let Some(plugins) = mir_plugin::snapshot() {
478            if plugins.hooks().before_add_issue {
479                all_issues.retain(|i| plugins.before_add_issue(i));
480            }
481        }
482
483        opts.apply(&mut all_issues);
484        let analyzed_files_vec: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
485        self.apply_suppressions_and_emit_unused(&mut all_issues, &analyzed_files_vec);
486        if let Some(dump) = crate::metrics::dump() {
487            eprintln!("{dump}");
488        }
489
490        // ---- Build workspace symbol index singleton -------------------------
491        {
492            let mut guard = self.db.salsa.write();
493            guard.rebuild_workspace_symbol_index();
494        }
495
496        AnalysisResult::build(all_issues, rustc_hash::FxHashMap::default(), all_symbols)
497    }
498    /// Re-analyze a single file (definition collection + body analysis) within the batch context.
499    ///
500    /// Mirrors the old `ProjectAnalyzer::re_analyze_file` cache-aware path.
501    /// Use [`Self::reanalyze_dependents`] for LSP-style per-file flows that
502    /// don't need batch options.
503    pub fn re_analyze_file(
504        &self,
505        file_path: &str,
506        new_content: &str,
507        opts: &BatchOptions,
508    ) -> AnalysisResult {
509        let php_version = self.batch_php_version(opts);
510
511        // Fast path: content unchanged and cache has a valid entry.
512        if let Some(cache) = &self.cache {
513            let h = hash_content(new_content);
514            if let Some((cached_issues, ref_locs)) = cache.get(file_path, &h) {
515                let mut issues = cached_issues.to_vec();
516                let file: Arc<str> = Arc::from(file_path);
517                // Replace semantics: the cached set is the file's complete
518                // reference set, so stale entries from a prior version are
519                // cleared rather than appended over.
520                let locs: Vec<RefLoc> = ref_locs
521                    .iter()
522                    .map(|(symbol, line, col_start, col_end)| RefLoc {
523                        symbol_key: Arc::clone(symbol),
524                        file: file.clone(),
525                        line: *line,
526                        col_start: *col_start,
527                        col_end: *col_end,
528                    })
529                    .collect();
530                let guard = self.db.salsa.read();
531                guard.set_file_reference_locations(file_path, locs);
532                drop(guard);
533                opts.apply(&mut issues);
534                self.apply_suppressions_and_emit_unused(&mut issues, std::slice::from_ref(&file));
535                return AnalysisResult::build(issues, HashMap::default(), Vec::new());
536            }
537        }
538
539        let file: Arc<str> = Arc::from(file_path);
540
541        {
542            let mut guard = self.db.salsa.write();
543            guard.remove_file_definitions(file_path);
544        }
545
546        let file_defs = {
547            let mut guard = self.db.salsa.write();
548            let salsa_file = guard.upsert_source_file(file.clone(), Arc::from(new_content));
549            collect_file_definitions(&**guard, salsa_file).clone()
550        };
551
552        let mut all_issues: Vec<Issue> = Arc::unwrap_or_clone(file_defs.issues.clone());
553
554        {
555            let mut guard = self.db.salsa.write();
556            if guard.workspace_symbol_index_singleton().is_some() {
557                if let Some(sf) = guard.lookup_source_file(file.as_ref()) {
558                    if guard.file_declarations_changed(sf) {
559                        guard.rebuild_workspace_symbol_index();
560                    }
561                }
562            }
563        }
564
565        let (symbols, surface_hash) = {
566            let guard = self.db.salsa.write();
567
568            let parsed = php_rs_parser::parse(new_content);
569            let surface_hash = surface_fingerprint(new_content, &parsed.program);
570
571            let has_hard_errors = parsed.errors.iter().any(crate::parser::is_hard_parse_error);
572            let symbols = if !has_hard_errors {
573                let db_ref: &dyn MirDatabase = &**guard;
574                let driver = BodyAnalyzer::new(db_ref, php_version);
575                let (body_issues, symbols) = driver.analyze_bodies(
576                    &parsed.program,
577                    file.clone(),
578                    new_content,
579                    &parsed.source_map,
580                );
581                all_issues.extend(body_issues);
582                let pending = guard.take_pending_ref_locs();
583                guard.set_file_reference_locations(file.as_ref(), pending);
584                symbols
585            } else {
586                Vec::new()
587            };
588            (symbols, surface_hash)
589        };
590
591        // Bake inline-suppression marks in *before* caching: suppression is a
592        // pure function of file content (and the cache key hashes content), so
593        // the cached issues should already carry their marks. The cache-hit
594        // branch above replays this file's source without re-registering the
595        // `SourceFile` input, so the db-backed post-filter cannot recompute
596        // marks there — caching the canonical result is what keeps a fresh
597        // process honoring `@mir-ignore` on an unchanged file.
598        mark_suppressed(
599            &mut all_issues,
600            &crate::suppression::SuppressionMap::from_source(new_content),
601        );
602
603        if let Some(cache) = &self.cache {
604            let h = hash_content(new_content);
605            // Cascade to dependents only when this file's cross-file surface
606            // changed; a body-only edit to a declared-return callable leaves
607            // their results valid. Unknown (absent/empty) stored surface cascades.
608            let surface_changed = match cache.surface_hash(file_path) {
609                Some(stored) if !stored.is_empty() => stored != surface_hash,
610                _ => true,
611            };
612            if surface_changed {
613                cache.evict_with_dependents(&[file_path.to_string()]);
614            }
615            let db = self.snapshot_db();
616            let ref_locs = extract_reference_locations(&db, &file);
617            cache.put(
618                file_path,
619                h,
620                surface_hash,
621                all_issues.as_slice().into(),
622                ref_locs,
623            );
624        }
625
626        opts.apply(&mut all_issues);
627        // Emit `UnusedSuppress` for named suppressions that matched nothing —
628        // present on the cache-hit branch above and on `analyze_paths`, but
629        // missing here, so every non-cached re-analysis (the actual
630        // incremental/LSP-edit pipeline) silently dropped this diagnostic.
631        // `mark_suppressed` above already baked suppression marks into what
632        // gets cached; this only adds the (uncached, always-fresh) unused-
633        // suppression pass on top, mirroring the cache-hit branch's order.
634        self.apply_suppressions_and_emit_unused(&mut all_issues, std::slice::from_ref(&file));
635        AnalysisResult::build(all_issues, HashMap::default(), symbols)
636    }
637
638    /// Collect type definitions only from `paths` into the codebase
639    /// without analyzing method bodies or emitting issues. Used to load
640    /// vendor types.
641    ///
642    /// When a disk-backed cache is attached, per-file `StubSlice` results
643    /// from previous runs are reused on a content-hash match, eliminating
644    /// the parse + definition-collection step. Cache misses run the normal
645    /// pipeline and write back so subsequent runs hit.
646    pub fn collect_definitions(&self, paths: &[PathBuf]) {
647        let _timing = std::env::var("MIR_TIMING").is_ok();
648        let _t0 = std::time::Instant::now();
649
650        let php_v = self.php_version.cache_byte();
651
652        struct FileEntry {
653            file: Arc<str>,
654            src: Arc<str>,
655            hash: [u8; 32],
656            cached: Option<mir_codebase::definitions::StubSlice>,
657        }
658        let entries: Vec<FileEntry> = paths
659            .par_iter()
660            .filter_map(|path| {
661                let src = std::fs::read_to_string(path).ok()?;
662                let file: Arc<str> = Arc::from(path.to_string_lossy().as_ref());
663                let src: Arc<str> = Arc::from(src);
664                let hash = hash_source(&src);
665                let cached = self.db.stub_cache.as_ref().and_then(|c| {
666                    let mut slice = c.get(&file, &hash, php_v)?;
667                    prepare_for_ingest(&mut slice);
668                    Some(slice)
669                });
670                Some(FileEntry {
671                    file,
672                    src,
673                    hash,
674                    cached,
675                })
676            })
677            .collect();
678        let _t_read = _t0.elapsed();
679
680        let source_files: Vec<SourceFile> = {
681            let mut guard = self.db.salsa.write();
682            entries
683                .iter()
684                .map(|e| {
685                    guard.upsert_source_file_with_durability(
686                        e.file.clone(),
687                        e.src.clone(),
688                        salsa::Durability::HIGH,
689                    )
690                })
691                .collect()
692        };
693        let _t_reg = _t0.elapsed();
694
695        let db_pass1 = {
696            let guard = self.db.salsa.read();
697            (**guard).clone()
698        };
699        let stub_cache = self.db.stub_cache.clone();
700        let prepared: Vec<(Arc<str>, mir_codebase::definitions::StubSlice)> = entries
701            .into_par_iter()
702            .zip(source_files.into_par_iter())
703            .map_with(db_pass1, |db, (mut entry, salsa_file)| {
704                if let Some(slice) = entry.cached.take() {
705                    let slice_arc = Arc::new(slice);
706                    db.parse_cache()
707                        .insert(entry.hash, php_v, Arc::clone(&slice_arc));
708                    return (entry.file.clone(), (*slice_arc).clone());
709                }
710                let defs = collect_file_definitions(&*db, salsa_file);
711                if let Some(cache) = stub_cache.as_ref() {
712                    cache.put(&entry.file, &entry.hash, php_v, &defs.slice);
713                }
714                (entry.file.clone(), (*defs.slice).clone())
715            })
716            .collect();
717        let _t_collect = _t0.elapsed();
718        // Commit subtype-index class edges for the vendor tree: without this,
719        // goto-implementation can't surface implementors that live only in
720        // vendor/ (index_batch's project-file warm sweep has the same gap for
721        // project files, but no StubSlice is cheaply available there — see
722        // the persistence work tracked separately).
723        {
724            let guard = self.db.salsa.read();
725            for (file, slice) in &prepared {
726                let entries = crate::db::subtype_index::entries_from_slice(slice);
727                guard.set_file_class_edges(file, entries);
728            }
729        }
730        drop(prepared);
731        let _t_ingest = _t0.elapsed();
732
733        if _timing {
734            let (hits, misses) = self.stub_cache_stats();
735            eprintln!(
736                "[vendor] read={:.0}ms reg={:.0}ms collect={:.0}ms ingest={:.0}ms total={:.0}ms (cache hits={hits} misses={misses})",
737                _t_read.as_secs_f64() * 1000.0,
738                (_t_reg - _t_read).as_secs_f64() * 1000.0,
739                (_t_collect - _t_reg).as_secs_f64() * 1000.0,
740                (_t_ingest - _t_collect).as_secs_f64() * 1000.0,
741                _t_ingest.as_secs_f64() * 1000.0,
742            );
743        }
744
745        {
746            let mut guard = self.db.salsa.write();
747            guard.rebuild_workspace_symbol_index();
748        }
749
750        crate::collector::print_collector_stats();
751    }
752}