MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / generate_constant_name

Function generate_constant_name

src/code_actions/extract_constant.rs:205–257  ·  view source on GitHub ↗

Generate a SCREAMING_SNAKE_CASE constant name from a literal value.

(value: &str)

Source from the content-addressed store, hash-verified

203
204/// Generate a SCREAMING_SNAKE_CASE constant name from a literal value.
205fn generate_constant_name(value: &str) -> String {
206 let trimmed = value.trim();
207 let lower = trimmed.to_ascii_lowercase();
208
209 // Boolean
210 if lower == "true" {
211 return "IS_ENABLED".to_string();
212 }
213 if lower == "false" {
214 return "IS_DISABLED".to_string();
215 }
216 if lower == "null" {
217 return "DEFAULT_VALUE".to_string();
218 }
219
220 // String literal — strip quotes and convert to SCREAMING_SNAKE_CASE
221 if (trimmed.starts_with('\'') && trimmed.ends_with('\''))
222 || (trimmed.starts_with('"') && trimmed.ends_with('"'))
223 {
224 let inner = &trimmed[1..trimmed.len() - 1];
225 let name = string_to_screaming_snake(inner);
226 if !name.is_empty() && name.chars().next().is_some_and(|c| c.is_ascii_alphabetic()) {
227 return name;
228 }
229 return "CONSTANT".to_string();
230 }
231
232 // Negative numeric
233 if let Some(stripped) = trimmed.strip_prefix('-') {
234 let abs = stripped.trim_start();
235 if abs.contains('.') || abs.contains('e') || abs.contains('E') {
236 return "VALUE".to_string();
237 }
238 return format!("VALUE_{}", abs.replace('_', ""));
239 }
240
241 // Numeric literal
242 if is_numeric_literal(trimmed) {
243 // Float
244 if trimmed.contains('.') || trimmed.contains('e') || trimmed.contains('E') {
245 return "VALUE".to_string();
246 }
247 // Integer — use VALUE_NNN
248 return format!("VALUE_{}", trimmed.replace('_', ""));
249 }
250
251 // Concatenated string expression — try to use the first segment
252 if is_concat_expression(trimmed) {
253 return "CONSTANT".to_string();
254 }
255
256 "CONSTANT".to_string()
257}
258
259/// Determine the PHP type name for a literal value.
260///

Callers 1

Calls 6

is_numeric_literalFunction · 0.85
is_concat_expressionFunction · 0.85
nextMethod · 0.80
containsMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected