Skip to content

Commit

Permalink
Add better require support
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 14, 2024
1 parent 31c38b6 commit 8d5bfb9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/analyzer/expr/const_fetch_analyzer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/code_info/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 8d5bfb9

Please sign in to comment.