Documentation
¶
Overview ¶
Package model wraps the Go standard-library AST into phpmd-style code artifacts (Class, Interface, Method, Function, Field, Parameter). Rules are written against these artifacts, mirroring how PHPMD rules operate on pdepend's ASTClass / ASTMethod / ASTFunction nodes.
Index ¶
- type Call
- type Class
- type DuplicateLiteralKey
- type Field
- type File
- func (f *File) EffectiveLinesOfCode(start, end token.Pos) int
- func (f *File) MemberSelected(name string) bool
- func (f *File) MutatedPackageGlobals() map[string]bool
- func (f *File) PackageDuplicateLiteralKeys() []DuplicateLiteralKey
- func (f *File) PackageVars() []PackageVar
- func (f *File) SelectedMemberNames() map[string]bool
- type Function
- func (f *Function) AccessorField(fields map[string]bool) string
- func (f *Function) Calls() []Call
- func (f *Function) DuplicateLiteralKeys() []DuplicateLiteralKey
- func (f *Function) ElseBlockLines() []int
- func (f *Function) EmptyNilCheckBlockLines() []int
- func (f *Function) HasGoto() bool
- func (f *Function) IdentifierRead(target *ast.Ident) bool
- func (f *Function) IdentifierReads() map[string]bool
- func (f *Function) IfAssignmentInitPositions() []SourcePosition
- func (f *Function) IsMethod() bool
- func (f *Function) LocalVariables() []LocalVariable
- func (f *Function) LoopConditionCalls(names map[string]bool) []Call
- func (f *Function) NodeType() NodeType
- func (f *Function) ReceiverUses(fields map[string]bool, methods map[string]int) (usedFields, calledMethods []string)
- type Interface
- type LocalVariable
- type NodeType
- type PackageVar
- type Parameter
- type SourcePosition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Class ¶
type Class struct {
Name string
Line int
EndLine int
Exported bool
Fields []*Field
Methods []*Function
File *File
Spec *ast.TypeSpec
Struct *ast.StructType
DocComment string
// Embeds holds embedded type names (the closest Go analog to parents).
Embeds []string
}
Class is a named struct type plus its associated methods (analog of a PHP class). A type defined as `type T struct{...}` becomes a Class; methods with receiver T are attached.
type DuplicateLiteralKey ¶ added in v0.2.0
DuplicateLiteralKey describes a repeated constant key in a composite literal.
type Field ¶
type Field struct {
Name string
Type string
Line int
Exported bool
Static bool // package-level var attached as a "static" field (unused for structs)
Ident *ast.Ident
}
Field is a struct field (the analog of a PHP class property).
type File ¶
type File struct {
Path string
Fset *token.FileSet
Syntax *ast.File
Src []byte
Package string
Classes []*Class
Interfaces []*Interface
Functions []*Function
// AllFuncs includes both free functions and methods, in source order.
AllFuncs []*Function
// MutatedGlobals holds the package-level variable names that are mutated
// anywhere in this file's package. It is populated by the runner once all
// of a package's files are parsed, enabling cross-file analysis. It is nil
// when a file is analyzed in isolation (rules then fall back to single-file
// analysis).
MutatedGlobals map[string]bool
// contains filtered or unexported fields
}
File is a parsed Go source file plus all artifacts discovered within it.
func ParseSource ¶
ParseSource parses Go source bytes into a File model.
func (*File) EffectiveLinesOfCode ¶ added in v0.2.2
EffectiveLinesOfCode returns the number of code-bearing physical source lines in the requested span. The source is indexed at most once per file.
func (*File) MemberSelected ¶ added in v0.2.2
MemberSelected reports whether name is selected or used as a keyed struct literal field anywhere in this file. The file-wide AST scan runs once.
func (*File) MutatedPackageGlobals ¶ added in v0.2.0
MutatedPackageGlobals returns package-level variable names mutated in this file unless the runner has already populated cross-file mutation data.
func (*File) PackageDuplicateLiteralKeys ¶ added in v0.2.1
func (f *File) PackageDuplicateLiteralKeys() []DuplicateLiteralKey
PackageDuplicateLiteralKeys returns duplicate constant keys in package-level composite literals.
func (*File) PackageVars ¶ added in v0.2.0
func (f *File) PackageVars() []PackageVar
PackageVars returns package-level variables declared in the file, skipping the blank identifier.
func (*File) SelectedMemberNames ¶ added in v0.2.0
SelectedMemberNames returns a snapshot of field or method names selected or used as keyed struct-literal fields anywhere in this file.
type Function ¶
type Function struct {
Name string
Receiver string // empty for free functions; type name (without *) for methods
RecvName string // receiver variable name, e.g. "f" in (f *Foo)
Params []*Parameter
Results []*Parameter
Line int
EndLine int
Exported bool
Decl *ast.FuncDecl
Body *ast.BlockStmt
File *File
Class *Class // owning class for a method, if resolved
DocComment string
// contains filtered or unexported fields
}
Function represents a free function OR a method (when Receiver != ""). PHPMD distinguishes ASTMethod from ASTFunction; we keep one struct and use IsMethod()/Receiver to tell them apart, which keeps rule code uniform.
func (*Function) AccessorField ¶ added in v0.2.0
AccessorField returns the field wrapped by a trivial getter or setter.
func (*Function) DuplicateLiteralKeys ¶ added in v0.2.0
func (f *Function) DuplicateLiteralKeys() []DuplicateLiteralKey
DuplicateLiteralKeys returns duplicate constant keys in composite literals.
func (*Function) ElseBlockLines ¶ added in v0.2.0
ElseBlockLines returns the source lines of else blocks, excluding else-if chains.
func (*Function) EmptyNilCheckBlockLines ¶ added in v0.2.0
EmptyNilCheckBlockLines returns lines for empty if-blocks whose condition compares any operand with nil using !=.
func (*Function) HasGoto ¶ added in v0.2.0
HasGoto reports whether the function body contains a goto statement.
func (*Function) IdentifierRead ¶ added in v0.2.1
IdentifierRead reports whether target's binding is read in this function. Object identity distinguishes a shadowing local from a captured parameter or local with the same name.
func (*Function) IdentifierReads ¶ added in v0.2.0
IdentifierReads returns names read from this function body. Identifiers used only as assignment or declaration write targets are excluded.
func (*Function) IfAssignmentInitPositions ¶ added in v0.2.0
func (f *Function) IfAssignmentInitPositions() []SourcePosition
IfAssignmentInitPositions returns positions for plain assignment initializers in if statements. Short declarations are intentionally excluded.
func (*Function) LocalVariables ¶ added in v0.2.0
func (f *Function) LocalVariables() []LocalVariable
LocalVariables returns local variable declarations in this function.
func (*Function) LoopConditionCalls ¶ added in v0.2.0
LoopConditionCalls returns calls to selected names found in for-loop conditions, reported at the loop's line.
type Interface ¶
type Interface struct {
Name string
Line int
EndLine int
Exported bool
Methods []*Function
File *File
Spec *ast.TypeSpec
Iface *ast.InterfaceType
DocComment string
Embeds []string
}
Interface is a named interface type (analog of a PHP interface).
type LocalVariable ¶ added in v0.2.0
LocalVariable describes a local variable declaration.
type NodeType ¶
type NodeType string
NodeType identifies the kind of artifact, used for rule message rendering (the PHPMD "{0}" placeholder is typically the artifact type: "class", "method", "function", "interface").
type PackageVar ¶ added in v0.2.0
PackageVar describes a package-level variable declaration.
type Parameter ¶
type Parameter struct {
Name string
Type string
Line int
Field *ast.Field
Ident *ast.Ident
Promoted bool // reserved; Go has no constructor promotion
}
Parameter is a formal parameter of a function or method.
type SourcePosition ¶ added in v0.2.0
SourcePosition identifies a position in the original source file.