From 508701434af0ffe1d2bc90562304d94e28bf5b42 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 15 Nov 2026 17:12:58 +0100 Subject: [PATCH] Skip union types, only add a single type or a nullable one --- CLAUDE.md | 2 +- README.md | 22 +++++++++------------- internal/aggregate/aggregate.go | 11 ++++++----- internal/aggregate/aggregate_test.go | 11 ++++------- internal/apply/apply.go | 2 +- internal/apply/apply_test.go | 16 ++++++++-------- main.go | 2 +- 7 files changed, 30 insertions(+), 36 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 1d9594f7..9db7a988 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -48,7 +48,7 @@ argtyper add-types [project-path] [--dry] [--literals] [--objects] `project-path` defaults to `.`. `--dry` prints the diff without writing. `--literals` only adds string types with a `@param 'a'|'b'` literal docblock. -`--objects` only adds object types, including unions made only of objects. +`--objects` only adds object types. Both can be combined. Dev tasks via `Makefile`: diff --git a/README.md b/README.md index c5babd3d..eb8fa168 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,9 @@ $this->setLocale(null); } ``` -**Union types** when two or more different types reach the same parameter - -instead of guessing or giving up, it writes them all: +**No union types** - when two or more different types reach the same +parameter, it is ambiguous and left untyped. Only a single type, or a single +type plus `null` (nullable), is added: ```php $this->handle(1); // int @@ -100,16 +101,12 @@ $this->handle("text"); // string ``` ```diff --public function handle($value) -+public function handle(int|string $value) + public function handle($value) { } ``` -There is no cap on the number of union members, and they are sorted -alphabetically. A nullable union is written with a trailing `|null` (since `?` -only works for a single type). The one type it will not accept is `callable` -(a closure or arrow function) - that poisons the parameter and it is left +A `callable` argument (a closure or arrow function) also leaves the parameter untyped. **Default values** are typed too, even without a call site: @@ -201,9 +198,8 @@ Only add the string literal docblocks, and no other types, with `--literals`: argtyper add-types . --literals ``` -Only add object types with `--objects` - a single class, a nullable one, or a -union made only of classes (`\Foo|\Bar`). A union with a scalar, like -`int|\Money`, is skipped. Both options can be combined: +Only add object types with `--objects` - a single class or a nullable one. +Both options can be combined: ```bash argtyper add-types . --objects @@ -219,8 +215,8 @@ argtyper add-types . --literals --objects objects, `Foo::class`, enum cases, constants, and a few built-in function returns. 2. **Resolve** - it groups the recorded types per parameter position. A single - type is used directly; two or more distinct types become a union; `null` - becomes the nullable flag rather than a union member. + type is used directly; two or more distinct types are ambiguous and skipped; + `null` becomes the nullable flag. 3. **Apply** - it adds each resolved type to the definitions that are still missing one, removes any now-redundant `@param` docblock, and reprints the file. Unchanged code keeps its exact formatting. diff --git a/internal/aggregate/aggregate.go b/internal/aggregate/aggregate.go index b487d354..f553da73 100644 --- a/internal/aggregate/aggregate.go +++ b/internal/aggregate/aggregate.go @@ -14,9 +14,9 @@ import ( ) // Resolved is the final type decision for one parameter position. Two or more -// types observed for the same parameter become a union. +// types observed for the same parameter are ambiguous and left unresolved. type Resolved struct { - Types []valuetype.Type // one or more types, never Null + Types []valuetype.Type // exactly one type, never Null Nullable bool Literals []string // sorted string literal values, set only for a plain string type, see literalValues } @@ -54,8 +54,8 @@ func (t Types) Function(name string, position int) (Resolved, bool) { return resolved, ok } -// Resolve groups records per parameter into a resolved type: the observed types -// as a union, with null captured as nullability rather than a member. +// Resolve groups records per parameter into a resolved type: a single observed +// type, with null captured as nullability. Unions are skipped. func Resolve(records []collect.Record) Types { methodTypes := map[string]*group{} functionTypes := map[string]*group{} @@ -95,7 +95,8 @@ func resolveGroups(groups map[string]*group) map[string]Resolved { nullable := contains(types, valuetype.Null) members := without(types, valuetype.Null) - if len(members) == 0 { + // only a single type is added, unions are left untyped + if len(members) != 1 { continue } resolved[key] = Resolved{Types: members, Nullable: nullable, Literals: literalValues(members, group)} diff --git a/internal/aggregate/aggregate_test.go b/internal/aggregate/aggregate_test.go index 46acb650..dcbf1d7a 100644 --- a/internal/aggregate/aggregate_test.go +++ b/internal/aggregate/aggregate_test.go @@ -34,24 +34,21 @@ func TestResolveMethod(t *testing.T) { wantFound: true, }, { - name: "two real types become a union", + name: "two real types are skipped", records: []collect.Record{ {Class: "A", Name: "m", Type: "int"}, {Class: "A", Name: "m", Type: "string"}, }, - wantType: "int|string", - wantFound: true, + wantFound: false, }, { - name: "union with null", + name: "two real types with null are skipped", records: []collect.Record{ {Class: "A", Name: "m", Type: "int"}, {Class: "A", Name: "m", Type: "string"}, {Class: "A", Name: "m", Type: "null"}, }, - wantType: "int|string", - wantNullable: true, - wantFound: true, + wantFound: false, }, { name: "unresolved argument keeps the resolved type", diff --git a/internal/apply/apply.go b/internal/apply/apply.go index b0aea583..52258e5f 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -42,7 +42,7 @@ func Source(src []byte, types aggregate.Types, table *symbols.Table, inheritance // added; with one or more set, only the chosen kinds are. type Options struct { Literals bool // string types that get a `'a'|'b'` literal doc type - Objects bool // object types, including unions made only of objects + Objects bool // object types } // allows reports whether a resolved type passes the options; docType is its diff --git a/internal/apply/apply_test.go b/internal/apply/apply_test.go index f7f8caae..acd11c84 100644 --- a/internal/apply/apply_test.go +++ b/internal/apply/apply_test.go @@ -130,19 +130,19 @@ func TestApply(t *testing.T) { want: "set(\"x\"); }\n}", }, { - name: "unions multiple observed types", + name: "skips multiple observed types", target: "set(1); $this->set(\"x\"); }\n}", - want: "set(1); $this->set(\"x\"); }\n}", + want: "set(1); $this->set(\"x\"); }\n}", }, { - name: "unions multiple types with null as a member", + name: "skips multiple types with null", target: "set(1); $this->set(\"x\"); $this->set(null); }\n}", - want: "set(1); $this->set(\"x\"); $this->set(null); }\n}", + want: "set(1); $this->set(\"x\"); $this->set(null); }\n}", }, { - name: "unions object and scalar types", + name: "skips object and scalar types", target: "set(new Money()); $this->set(1); }\n}", - want: "set(new Money()); $this->set(1); }\n}", + want: "set(new Money()); $this->set(1); }\n}", }, { name: "skips magic method", @@ -502,8 +502,8 @@ func TestObjectsOnly(t *testing.T) { output, added, _ := apply.Source([]byte(src), types, table, inherit.New(), apply.Options{Objects: true}) - want := "