Skip to content

Commit 69effc6

Browse files
committed
Change the case of method identifiers to comply with PSR-1
1 parent 1483ef7 commit 69effc6

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

ext-php-rs-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ proc-macro = true
1414
[dependencies]
1515
syn = { version = "1.0.68", features = ["full", "extra-traits"] }
1616
darling = "0.12"
17+
ident_case = "1.0.1"
1718
quote = "1.0.9"
1819
proc-macro2 = "1.0.26"
1920
lazy_static = "1.4.0"

ext-php-rs-derive/src/method.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::{anyhow, bail, Result};
2+
use ident_case::RenameRule;
23
use quote::ToTokens;
34
use std::collections::HashMap;
45

@@ -34,6 +35,29 @@ pub struct Method {
3435
pub visibility: Visibility,
3536
}
3637

38+
fn camel_case_identifier(field: &str) -> String {
39+
match field {
40+
"__construct" => "__construct".to_string(),
41+
"__destruct" => "__destruct".to_string(),
42+
"__call" => "__call".to_string(),
43+
"__call_static" => "__callStatic".to_string(),
44+
"__get" => "__get".to_string(),
45+
"__set" => "__set".to_string(),
46+
"__isset" => "__isset".to_string(),
47+
"__unset" => "__unset".to_string(),
48+
"__sleep" => "__sleep".to_string(),
49+
"__wakeup" => "__wakeup".to_string(),
50+
"__serialize" => "__serialize".to_string(),
51+
"__unserialize" => "__unserialize".to_string(),
52+
"__to_string" => "__toString".to_string(),
53+
"__invoke" => "__invoke".to_string(),
54+
"__set_state" => "__set_state".to_string(),
55+
"__clone" => "__clone".to_string(),
56+
"__debug_info" => "__debugInfo".to_string(),
57+
field => RenameRule::CamelCase.apply_to_field(field),
58+
}
59+
}
60+
3761
pub fn parser(input: &mut ImplItemMethod) -> Result<(TokenStream, Method)> {
3862
let mut defaults = HashMap::new();
3963
let mut optional = None;
@@ -93,7 +117,7 @@ pub fn parser(input: &mut ImplItemMethod) -> Result<(TokenStream, Method)> {
93117
};
94118

95119
let method = Method {
96-
name: ident.to_string(),
120+
name: camel_case_identifier(&ident.to_string()),
97121
ident: internal_ident.to_string(),
98122
args,
99123
optional,
@@ -247,3 +271,34 @@ impl Method {
247271
.to_token_stream()
248272
}
249273
}
274+
275+
#[cfg(test)]
276+
mod tests {
277+
use super::camel_case_identifier;
278+
279+
#[test]
280+
fn test_rename_php_methods() {
281+
for &(original, expected) in &[
282+
("__construct", "__construct"),
283+
("__destruct", "__destruct"),
284+
("__call", "__call"),
285+
("__call_static", "__callStatic"),
286+
("__get", "__get"),
287+
("__set", "__set"),
288+
("__isset", "__isset"),
289+
("__unset", "__unset"),
290+
("__sleep", "__sleep"),
291+
("__wakeup", "__wakeup"),
292+
("__serialize", "__serialize"),
293+
("__unserialize", "__unserialize"),
294+
("__to_string", "__toString"),
295+
("__invoke", "__invoke"),
296+
("__set_state", "__set_state"),
297+
("__clone", "__clone"),
298+
("__debug_info", "__debugInfo"),
299+
("get_name", "getName"),
300+
] {
301+
assert_eq!(camel_case_identifier(original), expected);
302+
}
303+
}
304+
}

0 commit comments

Comments
 (0)