mago_semantics/lib.rs
1use mago_database::file::File;
2use mago_names::ResolvedNames;
3use mago_php_version::PHPVersion;
4use mago_reporting::IssueCollection;
5use mago_syntax::ast::Program;
6use mago_syntax::walker::Walker;
7
8use crate::internal::CheckingWalker;
9use crate::internal::context::Context;
10
11mod internal;
12
13/// The main entry point for performing semantic analysis on a PHP program's AST.
14///
15/// This checker is responsible for traversing the Abstract Syntax Tree (AST)
16/// and validating the code against a set of semantic rules, such as type correctness,
17/// variable usage, and adherence to language features for a specific PHP version.
18#[derive(Debug, Clone, Copy)]
19pub struct SemanticsChecker {
20 version: PHPVersion,
21}
22
23impl SemanticsChecker {
24 /// Creates a new `SemanticsChecker`.
25 ///
26 /// # Arguments
27 ///
28 /// - `php_version`: The target PHP version to check against.
29 ///
30 #[must_use]
31 pub fn new(php_version: PHPVersion) -> Self {
32 Self { version: php_version }
33 }
34
35 /// Analyzes the given program AST for semantic issues.
36 ///
37 /// This method walks the entire AST, applying semantic rules and collecting any
38 /// violations it finds.
39 ///
40 /// # Arguments
41 ///
42 /// - `source`: The source file being analyzed.
43 /// - `program`: The root of the AST for the program.
44 /// - `names`: The resolved names (e.g., fully qualified class names) from the name resolution pass.
45 ///
46 /// # Returns
47 ///
48 /// An `IssueCollection` containing all semantic issues discovered during the check.
49 #[must_use]
50 pub fn check<'ast, 'arena>(
51 &self,
52 file: &File,
53 program: &'ast Program<'arena>,
54 names: &'ast ResolvedNames<'arena>,
55 ) -> IssueCollection {
56 let mut context = Context::new(self.version, program, names, file);
57
58 CheckingWalker.walk_program(program, &mut context);
59
60 context.finalize()
61 }
62}