Skip to main content

mir_analyzer/
lib.rs

1use rustc_hash::FxHashMap;
2
3pub(crate) mod analyzer_db;
4pub(crate) mod attributes;
5pub mod batch;
6pub(crate) mod body_analysis;
7#[doc(hidden)]
8pub mod cache;
9pub(crate) mod call;
10pub(crate) mod class;
11pub(crate) mod collector;
12pub(crate) mod contradiction;
13#[doc(hidden)]
14pub mod db;
15pub(crate) mod dead_code;
16pub(crate) mod diagnostics;
17pub(crate) mod expr;
18pub mod file_analyzer;
19pub(crate) mod flow_state;
20pub(crate) mod generic;
21pub mod indexing;
22#[doc(hidden)]
23pub mod metrics;
24pub(crate) mod narrowing;
25#[doc(hidden)]
26pub mod parse_cache;
27#[doc(hidden)]
28pub mod parser;
29pub mod php_version;
30pub mod prelude;
31pub mod session;
32pub mod source_provider;
33pub(crate) mod stmt;
34#[doc(hidden)]
35pub mod stub_cache;
36#[doc(hidden)]
37pub mod stubs;
38pub(crate) mod subtype;
39pub mod suppression;
40pub(crate) mod taint;
41pub(crate) mod type_env;
42pub(crate) mod util;
43
44pub use batch::{
45    analyze_source, dead_code_issue_kinds, discover_files, AnalysisResult, BatchOptions,
46};
47pub use file_analyzer::{BatchFileAnalyzer, FileAnalysis, FileAnalyzer, ParsedFile};
48pub use indexing::{IndexBatchOutcome, IndexCancel, IndexParallelism};
49pub use parser::type_from_hint::type_from_hint;
50pub use parser::{DocblockParser, ParsedDocblock};
51pub use php_version::{ParsePhpVersionError, PhpVersion};
52pub use session::AnalysisSession;
53pub use source_provider::{FsSourceProvider, SourceProvider};
54
55/// Returns `Some((used, canonical))` when `written` and `canonical` FQCNs differ only in casing.
56/// Uses the short (last-segment) form when only the final segment is wrong and the namespace
57/// prefix is already correct; otherwise returns the full path so the mismatch is visible.
58pub(crate) fn fqcn_case_mismatch(written: &str, canonical: &str) -> Option<(String, String)> {
59    let w = written.trim_start_matches('\\');
60    let c = canonical.trim_start_matches('\\');
61    if w == c || !w.eq_ignore_ascii_case(c) {
62        return None;
63    }
64    let w_last = w.rsplit('\\').next().unwrap_or(w);
65    let c_last = c.rsplit('\\').next().unwrap_or(c);
66    if w_last != c_last {
67        let w_prefix = w.rsplit_once('\\').map_or("", |(p, _)| p);
68        let c_prefix = c.rsplit_once('\\').map_or("", |(p, _)| p);
69        if w_prefix == c_prefix {
70            return Some((w_last.to_string(), c_last.to_string()));
71        }
72    }
73    Some((w.to_string(), c.to_string()))
74}
75pub use stubs::{
76    is_builtin_function, stub_files, stub_path_for_class, ChainedClassResolver, StubClassResolver,
77    StubVfs,
78};
79
80// ============================================================================
81// Analysis entry points
82// ============================================================================
83//
84// `AnalysisSession` is the single analysis engine. It supports two usage modes:
85//
86// - Batch (CLI, CI, bulk analysis): use `analyze_paths` / `BatchOptions` to
87//   run definition collection and body analysis over many files in parallel.
88//
89// - Incremental (LSP, watch mode): ingest files as they change; per-file
90//   results come from `FileAnalyzer::analyze`. Builder-style configuration
91//   (`with_cache`, `with_psr4`, …).
92//
93// The two phases of analysis are:
94//   1. Definition collection — discovers classes, functions, constants in a
95//      file and registers them in the salsa database.
96//   2. Body analysis (`BodyAnalyzer`) — walks function/method bodies,
97//      inferring types and emitting issues.
98
99/// A position in source code: 1-based line, 0-based codepoint column.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
101pub struct Position {
102    pub line: u32,
103    pub column: u32,
104}
105
106/// A range in source code: start and end positions.
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
108pub struct Range {
109    pub start: Position,
110    pub end: Position,
111}
112
113/// A semantic identifier for a code entity that the analyzer can resolve.
114///
115/// Replaces the previous stringly-typed `&str` keys. Method names are
116/// normalized (lowercased) at construction since PHP method dispatch is
117/// case-insensitive — this prevents a class of correctness bugs where
118/// callers pass mixed-case names and get empty results.
119#[derive(Debug, Clone, PartialEq, Eq, Hash)]
120pub enum Name {
121    /// A class, interface, trait, or enum (FQCN).
122    Class(std::sync::Arc<str>),
123    /// A global function (FQN).
124    Function(std::sync::Arc<str>),
125    /// An instance or static method.
126    Method {
127        class: std::sync::Arc<str>,
128        name: std::sync::Arc<str>,
129    },
130    /// A class property.
131    Property {
132        class: std::sync::Arc<str>,
133        name: std::sync::Arc<str>,
134    },
135    /// A class / interface / enum constant.
136    ClassConstant {
137        class: std::sync::Arc<str>,
138        name: std::sync::Arc<str>,
139    },
140    /// A global constant.
141    GlobalConstant(std::sync::Arc<str>),
142}
143
144impl Name {
145    /// Construct a method symbol. Normalizes `name` to lowercase since PHP
146    /// methods are case-insensitive.
147    pub fn method(class: impl Into<std::sync::Arc<str>>, name: &str) -> Self {
148        Name::Method {
149            class: class.into(),
150            name: std::sync::Arc::from(name.to_ascii_lowercase()),
151        }
152    }
153
154    /// Construct a class symbol.
155    pub fn class(fqcn: impl Into<std::sync::Arc<str>>) -> Self {
156        Name::Class(fqcn.into())
157    }
158
159    /// Construct a function symbol.
160    pub fn function(fqn: impl Into<std::sync::Arc<str>>) -> Self {
161        Name::Function(fqn.into())
162    }
163
164    /// Construct a property symbol.
165    pub fn property(
166        class: impl Into<std::sync::Arc<str>>,
167        name: impl Into<std::sync::Arc<str>>,
168    ) -> Self {
169        Name::Property {
170            class: class.into(),
171            name: name.into(),
172        }
173    }
174
175    /// Construct a class constant symbol.
176    pub fn class_constant(
177        class: impl Into<std::sync::Arc<str>>,
178        name: impl Into<std::sync::Arc<str>>,
179    ) -> Self {
180        Name::ClassConstant {
181            class: class.into(),
182            name: name.into(),
183        }
184    }
185
186    /// Construct a global constant symbol.
187    pub fn global_constant(fqn: impl Into<std::sync::Arc<str>>) -> Self {
188        Name::GlobalConstant(fqn.into())
189    }
190
191    /// The codebase lookup key for this symbol (used internally for the
192    /// reference-locations index). Stable across releases.
193    pub fn codebase_key(&self) -> String {
194        match self {
195            Name::Class(fqcn) => fqcn.to_string(),
196            Name::Function(fqn) => fqn.to_string(),
197            Name::Method { class, name } => format!("{class}::{name}"),
198            Name::Property { class, name } => format!("{class}::{name}"),
199            Name::ClassConstant { class, name } => format!("{class}::{name}"),
200            Name::GlobalConstant(fqn) => fqn.to_string(),
201        }
202    }
203}
204
205/// Reason a symbol lookup did not return a location.
206#[derive(Debug, Clone, PartialEq, Eq)]
207pub enum SymbolLookupError {
208    /// No such symbol exists in the codebase.
209    NotFound,
210    /// The symbol exists but has no recorded source location (e.g. a
211    /// stub-only declaration without a span).
212    NoSourceLocation,
213}
214
215/// Outcome of a [`AnalysisSession::load_class`] attempt.
216#[derive(Debug, Clone, Copy, PartialEq, Eq)]
217pub enum LoadOutcome {
218    /// The symbol was already present in the session; no work performed.
219    AlreadyLoaded,
220    /// The symbol was resolved by the configured [`ClassResolver`] and the
221    /// defining file was ingested.
222    Loaded,
223    /// No resolver is configured, the resolver could not map the FQCN to a
224    /// file, or the resolved file could not be read / did not define the
225    /// requested symbol.
226    NotResolvable,
227}
228
229impl LoadOutcome {
230    /// `true` when the symbol is now present in the session (whether it was
231    /// already there or just freshly loaded).
232    pub fn is_loaded(self) -> bool {
233        !matches!(self, LoadOutcome::NotResolvable)
234    }
235}
236
237/// Pluggable strategy for mapping a fully-qualified class name to the file
238/// that should define it. The analyzer never touches `vendor/` or the
239/// filesystem on its own — it asks a `ClassResolver` when a symbol is needed.
240///
241/// `mir_analyzer::Psr4Map` is the built-in implementation for Composer-based
242/// projects. Consumers with non-Composer conventions (WordPress, Drupal, a
243/// custom autoloader, a workspace-walk index) supply their own.
244pub trait ClassResolver: Send + Sync {
245    /// Resolve `fqcn` to the file that defines it. Returning `None` causes
246    /// the analyzer to fall back to emitting `UndefinedClass`.
247    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf>;
248}
249
250impl ClassResolver for composer::Psr4Map {
251    fn resolve(&self, fqcn: &str) -> Option<std::path::PathBuf> {
252        composer::Psr4Map::resolve(self, fqcn)
253    }
254}
255
256impl std::fmt::Display for SymbolLookupError {
257    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
258        match self {
259            SymbolLookupError::NotFound => write!(f, "symbol not found"),
260            SymbolLookupError::NoSourceLocation => write!(f, "symbol has no source location"),
261        }
262    }
263}
264
265impl std::error::Error for SymbolLookupError {}
266
267/// Hover information for a symbol at a source location.
268/// Includes the inferred type, optional docstring, and location of definition.
269#[derive(Debug, Clone)]
270pub struct HoverInfo {
271    /// Inferred type of the symbol.
272    pub ty: Type,
273    /// Docstring / documentation comment for the symbol (if available).
274    pub docstring: Option<String>,
275    /// Source location of the symbol's definition.
276    pub definition: Option<mir_types::Location>,
277}
278
279/// File dependency graph: tracks which files depend on which other files.
280/// Used for incremental invalidation in LSP servers and build systems.
281#[derive(Debug, Clone)]
282pub struct DependencyGraph {
283    /// Direct dependencies: file → [files it depends on]
284    dependencies: FxHashMap<String, Vec<String>>,
285    /// Reverse dependencies: file → [files that depend on it]
286    dependents: FxHashMap<String, Vec<String>>,
287}
288
289impl DependencyGraph {
290    /// Files that `file` directly depends on (imports, parent classes, interfaces, traits).
291    pub fn dependencies_of(&self, file: &str) -> &[String] {
292        self.dependencies
293            .get(file)
294            .map(|v| v.as_slice())
295            .unwrap_or(&[])
296    }
297
298    /// Files that directly depend on `file` (reverse edge).
299    pub fn dependents_of(&self, file: &str) -> &[String] {
300        self.dependents
301            .get(file)
302            .map(|v| v.as_slice())
303            .unwrap_or(&[])
304    }
305
306    /// All files transitively depended upon by `file` (including indirect).
307    pub fn transitive_dependencies(&self, file: &str) -> Vec<String> {
308        let mut visited = rustc_hash::FxHashSet::default();
309        let mut queue = vec![file.to_string()];
310        let mut result = Vec::new();
311
312        while let Some(current) = queue.pop() {
313            if !visited.insert(current.clone()) {
314                continue;
315            }
316            for dep in self.dependencies_of(&current) {
317                if !visited.contains(dep) {
318                    queue.push(dep.clone());
319                    result.push(dep.clone());
320                }
321            }
322        }
323        result
324    }
325
326    /// All files that transitively depend on `file` (reverse transitive).
327    pub fn transitive_dependents(&self, file: &str) -> Vec<String> {
328        let mut visited = rustc_hash::FxHashSet::default();
329        let mut queue = vec![file.to_string()];
330        let mut result = Vec::new();
331
332        while let Some(current) = queue.pop() {
333            if !visited.insert(current.clone()) {
334                continue;
335            }
336            for dep in self.dependents_of(&current) {
337                if !visited.contains(dep) {
338                    queue.push(dep.clone());
339                    result.push(dep.clone());
340                }
341            }
342        }
343        result
344    }
345}
346
347pub mod symbol;
348pub use mir_codebase::storage::{FnParam, TemplateParam, Visibility};
349pub use mir_issues::{Issue, IssueKind, Severity};
350pub use mir_types::Type;
351
352/// Convert a parser [`php_ast::Span`] (byte-offset range) into a
353/// [`mir_types::Location`] (file path + 1-based line range +
354/// 0-based codepoint columns) using `source` and the parser's `source_map`.
355///
356/// This is the canonical way for consumers to translate body-analysis result spans
357/// (e.g. [`crate::symbol::ResolvedSymbol::span`]) into source locations they
358/// can hand to their own protocol layer. Consumers that need different
359/// position semantics (LSP UTF-16 code units, byte offsets, etc.) translate
360/// from this `Location` rather than re-implementing the column math.
361pub fn location_from_span(
362    span: php_ast::Span,
363    file: std::sync::Arc<str>,
364    source: &str,
365    source_map: &php_rs_parser::source_map::SourceMap,
366) -> mir_types::Location {
367    let (line, col_start) = diagnostics::offset_to_line_col(source, span.start, source_map);
368    let (line_end, col_end) = if span.start < span.end {
369        diagnostics::offset_to_line_col(source, span.end, source_map)
370    } else {
371        (line, col_start)
372    };
373    mir_types::Location {
374        file,
375        line,
376        line_end,
377        col_start,
378        col_end: col_end.max(col_start.saturating_add(1)),
379    }
380}
381pub use symbol::{DeclarationKind, DocumentSymbol, ReferenceKind, ResolvedSymbol};
382
383pub mod composer;
384pub use composer::{ComposerError, Psr4Map};
385pub use type_env::ScopeId;
386
387#[doc(hidden)]
388pub mod test_utils;