MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / php_use_shadows_dirname

Function php_use_shadows_dirname

codegraph-kernel/src/php.rs:1729–1790  ·  view source on GitHub ↗

One `namespace_use_declaration`'s part of phpShadowsDirname (#3298): its text with JS whitespace runs collapsed to one space, the space beside `\ { } ,` dropped, and trimmed. `use function A\b, C\d;` imports each listed function, a group `use A\{B, function c}` each `function` clause. True when one lands on the local name `dirname` (any letter case) from a name other than the global `dirname`: `Fo

(raw: &str)

Source from the content-addressed store, hash-verified

1727/// name other than the global `dirname`: `Foo\dirname`, not `dirname` or
1728/// `Foo\dirname as dn`.
1729fn php_use_shadows_dirname(raw: &str) -> bool {
1730 // .replace(/\s+/g, ' ')
1731 let mut collapsed: Vec<char> = Vec::with_capacity(raw.len());
1732 for c in raw.chars() {
1733 if !is_js_space(c) {
1734 collapsed.push(c);
1735 } else if collapsed.last() != Some(&' ') {
1736 collapsed.push(' ');
1737 }
1738 }
1739 // .replace(/\s*([\\{},])\s*/g, '$1'): with single spaces, each space
1740 // beside one of those characters goes.
1741 let special = |c: char| matches!(c, '\\' | '{' | '}' | ',');
1742 let mut text = String::with_capacity(raw.len());
1743 for (i, &c) in collapsed.iter().enumerate() {
1744 let beside = (i > 0 && special(collapsed[i - 1]))
1745 || collapsed.get(i + 1).is_some_and(|&next| special(next));
1746 if c == ' ' && beside {
1747 continue;
1748 }
1749 text.push(c);
1750 }
1751 // .trim() (only `' '` is left), then /^use (function )?(.*?);?$/i.
1752 let Some(rest) = strip_prefix_ascii_ci(text.trim_matches(' '), "use ") else { return false };
1753 let (all_functions, rest) = match strip_prefix_ascii_ci(rest, "function ") {
1754 Some(after) => (true, after),
1755 None => (false, rest),
1756 };
1757 let names = rest.strip_suffix(';').unwrap_or(rest);
1758 // /^([\w\\]*)\{(.*)\}$/ (JS ASCII `\w`) on the trimmed names.
1759 let trimmed = names.trim_matches(is_js_space);
1760 let head_len = trimmed
1761 .bytes()
1762 .take_while(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'\\')
1763 .count();
1764 let group = trimmed[head_len..]
1765 .strip_prefix('{')
1766 .and_then(|body| body.strip_suffix('}'))
1767 .map(|body| (&trimmed[..head_len], body));
1768 if !all_functions && group.is_none() {
1769 return false;
1770 }
1771 let prefix = group.map_or("", |(head, _)| head.strip_suffix('\\').unwrap_or(head));
1772 let list = group.map_or(names, |(_, body)| body);
1773 for raw_clause in list.split(',') {
1774 let function_clause = strip_prefix_ascii_ci(raw_clause.trim_matches(is_js_space), "function ");
1775 if !all_functions && function_clause.is_none() {
1776 continue;
1777 }
1778 let clause = function_clause.unwrap_or(raw_clause).trim_matches(is_js_space);
1779 let mut pieces = php_use_alias_re().split(clause);
1780 let name = pieces.next().unwrap_or("");
1781 let alias = pieces.next();
1782 let joined = if prefix.is_empty() { name.to_string() } else { format!("{prefix}\\{name}") };
1783 let fqn = joined.strip_prefix('\\').unwrap_or(&joined);
1784 let local = alias.unwrap_or_else(|| fqn.rsplit('\\').next().unwrap_or(fqn));
1785 if local.to_lowercase() == "dirname" && fqn.to_lowercase() != "dirname" {
1786 return true;

Callers 1

php_shadows_dirnameMethod · 0.85

Calls 9

strip_prefix_ascii_ciFunction · 0.85
php_use_alias_reFunction · 0.85
lenMethod · 0.80
pushMethod · 0.80
bytesMethod · 0.80
mapMethod · 0.80
nextMethod · 0.80
is_js_spaceFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected