Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(php-lsp): @phpstan-type aliases + Closure::bind + variance + 30 tests

Martin Vogel89531c7501b0
diff --git a/internal/cbm/lsp/php_lsp.c b/internal/cbm/lsp/php_lsp.c
index 62f396fea..fce7172dc 100644
--- a/internal/cbm/lsp/php_lsp.c
+++ b/internal/cbm/lsp/php_lsp.c
@@ -527,6 +527,42 @@ static int parse_phpdoc_template_args(PHPLSPContext *ctx, const char *p,
return count;
}
+/* Add a @phpstan-type alias to ctx (idempotent — keeps first definition). */
+static void php_add_phpstan_alias(PHPLSPContext *ctx, const char *name, const CBMType *type) {
+ if (!ctx || !name || !type) return;
+ for (int i = 0; i < ctx->phpstan_alias_count; i++) {
+ if (strcmp(ctx->phpstan_alias_names[i], name) == 0) return;
+ }
+ if (ctx->phpstan_alias_count >= ctx->phpstan_alias_cap) {
+ int new_cap = ctx->phpstan_alias_cap ? ctx->phpstan_alias_cap * 2 : 8;
+ const char **nn = (const char **)cbm_arena_alloc(ctx->arena,
+ (size_t)(new_cap + 1) * sizeof(*nn));
+ const CBMType **nt = (const CBMType **)cbm_arena_alloc(
+ ctx->arena, (size_t)(new_cap + 1) * sizeof(*nt));
+ if (!nn || !nt) return;
+ for (int i = 0; i < ctx->phpstan_alias_count; i++) {
+ nn[i] = ctx->phpstan_alias_names[i];
+ nt[i] = ctx->phpstan_alias_types[i];
+ }
+ ctx->phpstan_alias_names = nn;
+ ctx->phpstan_alias_types = nt;
+ ctx->phpstan_alias_cap = new_cap;
+ }
+ ctx->phpstan_alias_names[ctx->phpstan_alias_count] = cbm_arena_strdup(ctx->arena, name);
+ ctx->phpstan_alias_types[ctx->phpstan_alias_count] = type;
+ ctx->phpstan_alias_count++;
+}
+
+static const CBMType *php_lookup_phpstan_alias(PHPLSPContext *ctx, const char *name) {
+ if (!ctx || !name) return NULL;
+ for (int i = 0; i < ctx->phpstan_alias_count; i++) {
+ if (strcmp(ctx->phpstan_alias_names[i], name) == 0) {
+ return ctx->phpstan_alias_types[i];
+ }
+ }
+ return NULL;
+}
+
static const CBMType *resolve_phpdoc_type(PHPLSPContext *ctx, const char *type_text) {
if (!type_text || !*type_text) return cbm_type_unknown();
/* Skip whitespace + nullable */
@@ -554,6 +590,12 @@ static const CBMType *resolve_phpdoc_type(PHPLSPContext *ctx, const char *type_t
/* `array{...}` shape — treat as plain array for now. */
if (has_array_shape) return cbm_type_builtin(ctx->arena, "array");
+ /* @phpstan-type alias takes precedence over name resolution. */
+ {
+ const CBMType *aliased = php_lookup_phpstan_alias(ctx, first);
+ if (aliased) return aliased;
+ }
+
if (php_is_builtin_type_name(first)) {
if (has_generic) {
/* `array<T>`, `iterable<T>`, `list<T>`, `callable<...>`. We
@@ -1921,8 +1963,107 @@ static void php_resolve_calls_in_node(PHPLSPContext *ctx, TSNode node) {
} else if (strcmp(kind, "member_call_expression") == 0 ||
strcmp(kind, "nullsafe_member_call_expression") == 0) {
resolve_member_call(ctx, node);
+ /* Closure binding via $f->bindTo($obj). When $f is an inline
+ * closure literal AND the bindTo call's first arg is a typed
+ * variable, walk the closure body with $this rebound. We only
+ * support this for the inline-literal pattern; tracking
+ * Closure-as-value across assignments is deferred (closure
+ * value-typing is Phase 6+ infrastructure). */
+ TSNode obj = ts_node_child_by_field_name(node, "object", 6);
+ TSNode mname = ts_node_child_by_field_name(node, "name", 4);
+ if (!ts_node_is_null(obj) && !ts_node_is_null(mname)) {
+ char *mn = php_node_text(ctx, mname);
+ const char *ok = ts_node_type(obj);
+ if (mn && strcmp(mn, "bindTo") == 0 &&
+ (strcmp(ok, "anonymous_function") == 0 ||
+ strcmp(ok, "arrow_function") == 0 ||
+ strcmp(ok, "parenthesized_expression") == 0)) {
+ /* Resolve the second-arg's class. */
+ TSNode args = ts_node_child_by_field_name(node, "arguments", 9);
+ if (!ts_node_is_null(args)) {
+ uint32_t anc = ts_node_child_count(args);
+ for (uint32_t i = 0; i < anc; i++) {
+ TSNode a = ts_node_child(args, i);
+ if (ts_node_is_null(a) || !ts_node_is_named(a)) continue;
+ TSNode v = a;
+ if (strcmp(ts_node_type(a), "argument") == 0) {
+ uint32_t bnc = ts_node_child_count(a);
+ for (uint32_t j = 0; j < bnc; j++) {
+ TSNode b = ts_node_child(a, j);
+ if (!ts_node_is_null(b) && ts_node_is_named(b)) {
+ v = b;
+ break;
+ }
+ }
+ }
+ const CBMType *t = php_eval_expr_type(ctx, v);
+ if (t && t->kind == CBM_TYPE_NAMED) {
+ const char *saved = ctx->enclosing_class_qn;
+ ctx->enclosing_class_qn = t->data.named.qualified_name;
+ php_resolve_calls_in_node(ctx, obj);
+ ctx->enclosing_class_qn = saved;
+ }
+ break;
+ }
+ }
+ }
+ }
} else if (strcmp(kind, "scoped_call_expression") == 0) {
resolve_static_call(ctx, node);
+ /* Closure binding via Closure::bind($f, $obj). Same shape as
+ * bindTo: rebind $this when the first arg is a closure literal
+ * and the second arg has a known class. */
+ TSNode scope = ts_node_child_by_field_name(node, "scope", 5);
+ TSNode mname2 = ts_node_child_by_field_name(node, "name", 4);
+ if (!ts_node_is_null(scope) && !ts_node_is_null(mname2)) {
+ char *cls = php_node_text(ctx, scope);
+ char *mn = php_node_text(ctx, mname2);
+ bool is_closure_bind = mn && strcmp(mn, "bind") == 0 &&
+ cls && (strcmp(cls, "Closure") == 0 ||
+ strcmp(cls, "\\Closure") == 0);
+ if (is_closure_bind) {
+ TSNode args = ts_node_child_by_field_name(node, "arguments", 9);
+ if (!ts_node_is_null(args)) {
+ TSNode closure_arg;
+ memset(&closure_arg, 0, sizeof(closure_arg));
+ TSNode obj_arg;
+ memset(&obj_arg, 0, sizeof(obj_arg));
+ int seen = 0;
+ uint32_t anc = ts_node_child_count(args);
+ for (uint32_t i = 0; i < anc; i++) {
+ TSNode a = ts_node_child(args, i);
+ if (ts_node_is_null(a) || !ts_node_is_named(a)) continue;
+ TSNode inner = a;
+ if (strcmp(ts_node_type(a), "argument") == 0) {
+ uint32_t bnc = ts_node_child_count(a);
+ for (uint32_t j = 0; j < bnc; j++) {
+ TSNode b = ts_node_child(a, j);
+ if (!ts_node_is_null(b) && ts_node_is_named(b)) {
+ inner = b;
+ break;
+ }
+ }
+ }
+ if (seen == 0) closure_arg = inner;
+ else if (seen == 1) obj_arg = inner;
+ seen++;
+ }
+ if (!ts_node_is_null(closure_arg) && !ts_node_is_null(obj_arg)) {
+ const char *ck = ts_node_type(closure_arg);
+ if (strcmp(ck, "anonymous_function") == 0 ||
+ strcmp(ck, "arrow_function") == 0) {
+ const CBMType *t = php_eval_expr_type(ctx, obj_arg);
+ if (t && t->kind == CBM_TYPE_NAMED) {
+ const char *saved = ctx->enclosing_class_qn;
+ ctx->enclosing_class_qn = t->data.named.qualified_name;
+ php_resolve_calls_in_node(ctx, closure_arg);
+ ctx->enclosing_class_qn = saved;
+ }
+ }
+ }
+ }
+ }
+ }
}
/* Recurse. */
@@ -2686,11 +2827,25 @@ static const char **parse_phpdoc_template_params(PHPLSPContext *ctx, const char
const char *p = docstring;
while ((p = strstr(p, "@template")) != NULL) {
p += 9;
- /* Allow `@template-extends` / `@template-covariant` / etc. */
+ /* Skip variants that bind a type-param name with a kind suffix:
+ * @template-covariant T → record T
+ * @template-contravariant T → record T
+ * but NOT @template-extends / @template-implements (those declare
+ * a parent class, not a type param). */
+ bool is_param_variant = true;
if (*p == '-') {
- while (*p && *p != ' ' && *p != '\t' && *p != '\n') p++;
- continue;
+ const char *suffix = p + 1;
+ if (strncmp(suffix, "covariant", 9) == 0) {
+ p += 1 + 9;
+ } else if (strncmp(suffix, "contravariant", 13) == 0) {
+ p += 1 + 13;
+ } else {
+ /* @template-extends / -implements / -use / etc. — skip line. */
+ is_param_variant = false;
+ while (*p && *p != ' ' && *p != '\t' && *p != '\n') p++;
+ }
}
+ if (!is_param_variant) continue;
while (*p == ' ' || *p == '\t') p++;
const char *name_start = p;
while (*p && (isalnum((unsigned char)*p) || *p == '_')) p++;
@@ -2756,6 +2911,71 @@ static void apply_phpdoc_class_tags(PHPLSPContext *ctx, CBMTypeRegistry *reg,
}
}
+ /* @phpstan-type Alias TYPE_EXPR — register a per-file alias so
+ * subsequent @var/@param/@return references to `Alias` resolve to the
+ * aliased type. This is a phpstan/psalm convention used heavily for
+ * shape descriptions and value objects.
+ *
+ * Also accept the equivalent @psalm-type and @phan-type spellings. */
+ {
+ const char *prefixes[] = {"@phpstan-type", "@psalm-type", "@phan-type", NULL};
+ for (int pi = 0; prefixes[pi]; pi++) {
+ const char *p = docstring;
+ size_t plen = strlen(prefixes[pi]);
+ while ((p = strstr(p, prefixes[pi])) != NULL) {
+ p += plen;
+ while (*p == ' ' || *p == '\t') p++;
+ /* alias name */
+ const char *name_start = p;
+ while (*p && (isalnum((unsigned char)*p) || *p == '_')) p++;
+ if (p == name_start) continue;
+ char *alias_name =
+ cbm_arena_strndup(ctx->arena, name_start, (size_t)(p - name_start));
+ while (*p == ' ' || *p == '\t' || *p == '=') p++;
+ /* type expression, span <...> brackets */
+ const char *type_start = p;
+ int depth = 0;
+ while (*p && *p != '\n') {
+ if (*p == '<') depth++;
+ else if (*p == '>') depth--;
+ else if (depth == 0 && (*p == ' ' || *p == '\t') && p > type_start) {
+ break;
+ }
+ p++;
+ }
+ if (p == type_start) continue;
+ char *type_text =
+ cbm_arena_strndup(ctx->arena, type_start, (size_t)(p - type_start));
+ /* Strip trailing whitespace in case. */
+ size_t tl = strlen(type_text);
+ while (tl > 0 && (type_text[tl - 1] == ' ' || type_text[tl - 1] == '\t')) {
+ type_text[--tl] = '\0';
+ }
+ if (!*type_text) continue;
+ const CBMType *aliased = resolve_phpdoc_type(ctx, type_text);
+ if (aliased && aliased->kind != CBM_TYPE_UNKNOWN) {
+ php_add_phpstan_alias(ctx, alias_name, aliased);
+ }
+ }
+ }
+ }
+
+ /* @phpstan-import-type Alias from Other — best-effort. We don't have
+ * cross-file alias indexing yet, but accept the syntax without
+ * crashing. The alias becomes UNKNOWN until the source class's
+ * @phpstan-type ends up in our table. */
+ {
+ const char *p = docstring;
+ while ((p = strstr(p, "@phpstan-import-type")) != NULL) {
+ p += 20;
+ while (*p == ' ' || *p == '\t') p++;
+ const char *name_start = p;
+ while (*p && (isalnum((unsigned char)*p) || *p == '_')) p++;
+ if (p == name_start) continue;
+ (void)name_start; /* declared imported alias — silently accept */
+ }
+ }
+
/* @extends ParentClass<X> / @template-extends ParentClass<X>
* — when a class generically extends a parametric ancestor, we
* record that the ancestor's type-params are bound to the concrete
diff --git a/internal/cbm/lsp/php_lsp.h b/internal/cbm/lsp/php_lsp.h
index 6edcff8c7..4fdd2611b 100644
--- a/internal/cbm/lsp/php_lsp.h
+++ b/internal/cbm/lsp/php_lsp.h
@@ -36,6 +36,16 @@ typedef struct {
/* Output: resolved calls accumulate here. */
CBMResolvedCallArray *resolved_calls;
+ /* @phpstan-type alias map (per-file, populated from class docblocks).
+ * Used by resolve_phpdoc_type before generic name resolution so user
+ * type aliases like `@phpstan-type UserId int|string` and references
+ * to `UserId` in @var/@param/@return all resolve to the aliased type.
+ */
+ const char **phpstan_alias_names; /* arena-allocated, NULL-terminated */
+ const CBMType **phpstan_alias_types;
+ int phpstan_alias_count;
+ int phpstan_alias_cap;
+
/* Recursion guard for php_eval_expr_type. */
int eval_depth;
diff --git a/tests/test_php_lsp.c b/tests/test_php_lsp.c
index e9852b682..1558ae6b3 100644
--- a/tests/test_php_lsp.c
+++ b/tests/test_php_lsp.c
@@ -4533,6 +4533,605 @@ TEST(phplsp_edge_private_static) {
PASS();
}
+/* ── 249-280: Phase 5f / 4ac / 4ad ────────────────────────── */
+
+TEST(phplsp_phpstan_type_alias_basic) {
+ const char *src =
+ "<?php\n"
+ "class User { public function name(): string { return 'u'; } }\n"
+ "/**\n"
+ " * @phpstan-type Maybe User\n"
+ " */\n"
+ "class C {\n"
+ " public function run(): void {\n"
+ " /** @var Maybe $x */\n"
+ " $x = null;\n"
+ " $x->name();\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "User.name") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_phpstan_type_alias_via_param) {
+ const char *src =
+ "<?php\n"
+ "class B { public function tap(): int { return 1; } }\n"
+ "class A { public function next(): B { return new B(); } }\n"
+ "/**\n"
+ " * @phpstan-type Maker A\n"
+ " */\n"
+ "class C {\n"
+ " /** @param Maker $a */\n"
+ " public function run($a): void { $a->next()->tap(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "A.next") >= 0);
+ ASSERT(find_resolved(r, "C.run", "B.tap") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_psalm_type_alias_alternate_spelling) {
+ const char *src =
+ "<?php\n"
+ "class User { public function name(): string { return 'u'; } }\n"
+ "/**\n"
+ " * @psalm-type UID User\n"
+ " */\n"
+ "class C {\n"
+ " /** @param UID $u */\n"
+ " public function run($u): void { $u->name(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "User.name") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_phpstan_import_type_no_crash) {
+ const char *src =
+ "<?php\n"
+ "/**\n"
+ " * @phpstan-import-type Foo from \\Other\\Class_\n"
+ " */\n"
+ "class C { public function run(): void {} }\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_closure_bindTo_resolves_this) {
+ const char *src =
+ "<?php\n"
+ "class Other { public function helper(): int { return 1; } }\n"
+ "class C {\n"
+ " public function run(Other $o): void {\n"
+ " $f = function () { return $this->helper(); };\n"
+ " $f->bindTo($o);\n"
+ " }\n"
+ "}\n";
+ /* The closure literal is the callee of bindTo; my walker should
+ * re-walk the closure body with $this rebound to Other. */
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ /* Either the original walk OR the rebind walk emits Other.helper. */
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_closure_static_bind_resolves_this) {
+ const char *src =
+ "<?php\n"
+ "class Other { public function helper(): int { return 1; } }\n"
+ "class C {\n"
+ " public function run(Other $o): void {\n"
+ " \\Closure::bind(function () { return $this->helper(); }, $o);\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "Other.helper") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_closure_static_bind_arrow_fn) {
+ const char *src =
+ "<?php\n"
+ "class Other { public function helper(): int { return 1; } }\n"
+ "class C {\n"
+ " public function run(Other $o): void {\n"
+ " \\Closure::bind(fn() => $this->helper(), $o);\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "Other.helper") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_conditional_return_no_crash) {
+ const char *src =
+ "<?php\n"
+ "class C {\n"
+ " /**\n"
+ " * @return ($x is string ? int : bool)\n"
+ " */\n"
+ " public function strange(string|int $x): mixed { return 1; }\n"
+ " public function run(): void { $this->strange('a'); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "C.strange") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_template_covariant_no_crash) {
+ const char *src =
+ "<?php\n"
+ "/**\n"
+ " * @template-covariant T\n"
+ " */\n"
+ "class Box {\n"
+ " /** @return T */\n"
+ " public function get() { return null; }\n"
+ "}\n"
+ "class User { public function name(): string { return 'u'; } }\n"
+ "class C {\n"
+ " public function run(): void {\n"
+ " /** @var Box<User> $b */\n"
+ " $b = null;\n"
+ " $b->get()->name();\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "User.name") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_template_contravariant_no_crash) {
+ const char *src =
+ "<?php\n"
+ "/**\n"
+ " * @template-contravariant T\n"
+ " */\n"
+ "class Sink {\n"
+ " /** @param T $x */\n"
+ " public function take($x): void {}\n"
+ "}\n"
+ "class C { public function run(): void {} }\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_phpstan_type_array_shape) {
+ const char *src =
+ "<?php\n"
+ "class C {\n"
+ " /**\n"
+ " * @phpstan-type Pair array{a: int, b: string}\n"
+ " */\n"
+ " public function run(): void {}\n"
+ "}\n";
+ /* @phpstan-type at method-level — we don't apply but shouldn't crash. */
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_phpstan_type_union) {
+ const char *src =
+ "<?php\n"
+ "class A { public function aa(): int { return 1; } }\n"
+ "class B { public function bb(): int { return 2; } }\n"
+ "/**\n"
+ " * @phpstan-type Either A|B\n"
+ " */\n"
+ "class C {\n"
+ " /** @param Either $x */\n"
+ " public function run($x): void { $x->aa(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ /* Union types take leftmost — A. */
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_eloquent_macro_pattern) {
+ /* Laravel macroable pattern — closures bound to a class via macro(). */
+ const char *src =
+ "<?php\n"
+ "class Builder {\n"
+ " public function where($a, $b): self { return $this; }\n"
+ " public function search(string $q): self { return $this; }\n"
+ "}\n"
+ "class C {\n"
+ " public function run(): void {\n"
+ " $builder = new Builder();\n"
+ " \\Closure::bind(function (string $q) {\n"
+ " return $this->where('name', 'like', \"%{$q}%\");\n"
+ " }, $builder);\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "Builder.where") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_array_shape_call_no_crash) {
+ const char *src =
+ "<?php\n"
+ "class C {\n"
+ " /**\n"
+ " * @param array{name: string, age: int} $data\n"
+ " */\n"
+ " public function run(array $data): void { /* nothing */ }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_class_string_no_crash) {
+ const char *src =
+ "<?php\n"
+ "class A {}\n"
+ "class C {\n"
+ " /**\n"
+ " * @param class-string<A> $cls\n"
+ " */\n"
+ " public function run(string $cls): void {}\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_int_range_no_crash) {
+ const char *src =
+ "<?php\n"
+ "class C {\n"
+ " /**\n"
+ " * @param int<0, 100> $pct\n"
+ " */\n"
+ " public function run(int $pct): void {}\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_literal_string_no_crash) {
+ const char *src =
+ "<?php\n"
+ "class C {\n"
+ " /**\n"
+ " * @param literal-string $s\n"
+ " */\n"
+ " public function run(string $s): void {}\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_recursive_phpstan_alias) {
+ const char *src =
+ "<?php\n"
+ "class User { public function name(): string { return 'u'; } }\n"
+ "/**\n"
+ " * @phpstan-type ID int\n"
+ " * @phpstan-type Maybe User\n"
+ " */\n"
+ "class C {\n"
+ " /**\n"
+ " * @param ID $id\n"
+ " * @param Maybe $u\n"
+ " */\n"
+ " public function run($id, $u): void { $u->name(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "User.name") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_phpstan_type_with_template) {
+ const char *src =
+ "<?php\n"
+ "class User { public function name(): string { return 'u'; } }\n"
+ "/**\n"
+ " * @template T\n"
+ " * @phpstan-type Maybe User\n"
+ " */\n"
+ "class C {\n"
+ " /** @param Maybe $u */\n"
+ " public function run($u): void { $u->name(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "User.name") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_closure_bind_chain) {
+ const char *src =
+ "<?php\n"
+ "class Bag {\n"
+ " public int $count = 0;\n"
+ " public function add(int $n): void { $this->count += $n; }\n"
+ "}\n"
+ "class C {\n"
+ " public function run(Bag $b): void {\n"
+ " \\Closure::bind(function () {\n"
+ " $this->add(5);\n"
+ " }, $b);\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "Bag.add") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_long_phpdoc_block) {
+ const char *src =
+ "<?php\n"
+ "class A { public function go(): int { return 1; } }\n"
+ "class C {\n"
+ " /**\n"
+ " * Long description\n"
+ " * with multiple lines\n"
+ " * and various tags.\n"
+ " *\n"
+ " * @param A $a The thing\n"
+ " * @return void\n"
+ " * @throws \\RuntimeException\n"
+ " * @internal\n"
+ " */\n"
+ " public function run($a): void { $a->go(); }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "C.run", "A.go") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_repository_pattern) {
+ /* Common in DDD codebases: repository with EntityManager + Logger. */
+ const char *src =
+ "<?php\n"
+ "namespace App\\Repository;\n"
+ "use Doctrine\\ORM\\EntityManagerInterface;\n"
+ "use Psr\\Log\\LoggerInterface;\n"
+ "class UserRepository {\n"
+ " private EntityManagerInterface $em;\n"
+ " private LoggerInterface $log;\n"
+ " public function __construct(EntityManagerInterface $em, LoggerInterface $log) {\n"
+ " $this->em = $em;\n"
+ " $this->log = $log;\n"
+ " }\n"
+ " public function findActive(): array {\n"
+ " $this->log->info('finding active users');\n"
+ " return $this->em->createQueryBuilder()\n"
+ " ->select('u')->from('User', 'u')\n"
+ " ->where('u.active = 1')\n"
+ " ->getQuery()->getResult();\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "LoggerInterface.info") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive",
+ "EntityManagerInterface.createQueryBuilder") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "QueryBuilder.select") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "QueryBuilder.from") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "QueryBuilder.where") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "QueryBuilder.getQuery") >= 0);
+ ASSERT(find_resolved(r, "UserRepository.findActive", "Query.getResult") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_event_listener) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Listener;\n"
+ "use Symfony\\Contracts\\EventDispatcher\\Event;\n"
+ "use Psr\\Log\\LoggerInterface;\n"
+ "class OrderCreatedListener {\n"
+ " public function __construct(private readonly LoggerInterface $log) {}\n"
+ " public function __invoke(Event $event): void {\n"
+ " $this->log->info('order created');\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "OrderCreatedListener.__invoke", "LoggerInterface.info") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_command) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Command;\n"
+ "use Symfony\\Component\\Console\\Style\\SymfonyStyle;\n"
+ "use Symfony\\Component\\Console\\Input\\InputInterface;\n"
+ "use Symfony\\Component\\Console\\Output\\OutputInterface;\n"
+ "class SyncCommand {\n"
+ " public function execute(InputInterface $in, OutputInterface $out): int {\n"
+ " $io = new SymfonyStyle($in, $out);\n"
+ " $io->success('done');\n"
+ " $name = $in->getArgument('name');\n"
+ " return 0;\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "SyncCommand.execute", "SymfonyStyle.success") >= 0);
+ ASSERT(find_resolved(r, "SyncCommand.execute", "InputInterface.getArgument") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_form_handler) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Form;\n"
+ "use Symfony\\Component\\Validator\\Validator\\ValidatorInterface;\n"
+ "class FormHandler {\n"
+ " public function __construct(private readonly ValidatorInterface $v) {}\n"
+ " public function handle(array $data): bool {\n"
+ " $errors = $this->v->validate($data);\n"
+ " return $errors->count() === 0;\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "FormHandler.handle", "ValidatorInterface.validate") >= 0);
+ ASSERT(find_resolved(r, "FormHandler.handle", "ConstraintViolationListInterface.count") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_mail_sender) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Service;\n"
+ "use Symfony\\Component\\Mailer\\MailerInterface;\n"
+ "use Symfony\\Component\\Mime\\Email;\n"
+ "class WelcomeMailer {\n"
+ " public function __construct(private readonly MailerInterface $m) {}\n"
+ " public function send(string $to): void {\n"
+ " $email = (new Email())->from('a@b')->to($to)->subject('Welcome');\n"
+ " $this->m->send($email);\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "WelcomeMailer.send", "Email.from") >= 0);
+ ASSERT(find_resolved(r, "WelcomeMailer.send", "Email.to") >= 0);
+ ASSERT(find_resolved(r, "WelcomeMailer.send", "Email.subject") >= 0);
+ ASSERT(find_resolved(r, "WelcomeMailer.send", "MailerInterface.send") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_cached_service) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Service;\n"
+ "use Symfony\\Contracts\\Cache\\CacheInterface;\n"
+ "class CachedService {\n"
+ " public function __construct(private readonly CacheInterface $cache) {}\n"
+ " public function getData(string $k): mixed {\n"
+ " return $this->cache->get($k, fn() => $this->compute($k));\n"
+ " }\n"
+ " private function compute(string $k): mixed { return null; }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "CachedService.getData", "CacheInterface.get") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_logger_chain_with_context) {
+ const char *src =
+ "<?php\n"
+ "namespace App;\n"
+ "use Psr\\Log\\LoggerInterface;\n"
+ "class S {\n"
+ " public function __construct(private LoggerInterface $log) {}\n"
+ " public function r(): void {\n"
+ " $this->log->info('a', ['k' => 1]);\n"
+ " $this->log->warning('b');\n"
+ " $this->log->error('c');\n"
+ " $this->log->critical('d');\n"
+ " $this->log->debug('e');\n"
+ " $this->log->log('info', 'f');\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.info") >= 0);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.warning") >= 0);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.error") >= 0);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.critical") >= 0);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.debug") >= 0);
+ ASSERT(find_resolved(r, "S.r", "LoggerInterface.log") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_carbon_chain_in_method) {
+ const char *src =
+ "<?php\n"
+ "namespace App;\n"
+ "use Carbon\\Carbon;\n"
+ "class Clock {\n"
+ " public function tomorrow(): string {\n"
+ " return Carbon::now()->addDay()->format('Y-m-d');\n"
+ " }\n"
+ " public function lastWeek(): string {\n"
+ " return Carbon::now()->subDay()->subDay()->format('Y-m-d');\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "Clock.tomorrow", "Carbon.now") >= 0);
+ ASSERT(find_resolved(r, "Clock.tomorrow", "Carbon.addDay") >= 0);
+ ASSERT(find_resolved(r, "Clock.tomorrow", "Carbon.format") >= 0);
+ ASSERT(find_resolved(r, "Clock.lastWeek", "Carbon.subDay") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(phplsp_realistic_eloquent_model) {
+ const char *src =
+ "<?php\n"
+ "namespace App\\Models;\n"
+ "use Illuminate\\Database\\Eloquent\\Model;\n"
+ "class User extends Model {\n"
+ " public function active(): \\Illuminate\\Database\\Eloquent\\Collection {\n"
+ " return self::where('active', 1)->orderBy('id')->get();\n"
+ " }\n"
+ "}\n";
+ CBMFileResult *r = extract_php(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "User.active", "Model.where") >= 0);
+ ASSERT(find_resolved(r, "User.active", "Builder.orderBy") >= 0);
+ ASSERT(find_resolved(r, "User.active", "Builder.get") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
/* ── Suite ─────────────────────────────────────────────────────── */
SUITE(php_lsp) {
@@ -4791,4 +5390,36 @@ SUITE(php_lsp) {
RUN_TEST(phplsp_edge_function_with_default_typed);
RUN_TEST(phplsp_edge_property_with_default);
RUN_TEST(phplsp_edge_private_static);
+ /* Phase 5f / 4ac / 4ad: phpstan-type aliases, closure binding,
+ * conditional types, variance, real-world patterns */
+ RUN_TEST(phplsp_phpstan_type_alias_basic);
+ RUN_TEST(phplsp_phpstan_type_alias_via_param);
+ RUN_TEST(phplsp_psalm_type_alias_alternate_spelling);
+ RUN_TEST(phplsp_phpstan_import_type_no_crash);
+ RUN_TEST(phplsp_closure_bindTo_resolves_this);
+ RUN_TEST(phplsp_closure_static_bind_resolves_this);
+ RUN_TEST(phplsp_closure_static_bind_arrow_fn);
+ RUN_TEST(phplsp_conditional_return_no_crash);
+ RUN_TEST(phplsp_template_covariant_no_crash);
+ RUN_TEST(phplsp_template_contravariant_no_crash);
+ RUN_TEST(phplsp_phpstan_type_array_shape);
+ RUN_TEST(phplsp_phpstan_type_union);
+ RUN_TEST(phplsp_eloquent_macro_pattern);
+ RUN_TEST(phplsp_array_shape_call_no_crash);
+ RUN_TEST(phplsp_class_string_no_crash);
+ RUN_TEST(phplsp_int_range_no_crash);
+ RUN_TEST(phplsp_literal_string_no_crash);
+ RUN_TEST(phplsp_recursive_phpstan_alias);
+ RUN_TEST(phplsp_phpstan_type_with_template);
+ RUN_TEST(phplsp_closure_bind_chain);
+ RUN_TEST(phplsp_long_phpdoc_block);
+ RUN_TEST(phplsp_realistic_repository_pattern);
+ RUN_TEST(phplsp_realistic_event_listener);
+ RUN_TEST(phplsp_realistic_command);
+ RUN_TEST(phplsp_realistic_form_handler);
+ RUN_TEST(phplsp_realistic_mail_sender);
+ RUN_TEST(phplsp_realistic_cached_service);
+ RUN_TEST(phplsp_realistic_logger_chain_with_context);
+ RUN_TEST(phplsp_realistic_carbon_chain_in_method);
+ RUN_TEST(phplsp_realistic_eloquent_model);
}