phpdoc

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package phpdoc parses PHPDoc comments: tags and type expressions.

Index

Constants

View Source
const MaxAliasLen = 4096

MaxAliasLen is the longest type alias definition kept (the same cap as types.MaxDocTypeLen): a longer one is recorded as "" (mixed), so that resolvers do not copy a huge definition at every use of the alias.

Variables

This section is empty.

Functions

func SplitType

func SplitType(text string) (typ, rest string)

SplitType splits tag text into its leading type expression and the rest (balanced over <>, (), {}, []), e.g. "array<int, string> $x desc".

func VarName

func VarName(text string) string

VarName extracts a leading `$name` (without '$') from text.

Types

type Doc

type Doc struct {
	Summary string
	Tags    []Tag
}

Doc is a parsed doc comment.

func Parse

func Parse(comment string) *Doc

Parse parses a /** ... */ comment (or any comment text).

func (*Doc) All

func (d *Doc) All(name string) []Tag

All returns every tag with the given name, ignoring case.

func (*Doc) EffectiveParams

func (d *Doc) EffectiveParams() []Param

EffectiveParams selects parameter types, preferring phpstan-param over psalm-param over param independently for each named parameter. Empty types are ignored. Within one dialect the last declaration wins, as with Params. Results follow the first occurrence of each selected parameter in the doc.

func (*Doc) EffectiveReturnType

func (d *Doc) EffectiveReturnType() string

EffectiveReturnType selects the first nonempty return type in the highest available dialect: phpstan-return, psalm-return, then return. Description-only annotations are ignored using the same heuristic as ReturnType.

func (*Doc) EffectiveVarType

func (d *Doc) EffectiveVarType(name string) string

EffectiveVarType selects the first applicable nonempty variable annotation in the highest available dialect. An unnamed annotation applies to any name; an empty name selects the first variable annotation in that dialect.

func (*Doc) EffectiveVars

func (d *Doc) EffectiveVars() []Param

EffectiveVars selects variable types, preferring phpstan-var over psalm-var over var independently for each variable (including unnamed annotations). Empty types are ignored; the first declaration within a dialect wins. Results follow the first occurrence of each selected variable in the doc.

func (*Doc) Has

func (d *Doc) Has(name string) bool

Has reports whether a tag exists, ignoring case.

func (*Doc) ImportedTypes

func (d *Doc) ImportedTypes() []ImportedType

ImportedTypes returns the type aliases the doc imports (`@phpstan-import-type Name from From as Local`), as written.

func (*Doc) Params

func (d *Doc) Params() []Param

Params returns all @param tags.

func (*Doc) ParamsOf

func (d *Doc) ParamsOf(tag string) []Param

ParamsOf returns the tags named tag ("param", "phpstan-param", "psalm-param") parsed as @param tags.

func (*Doc) ReturnType

func (d *Doc) ReturnType() string

ReturnType returns the @return type text ("" when absent).

func (*Doc) Tag

func (d *Doc) Tag(name string) (Tag, bool)

Tag returns the first tag with the given name, ignoring case.

func (*Doc) TemplateParams

func (d *Doc) TemplateParams() []TemplateParam

TemplateParams returns the templates declared by @template (and the variants Templates accepts) with their bounds, in declaration order.

func (*Doc) Templates

func (d *Doc) Templates() []string

Templates returns the names declared by @template (and the psalm-/phpstan- prefixed and covariant/contravariant variants).

func (*Doc) TypeAliases

func (d *Doc) TypeAliases() map[string]string

TypeAliases returns local type aliases declared with @phpstan-type / @psalm-type (name -> definition text; "" beyond MaxAliasLen) and imported ones with @phpstan-import-type / @psalm-import-type (name -> "").

func (*Doc) VarType

func (d *Doc) VarType(name string) string

VarType returns the @var type for variable name (or the first @var when name is "" or the tag names no variable).

type ImportedType

type ImportedType struct{ Name, From, Local string }

ImportedType is a type alias imported with @phpstan-import-type or @psalm-import-type: alias Name of class From, known here as Local.

type MethodParam

type MethodParam struct {
	Name, Type, Default       string
	Optional, ByRef, Variadic bool
}

MethodParam describes one parameter in a documented magic method.

type MethodSignature

type MethodSignature struct {
	Name, Return string
	Static       bool
	Params       []MethodParam
}

MethodSignature is a class @method declaration. Its types are still PHPDoc text: callers resolve them in the declaring class's namespace.

func ParseMethod

func ParseMethod(text string) (MethodSignature, bool)

ParseMethod parses an @method signature, ignoring a trailing description. Malformed or oversized signatures are discarded rather than partially kept.

type Param

type Param struct {
	Type string
	Name string
}

Param is a parsed @param tag.

type Tag

type Tag struct {
	Name string // without '@', e.g. "return"
	Text string // rest of the tag (continuation lines joined with '\n')
}

Tag is one `@name rest` entry.

type TemplateParam

type TemplateParam struct {
	Name     string
	Bound    string // type text after `of` / `as`; "" when absent
	Default  string // type text after `=` (`@template T of array-key = int`); "" when absent
	Variance Variance
}

TemplateParam is a declared template: `@template T of Bound`.

type Variance

type Variance uint8

Variance is how a template argument may vary between a type and its subtypes: invariant (`@template`), covariant (`@template-covariant`) or contravariant (`@template-contravariant`).

const (
	Invariant Variance = iota
	Covariant
	Contravariant
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL