* Extract a static string-literal path from a PHP include/require expression. * * Returns null for dynamic forms (`include $var`, `require __DIR__ . '/x'`, * interpolated strings) — they have no resolvable compile-time path, which * matches the issue's "static string literals (the common case)"
(node: SyntaxNode, source: string)
| 20 | * matches the issue's "static string literals (the common case)" scope. |
| 21 | */ |
| 22 | function phpStaticIncludePath(node: SyntaxNode, source: string): string | null { |
| 23 | // The path argument is the expression's first named child; the call-style |
| 24 | // form `require("x")` wraps it in a parenthesized_expression. |
| 25 | let arg: SyntaxNode | null = node.namedChild(0); |
| 26 | if (arg?.type === 'parenthesized_expression') arg = arg.namedChild(0); |
| 27 | if (!arg || (arg.type !== 'string' && arg.type !== 'encapsed_string')) return null; |
| 28 | // Pure literal only: any non-`string_content` child (interpolated variable, |
| 29 | // escape sequence, …) means the value isn't a static path. |
| 30 | const parts = arg.namedChildren; |
| 31 | if (parts.some((c: SyntaxNode) => c.type !== 'string_content')) return null; |
| 32 | const content = parts.find((c: SyntaxNode) => c.type === 'string_content'); |
| 33 | return content ? getNodeText(content, source) : null; |
| 34 | } |
| 35 | |
| 36 | /** PHP built-in return types that can't be a method receiver (so no class to chain on). */ |
| 37 | const PHP_NON_CLASS_RETURN = new Set([ |