php

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package php reads a PHP source file and returns a structured summary of the first class/interface/trait/enum declaration it finds: namespace, FQCN, modifiers, parent/implements, public methods, constructor dependencies, and the docblock summary.

The goal is to let downstream tools answer questions about a class without making the model cat the whole file.

Backend: github.com/VKCOM/php-parser (pure Go, no CGO). Adapter is the package-level Read/ReadString — swap backends here if VKCOM ever stops being viable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Slice

func Slice(src []byte, symbol string) (text string, names []string, err error)

Slice returns the source text of the named top-level function or class method in src. symbol may be a bare name ("completeTask") or qualified ("CompleteTaskController::completeTask"). On no match, names lists the available function/method identifiers so callers can build a helpful error.

Types

type Attr

type Attr struct {
	Name string
	Args []AttrArg
}

Attr is a PHP attribute together with its arguments, in source order. Name is the resolved FQCN (e.g. "Doctrine\ORM\Mapping\Column"), so consumers can match on a stable suffix regardless of the `use` alias.

func (Attr) Get

func (a Attr) Get(name string) (string, bool)

Get returns the value of the named argument and whether it was present.

type AttrArg

type AttrArg struct {
	Name  string
	Value string
}

AttrArg is one attribute argument. Name is the label for named args ("length", "type", ...) or "" for positional. Value is a best-effort stringification: strings unquoted, ints/floats verbatim, bools as "true"/"false", arrays as "[a,b]", class consts as "Foo::BAR".

type CtorDep

type CtorDep struct {
	Name     string
	Type     string
	Promoted bool
}

CtorDep is one constructor argument. Promoted=true means it's also declared as a property (PHP 8 promoted-property syntax).

type EnumCase

type EnumCase struct {
	Name  string
	Value string
}

EnumCase is one case of a PHP enum. Value is the backing value for a backed enum (string/int); "" for a pure enum.

type Kind

type Kind string

Kind classifies the top-level declaration found in the file.

const (
	KindClass     Kind = "class"
	KindInterface Kind = "interface"
	KindTrait     Kind = "trait"
	KindEnum      Kind = "enum"
)

type Method

type Method struct {
	Name       string
	Params     []Param
	ReturnType string
	Attributes []string
	Attrs      []Attr
}

Method is a public method signature. Attributes hold the bare names (#[Override] -> "Override"); Attrs additionally carries the resolved names with arguments (e.g. #[Route('/x', methods: ['GET'])]).

type Param

type Param struct {
	Name string
	Type string
}

Param is a single method parameter.

type Property

type Property struct {
	Name       string
	Type       string // PHP type as written, resolved like params; "" if untyped
	Visibility string // public | protected | private
	Attributes []Attr
}

Property is a class property with its declared type and attributes.

type Symbol

type Symbol struct {
	File       string
	Namespace  string
	FQCN       string
	Kind       Kind
	Modifiers  []string // final, readonly, abstract — in source order
	Extends    string   // empty for interfaces/traits/enums; FQCN otherwise
	Implements []string // for classes: implements list; for interfaces: extends list; for enums: implements list
	Uses       []string // FQCN of traits composed via `use` in the body (classes and traits)
	DocSummary string   // first non-tag line of the class-level docblock, or ""
	Attributes []Attr   // class-level attributes with arguments (e.g. #[ORM\Table(...)])
	CtorDeps   []CtorDep
	Properties []Property // declared properties (any visibility) with type + attributes
	Cases      []EnumCase // enum cases (empty for non-enums)
	Methods    []Method   // public only; __construct excluded (captured as CtorDeps)
	Partial    bool       // recovered by the regex fallback, not the AST: names are unresolved and members are absent
}

Symbol is the structured summary of one PHP type declaration. All names are FQCN where applicable (parser resolves short names via `use`-imports).

func Read

func Read(path string) (*Symbol, error)

Read parses path and returns the first type declaration. Returns an error if no such declaration exists, or if the file cannot be parsed.

func ReadString

func ReadString(src, virtualPath string) (*Symbol, error)

ReadString parses src (which must begin with "<?php") and uses virtualPath as the Symbol.File field and as context in error messages. Useful for unit tests where the source lives in a string literal.

Jump to

Keyboard shortcuts

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