Skip to content

Rewrite argtyper in Go using php-parser-in-go - #12

Merged
TomasVotruba merged 1 commit into
mainfrom
go-rewrite
Sep 13, 2026
Merged

Rewrite argtyper in Go using php-parser-in-go#12
TomasVotruba merged 1 commit into
mainfrom
go-rewrite

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Replaces the PHP + PHPStan + Rector implementation with a self-contained Go tool built on php-parser-in-go.

What it does

argtyper add-types <path> walks the project, records the types of literal arguments passed into calls, then fills in missing parameter type declarations.

  • Collects literal argument types: int, float, string, bool, array, null, and new X() as object:X.
  • Resolves call targets from the syntax tree alone: new X(), X::m(), self::m(), $this->m() and plain function() calls.
  • Groups types per parameter position; single type is applied, single type + null becomes nullable, multiple types are skipped as ambiguous.
  • Skips params that already have a type, magic methods (except __construct), and methods that may override a parent/interface (unless private or constructor).

Scope vs the old version

The PHP tool used PHPStan's type inference, so it could type a param even when the argument was a variable carrying a known type. The Go version has no inference engine - it uses only the parsed AST, so it handles literal values passed into statically resolvable targets. Calls on arbitrary variables ($service->method(...)) are skipped, since the class cannot be resolved without inference. Classes are matched by short name.

Layout

  • main.go - CLI
  • internal/finder - find PHP files in code dirs
  • internal/collect - record literal arg types at call sites
  • internal/aggregate - resolve one type per parameter
  • internal/apply - add missing param types and reprint
  • internal/phpast - shared AST helpers

CI

PHP workflows replaced with a Go build workflow (gofmt, vet, build, test). Tests cover collect, aggregate, apply and finder.

Replace the PHP + PHPStan + Rector implementation with a self-contained
Go tool built on rectorphp/php-parser-in-go.

It collects the types of literal arguments passed into statically
resolvable call targets (new X(), X::m(), self::m(), $this->m() and
plain function calls), groups them per parameter position, and fills in
missing parameter type declarations. Ambiguous types are skipped, a
single type plus null becomes nullable.

No type inference engine is used, so only literal values and
statically resolvable targets are handled.
@TomasVotruba
TomasVotruba merged commit acb1a26 into main Sep 13, 2026
1 check passed
@TomasVotruba
TomasVotruba deleted the go-rewrite branch September 13, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant