model

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call added in v0.2.0

type Call struct {
	Name string
	Line int
}

Call describes a function call found inside an artifact.

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.

func (*Class) NodeType

func (c *Class) NodeType() NodeType

type DuplicateLiteralKey added in v0.2.0

type DuplicateLiteralKey struct {
	Display   string
	FirstLine int
	Line      int
}

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 Parse

func Parse(path string) (*File, error)

Parse reads and parses a Go source file into a File model.

func ParseSource

func ParseSource(path string, src []byte) (*File, error)

ParseSource parses Go source bytes into a File model.

func (*File) EffectiveLinesOfCode added in v0.2.2

func (f *File) EffectiveLinesOfCode(start, end token.Pos) int

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

func (f *File) MemberSelected(name string) bool

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

func (f *File) MutatedPackageGlobals() map[string]bool

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

func (f *File) SelectedMemberNames() map[string]bool

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

func (f *Function) AccessorField(fields map[string]bool) string

AccessorField returns the field wrapped by a trivial getter or setter.

func (*Function) Calls added in v0.2.0

func (f *Function) Calls() []Call

Calls returns every call expression in this function body.

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

func (f *Function) ElseBlockLines() []int

ElseBlockLines returns the source lines of else blocks, excluding else-if chains.

func (*Function) EmptyNilCheckBlockLines added in v0.2.0

func (f *Function) EmptyNilCheckBlockLines() []int

EmptyNilCheckBlockLines returns lines for empty if-blocks whose condition compares any operand with nil using !=.

func (*Function) HasGoto added in v0.2.0

func (f *Function) HasGoto() bool

HasGoto reports whether the function body contains a goto statement.

func (*Function) IdentifierRead added in v0.2.1

func (f *Function) IdentifierRead(target *ast.Ident) bool

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

func (f *Function) IdentifierReads() map[string]bool

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) IsMethod

func (f *Function) IsMethod() bool

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

func (f *Function) LoopConditionCalls(names map[string]bool) []Call

LoopConditionCalls returns calls to selected names found in for-loop conditions, reported at the loop's line.

func (*Function) NodeType

func (f *Function) NodeType() NodeType

func (*Function) ReceiverUses added in v0.2.0

func (f *Function) ReceiverUses(fields map[string]bool, methods map[string]int) (usedFields, calledMethods []string)

ReceiverUses returns fields and sibling methods selected through the receiver variable in this function body.

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).

func (*Interface) NodeType

func (i *Interface) NodeType() NodeType

type LocalVariable added in v0.2.0

type LocalVariable struct {
	Name   string
	Line   int
	IsLoop bool
	Ident  *ast.Ident
}

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").

const (
	TypeClass     NodeType = "class"
	TypeInterface NodeType = "interface"
	TypeTrait     NodeType = "trait"
	TypeMethod    NodeType = "method"
	TypeFunction  NodeType = "function"
)

type PackageVar added in v0.2.0

type PackageVar struct {
	Name string
	Line int
}

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

type SourcePosition struct {
	Line   int
	Column int
}

SourcePosition identifies a position in the original source file.

Jump to

Keyboard shortcuts

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