Skip to content

Commit d5ce128

Browse files
authored
Add support for finding PHP constants (#37)
This PR updates the crate so it can locate PHP constants defined either with const or with define().
1 parent 07227f7 commit d5ce128

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/query_regex.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ pub fn get_regex_for_query(query: &str, file_type: &FileType) -> Regex {
66
FileType::JS => &format!(
77
r"(?:\b(?:function|var|let|const|class|interface|type)\s+{query}\b|\b{query}\([^)]*\)\s*(?::[^\{{]+)?\{{|\b{query}:|@typedef\s*(?:\{{[^\}}]+\}})?\s*{query}\b)"
88
),
9-
FileType::PHP => &format!(r"\b(?:function|class|trait|interface|enum) {query}\b"),
9+
FileType::PHP => &format!(
10+
r#"\b(?:function|class|trait|interface|enum|const) {query}\b|define\s*\(\s*['"]{query}"#
11+
),
1012
FileType::RS => &format!(r"\b(?:fn|trait|enum|struct|mod) {query}\b"),
1113
};
1214
Regex::new(regexp_string).expect("Could not create regex for file type query")

tests/fixtures/by-language/php-fixture.php

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ public function doSomething(): Foo {
2626
public function doSomethingAbsolute(): \Home\Foo {
2727
}
2828
}
29+
30+
const GLOBALCONSTANT = 77;
31+
define( 'GLOBALDEFINESINGLE', 42 );
32+
define( "GLOBALDEFINEDOUBLE", 43 );
33+
34+
class ClassWithConstant {
35+
public const MYCONSTANT = 5;
36+
37+
public function echoConstant(): void {
38+
echo self::MYCONSTANT;
39+
echo GLOBALCONSTANT;
40+
echo GLOBALDEFINE;
41+
}
42+
}

tests/integration_test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ fn search_returns_matching_js_function_line_with_filetype_alias(#[case] file_typ
287287
#[case(String::from("MyEnum"), String::from("php"), 20)]
288288
#[case(String::from("doSomething"), String::from("php"), 24)]
289289
#[case(String::from("doSomethingAbsolute"), String::from("php"), 26)]
290+
#[case::php_global_constant(String::from("GLOBALCONSTANT"), String::from("php"), 30)]
291+
#[case::php_global_define_single(String::from("GLOBALDEFINESINGLE"), String::from("php"), 31)]
292+
#[case::php_global_define_double(String::from("GLOBALDEFINEDOUBLE"), String::from("php"), 32)]
293+
#[case::php_global_class_constant(String::from("MYCONSTANT"), String::from("php"), 35)]
290294
#[case(String::from("query_db"), String::from("rs"), 1)]
291295
#[case(String::from("public_func"), String::from("rs"), 6)]
292296
#[case(String::from("Wrapper"), String::from("rs"), 4)]

0 commit comments

Comments
 (0)