* Whether an unqualified `dirname(...)` in this file reaches a function other * than PHP's: one a `use function` imports under that name (`use function * Foo\\dirname;`, `Foo\\{dirname}`, `Foo\\bar, Foo\\dirname`; not the global * `use function dirname;`, nor one renamed `as dn`), or one the file
(node: SyntaxNode, source: string)
| 89 | * neither. Read once per file. |
| 90 | */ |
| 91 | function phpShadowsDirname(node: SyntaxNode, source: string): boolean { |
| 92 | // Keyed on the tree: each read of `tree.rootNode` hands back a new wrapper. |
| 93 | const root = node.tree.rootNode; |
| 94 | const cached = PHP_DIRNAME_SHADOWED.get(node.tree); |
| 95 | if (cached !== undefined) return cached; |
| 96 | let shadowed = false; |
| 97 | for (const use of root.descendantsOfType('namespace_use_declaration')) { |
| 98 | const text = getNodeText(use, source).replace(/\s+/g, ' ').replace(/\s*([\\{},])\s*/g, '$1').trim(); |
| 99 | // `use function A\b;` imports functions throughout; a group `use A\{B, |
| 100 | // function c}` marks each function clause on its own. |
| 101 | const m = /^use (function )?(.*?);?$/i.exec(text); |
| 102 | if (!m) continue; |
| 103 | const allFunctions = m[1] !== undefined; |
| 104 | const group = /^([\w\\]*)\{(.*)\}$/.exec(m[2]!.trim()); |
| 105 | if (!allFunctions && !group) continue; |
| 106 | const prefix = group ? group[1]!.replace(/\\$/, '') : ''; |
| 107 | for (const raw of (group ? group[2]! : m[2]!).split(',')) { |
| 108 | const fn = /^function /i.exec(raw.trim()); |
| 109 | if (!allFunctions && !fn) continue; |
| 110 | const clause = fn ? raw.trim().slice(fn[0].length) : raw; |
| 111 | const [name = '', alias] = clause.trim().split(/\s+as\s+/i); |
| 112 | const fqn = (prefix ? `${prefix}\\${name}` : name).replace(/^\\/, ''); |
| 113 | const local = alias ?? fqn.split('\\').pop()!; |
| 114 | if (local.toLowerCase() === 'dirname' && fqn.toLowerCase() !== 'dirname') shadowed = true; |
| 115 | } |
| 116 | } |
| 117 | if (!shadowed && root.descendantsOfType('namespace_definition').length > 0) { |
| 118 | shadowed = root.descendantsOfType('function_definition').some((fn) => { |
| 119 | const name = getChildByField(fn, 'name'); |
| 120 | return !!name && getNodeText(name, source).toLowerCase() === 'dirname'; |
| 121 | }); |
| 122 | } |
| 123 | PHP_DIRNAME_SHADOWED.set(node.tree, shadowed); |
| 124 | return shadowed; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * `$_SERVER['DOCUMENT_ROOT']`, in either quote and with any spacing inside the |
no test coverage detected