format

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: 9 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 format lays out PHP source code: indentation, spaces, line breaks, braces, blank lines and the token spellings a style guide fixes.

One pass over the token stream decides the whitespace between every two significant tokens, using the syntax tree for context. The pass yields the formatted output and the list of changes that produce it, so a check reports exactly what a fix writes.

Index

Constants

View Source
const DefaultPreset = "per"

DefaultPreset is the preset used when nothing names one.

Variables

This section is empty.

Functions

func PresetReference

func PresetReference(name string) string

PresetReference returns the style guide a preset follows.

func Presets

func Presets() []string

Presets returns the preset names, sorted.

Types

type Change

type Change struct {
	Rule  string // the setting behind the edit
	Start uint32
	End   uint32
	New   string
	// Report marks a finding formatting cannot fix: it edits nothing.
	Report bool
	// Group, when not 0, links edits that only work together (the two
	// brackets of an array, a rebuilt import block): apply all or none.
	Group int
}

Change is one edit of the source: [Start, End) becomes New.

func (Change) Message

func (c Change) Message() string

Message returns the description of the change.

type Kind

type Kind uint8

Kind is the value type of a setting.

const (
	KindBool Kind = iota
	KindInt
	KindEnum
	KindList
)

Setting kinds.

func (Kind) String

func (k Kind) String() string

type Layer

type Layer struct {
	Source string         // shown by --show-config, e.g. "custos.json"
	Values map[string]any // setting ID → value
}

Layer is one source of setting values, applied over the previous ones.

type Options

type Options struct {
	Settings *Settings
	PHP      phpversion.Version
}

Options configure Source.

type Resolved

type Resolved struct {
	Settings
	Preset string
	Source map[string]string // setting ID → source of its value
}

Resolved are settings plus where each value came from.

func Resolve

func Resolve(name string, allowRisky bool, layers ...Layer) (*Resolved, error)

Resolve builds the settings of preset name, then applies layers in order. Risky settings keep their neutral value unless allowRisky is set, in which case the preset's risky values apply first; a layer may always turn a risky setting on explicitly.

type Result

type Result struct {
	Output  []byte
	Changes []Change
	// Skipped explains why the file was left as written.
	Skipped string
}

Result is the outcome of formatting one file.

func Source

func Source(path string, src []byte, opt Options) Result

Source formats src, the content of the PHP file path.

func (Result) Changed

func (r Result) Changed() bool

Changed reports whether formatting changes the file or reports something on it.

type Setting

type Setting struct {
	ID          string
	Group       string
	Kind        Kind
	Values      []string // allowed values (enum, list)
	Max         int      // largest value (int)
	Risky       bool
	Description string
	// contains filtered or unexported fields
}

Setting describes one formatting setting.

func Lookup

func Lookup(id string) (*Setting, bool)

Lookup returns the setting with the given ID.

func Registry

func Registry() []*Setting

Registry returns every setting in documentation order.

func (*Setting) Get

func (d *Setting) Get(s *Settings) any

Get returns the value of the setting in s, as configuration writes it.

func (*Setting) Set

func (d *Setting) Set(s *Settings, v any) error

Set stores v (a JSON-decoded value or a command-line string) in s.

type Settings

type Settings struct {
	IndentStyle            string // space, tab, keep
	IndentSize             int
	TabWidth               int
	ContinuationIndent     string // one, keep
	LineEnding             string // lf, crlf, cr, keep
	FinalNewline           bool
	TrimTrailingWhitespace bool
	RemoveBOM              bool
	ClosingTag             string // remove, keep
	OpenTagOwnLine         bool
	BlankLineAfterOpenTag  bool
	MaxBlankLines          int // 0: no limit
	LineLength             int
	Wrap                   string // long, off
	JoinFittingLists       bool
	ReportLongLines        bool

	ClassBrace          string // next_line, same_line, keep
	FunctionBrace       string // next_line, same_line, keep
	ClosureBrace        string // same_line, next_line, keep
	AnonymousClassBrace string // same_line, next_line, keep
	ControlBrace        string // same_line, next_line, keep
	ElsePosition        string // same_line, next_line, keep
	CatchPosition       string // same_line, next_line, keep
	EmptyBody           string // inline, keep
	SingleLineClosures  bool

	StatementPerLine             bool
	SingleLineThrow              bool
	BlankLineAfterNamespace      bool
	BlankLineAfterImports        bool
	BlankLineBetweenImportGroups bool
	ClassOpenBlankLine           string // none, one, keep
	ClassCloseBlankLine          string // none, one, keep
	TrimBlockBlankLines          bool
	MethodSeparation             string // one, keep
	BlankLineAfterTraitUse       bool
	PropertySeparation           string // one, none, keep
	ConstantSeparation           string // one, none, keep
	BlankLineBefore              []string
	CaseSeparation               bool

	Spacing             bool
	ConcatSpace         string // one, none, keep
	BinaryOperatorSpace string // one, at_least_one
	CastSpace           string // one, none, keep
	NotOperatorSpace    string // none, one
	ArrowFunctionSpace  string // none, one, keep
	ClosureSpace        string // one, none

	KeywordCase       string // lower, keep
	ConstantCase      string // lower, upper, keep
	TypeCase          string // lower, keep
	MagicConstantCase string // upper, keep
	ShortCast         bool

	TrailingComma           []string
	SingleLineTrailingComma bool   // false: remove a trailing comma from single-line lists
	ArraySyntax             string // short, long, keep
	ListSyntax              string // short, long, keep
	ElseIf                  bool
	NewParentheses          string // add, remove, keep
	StandardizeNotEquals    bool
	HashComment             bool
	OperatorLinebreak       string // start, start_booleans, keep

	ImportOrder               string // alpha, length, none
	ImportGroups              string // class_function_const, const_class_function, none
	NoLeadingImportSlash      bool
	SingleImportPerStatement  bool
	SingleTraitPerStatement   bool
	ParameterAttributeLine    bool
	NullTypeLast              bool
	SemicolonLine             string // join, keep
	CommentSpacing            bool
	HexCase                   string // upper, keep
	BlankLineAfterThrow       bool
	CatchTypeSpace            string // one, none, keep
	FunctionSeparation        bool
	SingleLineClassDefinition bool
	TrimBracketBlankLines     bool
	SplitMultilineArrays      bool
	BranchComment             string // body, next
	CaseComment               string // body, label
	AssignmentChain           string // one, stairs
	CaseBodyLine              bool
	Visibility                string // all, methods_properties, keep
	AnonymousClassSpace       string // one, none, keep

	// Risky settings change tokens beyond layout; they apply only when
	// Risky allows them.
	Yoda                string // yoda, regular, keep
	SingleQuote         bool
	RemoveUnusedImports bool
	DeclareStrictTypes  bool
	IncrementStyle      string // pre, post, keep
	HeredocToNowdoc     bool
	OrderedTraits       bool
}

Settings are the resolved formatting settings of one file. Every field is one entry of the registry (see Registry); "keep" leaves the matching layout as written.

Directories

Path Synopsis
Package editorconfig resolves the EditorConfig properties of one file following https://spec.editorconfig.org/: it parses `.editorconfig` files, matches their section globs and merges their pairs by precedence.
Package editorconfig resolves the EditorConfig properties of one file following https://spec.editorconfig.org/: it parses `.editorconfig` files, matches their section globs and merges their pairs by precedence.

Jump to

Keyboard shortcuts

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