MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / parse_phpdoc_template_params

Function parse_phpdoc_template_params

internal/cbm/lsp/php_lsp.c:3390–3439  ·  view source on GitHub ↗

Parse a class-level docblock for @property / @method tags and register * them as virtual fields and methods on the class. Used heavily by * Eloquent and other dynamic-PHP frameworks. * * @property Foo $bar → field bar: Foo * @property-read Foo $bar → same (treat the same as @property) * @method Bar foo() → method foo() returning Bar * @method st

Source from the content-addressed store, hash-verified

3388/* Parse `@template T` / `@template T of Foo` lines and return the
3389 * NULL-terminated array of type-parameter names. */
3390static const char **parse_phpdoc_template_params(PHPLSPContext *ctx, const char *docstring) {
3391 if (!docstring)
3392 return NULL;
3393 const char *names[16];
3394 int n = 0;
3395 const char *p = docstring;
3396 while ((p = strstr(p, "@template")) != NULL) {
3397 p += 9;
3398 /* Skip variants that bind a type-param name with a kind suffix:
3399 * @template-covariant T → record T
3400 * @template-contravariant T → record T
3401 * but NOT @template-extends / @template-implements (those declare
3402 * a parent class, not a type param). */
3403 bool is_param_variant = true;
3404 if (*p == '-') {
3405 const char *suffix = p + 1;
3406 if (strncmp(suffix, "covariant", 9) == 0) {
3407 p += 1 + 9;
3408 } else if (strncmp(suffix, "contravariant", 13) == 0) {
3409 p += 1 + 13;
3410 } else {
3411 /* @template-extends / -implements / -use / etc. — skip line. */
3412 is_param_variant = false;
3413 while (*p && *p != ' ' && *p != '\t' && *p != '\n')
3414 p++;
3415 }
3416 }
3417 if (!is_param_variant)
3418 continue;
3419 while (*p == ' ' || *p == '\t')
3420 p++;
3421 const char *name_start = p;
3422 while (*p && (isalnum((unsigned char)*p) || *p == '_'))
3423 p++;
3424 if (p == name_start)
3425 continue;
3426 if (n < 16) {
3427 names[n++] = cbm_arena_strndup(ctx->arena, name_start, (size_t)(p - name_start));
3428 }
3429 }
3430 if (n == 0)
3431 return NULL;
3432 const char **out = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(n + 1) * sizeof(*out));
3433 if (!out)
3434 return NULL;
3435 for (int i = 0; i < n; i++)
3436 out[i] = names[i];
3437 out[n] = NULL;
3438 return out;
3439}
3440
3441/* Substitute the type parameters in `t` according to a parallel
3442 * (param_names, args) mapping, returning a new arena-allocated CBMType. */

Callers 1

apply_phpdoc_class_tagsFunction · 0.85

Calls 2

cbm_arena_strndupFunction · 0.50
cbm_arena_allocFunction · 0.50

Tested by

no test coverage detected