From 8d5bfb98482517d156c0967377f6e7f93372ccf5 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 14 Feb 2024 10:41:51 -0500 Subject: [PATCH] Add better require support --- src/analyzer/expr/const_fetch_analyzer.rs | 21 ++++++++++++++++++--- src/code_info/lib.rs | 12 ++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/analyzer/expr/const_fetch_analyzer.rs b/src/analyzer/expr/const_fetch_analyzer.rs index 28c732c0..19a1b88b 100644 --- a/src/analyzer/expr/const_fetch_analyzer.rs +++ b/src/analyzer/expr/const_fetch_analyzer.rs @@ -1,7 +1,10 @@ +use std::path::Path; use std::rc::Rc; use hakana_reflection_info::issue::Issue; use hakana_reflection_info::issue::IssueKind; +use hakana_reflection_info::StrId; +use hakana_type::get_literal_string; use hakana_type::get_mixed_any; use hakana_type::get_string; use hakana_type::type_expander; @@ -46,10 +49,22 @@ pub(crate) fn analyze( get_mixed_any() } } else { - let constant_name = statements_analyzer.get_interner().lookup(name); - match constant_name { - "__FILE__" | "__DIR__" | "__FUNCTION__" => get_string(), + match name { + &StrId::FILE_CONST => { + get_literal_string(statements_analyzer.get_file_path_actual().to_string()) + } + &StrId::DIR_CONST => { + let path = Path::new(statements_analyzer.get_file_path_actual()); + if let Some(dir) = path.parent() { + get_literal_string(dir.to_str().unwrap().to_owned()) + } else { + get_string() + } + } + &StrId::FUNCTION_CONST => get_string(), _ => { + let constant_name = statements_analyzer.get_interner().lookup(name); + analysis_data.maybe_add_issue( Issue::new( IssueKind::NonExistentConstant, diff --git a/src/code_info/lib.rs b/src/code_info/lib.rs index 6dda582b..dd1adeb7 100644 --- a/src/code_info/lib.rs +++ b/src/code_info/lib.rs @@ -97,6 +97,12 @@ impl StrId { pub const KEY_EXISTS: StrId = StrId(39); pub const REMOVE_KEY: StrId = StrId(40); pub const MATH_INT32_MAX: StrId = StrId(41); + pub const FILE_CONST: StrId = StrId(42); + pub const DIR_CONST: StrId = StrId(43); + pub const FUNCTION_CONST: StrId = StrId(44); + pub const REQUIRE: StrId = StrId(45); + pub const REQUIRE_ONCE: StrId = StrId(46); + pub const INCLUDE_ONCE: StrId = StrId(47); #[inline] pub fn is_empty(&self) -> bool { @@ -165,6 +171,12 @@ impl Default for Interner { interner.intern("keyExists".to_string()); interner.intern("removeKey".to_string()); interner.intern("HH\\Lib\\Math\\INT32_MAX".to_string()); + interner.intern("__FILE__".to_string()); + interner.intern("__DIR__".to_string()); + interner.intern("__FUNCTION__".to_string()); + interner.intern("require".to_string()); + interner.intern("require_once".to_string()); + interner.intern("include_once".to_string()); interner } }