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

Function phpBraceOffsetsAsBrackets

src/extraction/languages/php.ts:287–306  ·  view source on GitHub ↗

* PHP before 8.0 also wrote a string offset with braces, `$code{0}` or * `$this->name{0}`. The grammar knows only brackets, and its recovery could * drop the whole function around such an offset (#3299). Outside strings, * comments and heredocs, braces right after a variable or a property name *

(source: string)

Source from the content-addressed store, hash-verified

285 * `{$x}` and `$obj->{$name}` don't match.
286 */
287function phpBraceOffsetsAsBrackets(source: string): string {
288 if (!/(?:\$|->)[A-Za-z_]\w*\{/.test(source)) return source;
289 const code = phpCodeOnly(source);
290 const offset = /(?:\$|->)[A-Za-z_]\w*\{/g;
291 let out: string[] | null = null;
292 for (let m = offset.exec(code); m; m = offset.exec(code)) {
293 const open = m.index + m[0].length - 1;
294 let depth = 0;
295 let close = -1;
296 for (let k = open; k < code.length; k++) {
297 if (code[k] === '{') depth++;
298 else if (code[k] === '}' && --depth === 0) { close = k; break; }
299 }
300 if (close < 0) continue;
301 out ??= source.split('');
302 out[open] = '[';
303 out[close] = ']';
304 }
305 return out ? out.join('') : source;
306}
307
308export const phpExtractor: LanguageExtractor = {
309 preParse: phpBraceOffsetsAsBrackets,

Callers

nothing calls this directly

Calls 3

phpCodeOnlyFunction · 0.85
execMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected