Now that we have a basic Rule API setup, we can start to implement some basic analysis rules. We need to create a new struct and implement the Rule trait on it. #[derive(Debug)] pub struct ValidFunctionRule; impl Rule for ValidFunctionRule { fn should_run(&self, node: &dyn Node) -> bool { downcast:: (node).is_some()} fn run(&mut self, node: &mut dyn Node, definitions: &DefinitionCollection) { todo!()} }

pub trait Rule: Debug { fn should_run(&self, node: &dyn Node) -> bool; fn run(&mut self, node: &mut dyn Node, definitions: &DefinitionCollection, messages: &mut MessageCollector); }

, } impl Analyser { pub fn new(definitions: DefinitionCollection) -> Self { Self { rules: Vec::new(), definitions, message_collector: MessageCollector::default(), context_stack: Vec::new(), }} pub fn analyse(&mut self, file: String, contents: &[u8]) -> MessageCollector { self.message_collector = MessageCollector::new(file); let parse_result = parse(contents); if let Err(error) = parse_result { self.message_collector.add(error.to_string()); return self.message_collector.clone(); } let mut ast = parse_result.unwrap(); self.context_stack.push(Context::new()); self.visit_node(&mut ast).unwrap(); return self.message_collector.clone(); } pub fn add_rule(&mut self, rule: Box ) { self.rules.push(rule); }} impl Visitor for Analyser { fn visit(&mut self, node: &mut dyn Node) -> Result { let mut context = self.context_stack.last_mut().unwrap(); for rule in &mut self.rules { if rule.should_run(node) { rule.run(node, &self.definitions, &mut self.message_collector, &mut context); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners