meta

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 12 Imported by: 12

Documentation

Index

Constants

View Source
const (

	// WStaticMethodCall type is "Wrap Static Method Call":
	// E.g. Class::doSomething()
	// Params: [Class name <string>] [Method name <string>]
	WStaticMethodCall byte = iota

	// WInstanceMethodCall is a method call on some expression.
	// You need to specify expression type (might be lazy type, e.g. <WStaticMethodCall, SomeClass, instance> ).
	// E.g. $var->callSomething()
	// Params: [Expression type <string>] [Method <string>]
	WInstanceMethodCall

	// WStaticPropertyFetch is a property fetch for static property :).
	// E.g. Test::$something
	// Params: [Class name <string>] [Property name with $ <string>]
	WStaticPropertyFetch

	// WClassConstFetch is a const fetch from a class.
	// E.g. Test::CONSTANT
	// Params: [Class name <string>] [Constant name <string>]
	WClassConstFetch

	// WInstancePropertyFetch is a property fetch from some instance.
	// You need to provide expression type, see example for WInstanceMethodCall.
	// E.g. $var->something
	// Params: [Expression type <string>] [Property name <string>]
	WInstancePropertyFetch

	// WFunctionCall represents a function call.
	// Function name must contain namespace. It will be first searched in the defined namespace
	// and then it will fall back to root namespace.
	// E.g. callSomething()
	// Params: [Function name with full namespace <string>]
	WFunctionCall

	// WArrayOf means that expression is array of another expression
	// E.g. <WArrayOf, string> would be normally written as "string[]"
	//      <WArrayOf, <WFunctionCall, callSomething>>
	// Params: [Expression type <string>]
	WArrayOf

	// WElemOf is the opposite of WArrayOf: it means the type of an element of the expression
	// E.g. $arr[0] would be "string" if $arr type is "string[]"
	// Params: [Expression type <string>]
	WElemOf

	// WElemOfKey is extended for of WElemOf where we also save the key that
	// was used during the indexing.
	// Params: [Expression type <string>] [Key <string>]
	WElemOfKey

	// WGlobal means global variable.
	// E.g. global $Something;
	// Params: [Global variable name <string>]
	WGlobal

	// WConstant means constant
	// e.g. type of MINUTE constant
	// Params: [Constant name <string>]
	WConstant

	// WBaseMethodParam is a way to inherit base type method type of nth parameter.
	// e.g. type of $x param of foo method from one of the implemented interfaces.
	// Params: [Index <uint8>] [Class name <string>] [Method name <string>]
	WBaseMethodParam

	// WMax must always be last to indicate which byte is the maximum value of a type byte
	WMax
)

Variables

View Source
var (
	UnknownValue = ConstValue{Type: Undefined}
	TrueValue    = ConstValue{Type: Bool, Value: true}
	FalseValue   = ConstValue{Type: Bool, Value: false}
)
View Source
var (
	MixedType = NewTypesMap("mixed").Immutable()
	VoidType  = NewTypesMap("void").Immutable()
	NullType  = NewTypesMap("null").Immutable()

	PreciseIntType    = NewPreciseTypesMap("int").Immutable()
	PreciseFloatType  = NewPreciseTypesMap("float").Immutable()
	PreciseBoolType   = NewPreciseTypesMap("bool").Immutable()
	PreciseStringType = NewPreciseTypesMap("string").Immutable()
)

Preallocated and shared immutable type maps.

View Source
var (

	// Info contains global meta information for all classes, functions, etc.
	Info info
)

Functions

func IsArrayType added in v0.3.0

func IsArrayType(s string) bool

func IsClassType added in v0.3.0

func IsClassType(s string) bool

func IsIndexingComplete

func IsIndexingComplete() bool

func IsInternalClass added in v0.2.0

func IsInternalClass(className string) bool

func IsLoadingStubs added in v0.3.0

func IsLoadingStubs() bool

IsLoadingStubs reports whether we're parsing stub files right now.

func IsShapeType added in v0.3.0

func IsShapeType(s string) bool

func NameNodeEquals

func NameNodeEquals(n ir.Node, s string) bool

NameNodeEquals checks whether n node name value is identical to s.

func NameNodeToString

func NameNodeToString(n ir.Node) string

NameNodeToString converts nodes of *name.Name, and *node.Identifier to string. This function is a helper function to aid printing function names, not for actual code analysis.

func OnIndexingComplete

func OnIndexingComplete(cb func())

func ResetInfo

func ResetInfo()

ResetInfo creates empty meta info

func SetIndexingComplete

func SetIndexingComplete(complete bool)

func SetLoadingStubs added in v0.3.0

func SetLoadingStubs(loading bool)

SetLoadingStubs changes IsLoadingStubs() return value.

Should be only called from linter.InitStubs() function.

func UnwrapArrayOf

func UnwrapArrayOf(s string) (typ string)

func UnwrapBaseMethodParam

func UnwrapBaseMethodParam(s string) (paramIndex uint8, className, methodName string)

func UnwrapClassConstFetch

func UnwrapClassConstFetch(s string) (className, constName string)

func UnwrapConstant

func UnwrapConstant(s string) (constName string)

func UnwrapElemOf

func UnwrapElemOf(s string) (typ string)

func UnwrapElemOfKey added in v0.3.0

func UnwrapElemOfKey(s string) (typ, key string)

func UnwrapFunctionCall

func UnwrapFunctionCall(s string) (funcName string)

func UnwrapGlobal

func UnwrapGlobal(s string) (varName string)

func UnwrapInstanceMethodCall

func UnwrapInstanceMethodCall(s string) (typ, methodName string)

func UnwrapInstancePropertyFetch

func UnwrapInstancePropertyFetch(s string) (typ, propName string)

func UnwrapStaticMethodCall

func UnwrapStaticMethodCall(s string) (className, methodName string)

func UnwrapStaticPropertyFetch

func UnwrapStaticPropertyFetch(s string) (className, propName string)

func WrapArray2 added in v0.2.0

func WrapArray2(ktyp, vtyp string) string

func WrapArrayOf

func WrapArrayOf(typ string) string

func WrapBaseMethodParam

func WrapBaseMethodParam(paramIndex int, className, methodName string) string

func WrapClassConstFetch

func WrapClassConstFetch(className, constName string) string

func WrapConstant

func WrapConstant(constName string) string

func WrapElemOf

func WrapElemOf(typ string) string

func WrapElemOfKey added in v0.3.0

func WrapElemOfKey(typ, key string) string

func WrapFunctionCall

func WrapFunctionCall(funcName string) string

func WrapGlobal

func WrapGlobal(varName string) string

func WrapInstanceMethodCall

func WrapInstanceMethodCall(typ, methodName string) string

func WrapInstancePropertyFetch

func WrapInstancePropertyFetch(typ, propName string) string

func WrapStaticMethodCall

func WrapStaticMethodCall(className, methodName string) string

func WrapStaticPropertyFetch

func WrapStaticPropertyFetch(className, propName string) string

Types

type AccessLevel

type AccessLevel int
const (
	Public AccessLevel = iota
	Protected
	Private
)

func (AccessLevel) String

func (l AccessLevel) String() string

type ClassFlags added in v0.2.0

type ClassFlags uint8
const (
	ClassAbstract ClassFlags = 1 << iota
	ClassFinal
	ClassShape
)

type ClassInfo

type ClassInfo struct {
	Pos              ElementPosition
	Name             string
	Flags            ClassFlags
	Parent           string
	ParentInterfaces []string // interfaces allow multiple inheritance
	Traits           map[string]struct{}
	Interfaces       map[string]struct{}
	Methods          FunctionsMap
	Properties       PropertiesMap // both instance and static properties are inside. Static properties have "$" prefix
	Constants        ConstantsMap
	Mixins           []string
}

func (*ClassInfo) IsAbstract added in v0.2.0

func (info *ClassInfo) IsAbstract() bool

func (*ClassInfo) IsShape added in v0.3.0

func (info *ClassInfo) IsShape() bool

type ClassParseState

type ClassParseState struct {
	IsTrait                 bool
	Namespace               string
	FunctionUses            map[string]string
	Uses                    map[string]string
	CurrentFile             string
	CurrentClass            string
	CurrentParentClass      string
	CurrentParentInterfaces []string // interfaces allow for multiple inheritance...
	CurrentFunction         string   // current method or function name
}

type ClassesMap

type ClassesMap struct {
	H map[lowercaseString]ClassInfo
}

func NewClassesMap added in v0.2.0

func NewClassesMap() ClassesMap

func (ClassesMap) Delete added in v0.2.0

func (m ClassesMap) Delete(name string)

func (ClassesMap) Get added in v0.2.0

func (m ClassesMap) Get(name string) (ClassInfo, bool)

func (ClassesMap) Len added in v0.2.0

func (m ClassesMap) Len() int

func (ClassesMap) Set added in v0.2.0

func (m ClassesMap) Set(name string, class ClassInfo)

type ConstInfo added in v0.3.0

type ConstInfo struct {
	Pos         ElementPosition
	Typ         TypesMap
	AccessLevel AccessLevel
	Value       ConstValue
}

type ConstValue added in v0.3.0

type ConstValue struct {
	Type  ConstValueType
	Value interface{}
}

ConstValue structure is used to store the value and type of a constant.

func NewBoolConst added in v0.3.0

func NewBoolConst(v bool) ConstValue

NewBoolConst returns a new constant value with the preset bool type and the passed value v.

func NewFloatConst added in v0.3.0

func NewFloatConst(v float64) ConstValue

NewFloatConst returns a new constant value with the preset float type and the passed value v.

func NewIntConst added in v0.3.0

func NewIntConst(v int64) ConstValue

NewIntConst returns a new constant value with the preset int type and the passed value v.

func NewStringConst added in v0.3.0

func NewStringConst(v string) ConstValue

NewStringConst returns a new constant value with the preset string type and the passed value v.

func (ConstValue) GetBool added in v0.3.0

func (c ConstValue) GetBool() bool

GetBool returns the value stored in c.Value cast to bool type.

Should be used with care, it can panic if the type is not equal to the required one. Usually used in places where the type has already been clearly defined and the probability of panic is 0.

func (ConstValue) GetFloat added in v0.3.0

func (c ConstValue) GetFloat() float64

GetFloat returns the value stored in c.Value cast to float type.

Should be used with care, it can panic if the type is not equal to the required one. Usually used in places where the type has already been clearly defined and the probability of panic is 0.

func (ConstValue) GetInt added in v0.3.0

func (c ConstValue) GetInt() int64

GetInt returns the value stored in c.Value cast to int type.

Should be used with care, it can panic if the type is not equal to the required one. Usually used in places where the type has already been clearly defined and the probability of panic is 0.

func (ConstValue) GetString added in v0.3.0

func (c ConstValue) GetString() string

GetString returns the value stored in c.Value cast to string type.

Should be used with care, it can panic if the type is not equal to the required one. Usually used in places where the type has already been clearly defined and the probability of panic is 0.

func (*ConstValue) GobDecode added in v0.3.0

func (c *ConstValue) GobDecode(buf []byte) error

func (ConstValue) GobEncode added in v0.3.0

func (c ConstValue) GobEncode() ([]byte, error)

func (ConstValue) IsEqual added in v0.3.0

func (c ConstValue) IsEqual(v ConstValue) bool

IsEqual checks for equality with the passed constant value.

If any of the constants are undefined, false is returned.

func (ConstValue) IsValid added in v0.3.0

func (c ConstValue) IsValid() bool

IsValid checks that the value is valid and its type is not undefined.

func (ConstValue) String added in v0.3.0

func (c ConstValue) String() string

func (ConstValue) ToBool added in v0.3.0

func (c ConstValue) ToBool() (value bool, ok bool)

ToBool converts x constant to boolean constants following PHP conversion rules. Second bool result tells whether that conversion was successful.

func (ConstValue) ToInt added in v0.3.0

func (c ConstValue) ToInt() (int64, bool)

ToInt converts x constant to int constants following PHP conversion rules. Second bool result tells whether that conversion was successful.

func (ConstValue) ToString added in v0.3.0

func (c ConstValue) ToString() (string, bool)

ToString converts x constant to string constants following PHP conversion rules. Second bool result tells whether that conversion was successful.

type ConstValueType added in v0.3.0

type ConstValueType uint8
const (
	Undefined ConstValueType = iota
	Integer
	Float
	String
	Bool
)

func (ConstValueType) String added in v0.3.0

func (i ConstValueType) String() string

type ConstantsMap

type ConstantsMap map[string]ConstInfo

type ElementPosition

type ElementPosition struct {
	Filename  string
	Line      int32
	EndLine   int32
	Character int32
	Length    int32 // body length
}

type FuncFlags added in v0.2.0

type FuncFlags uint8
const (
	FuncStatic FuncFlags = 1 << iota
	FuncPure
	FuncAbstract
	FuncFinal
)

type FuncInfo

type FuncInfo struct {
	Pos          ElementPosition
	Name         string
	Params       []FuncParam
	MinParamsCnt int
	Typ          TypesMap
	AccessLevel  AccessLevel
	Flags        FuncFlags
	ExitFlags    int // if function has exit/die/throw, then ExitFlags will be <> 0
	Doc          PhpDocInfo
}

func GetInternalFunctionInfo

func GetInternalFunctionInfo(fn string) (info FuncInfo, ok bool)

func (*FuncInfo) IsAbstract added in v0.2.0

func (info *FuncInfo) IsAbstract() bool

func (*FuncInfo) IsPure added in v0.2.0

func (info *FuncInfo) IsPure() bool

func (*FuncInfo) IsStatic added in v0.2.0

func (info *FuncInfo) IsStatic() bool

type FuncInfoOverride

type FuncInfoOverride struct {
	OverrideType OverrideType
	ArgNum       int
}

FuncInfoOverride defines return type overrides based on their parameter types. For example, \array_slice($arr) returns type of element (OverrideElementType) of the ArgNum=0

func GetInternalFunctionOverrideInfo

func GetInternalFunctionOverrideInfo(fn string) (info FuncInfoOverride, ok bool)

type FuncParam

type FuncParam struct {
	IsRef bool
	Name  string
	Typ   TypesMap
}

type FunctionsMap

type FunctionsMap struct {
	H map[lowercaseString]FuncInfo
}

func NewFunctionsMap added in v0.2.0

func NewFunctionsMap() FunctionsMap

func (FunctionsMap) Delete added in v0.2.0

func (m FunctionsMap) Delete(name string)

func (FunctionsMap) Get added in v0.2.0

func (m FunctionsMap) Get(name string) (FuncInfo, bool)

func (FunctionsMap) Len added in v0.2.0

func (m FunctionsMap) Len() int

func (FunctionsMap) Set added in v0.2.0

func (m FunctionsMap) Set(name string, fn FuncInfo)

type FunctionsOverrideMap

type FunctionsOverrideMap map[string]FuncInfoOverride

type OverrideType

type OverrideType int
const (
	// OverrideArgType means that return type of a function is the same as the type of the argument
	OverrideArgType OverrideType = iota
	// OverrideElementType means that return type of a function is the same as the type of an element of the argument
	OverrideElementType
	// OverrideClassType means that return type of a function is the same as the type represented by the class name.
	OverrideClassType
)

type PerFile

type PerFile struct {
	Traits    ClassesMap
	Classes   ClassesMap
	Functions FunctionsMap
	Constants ConstantsMap
}

PerFile contains all meta information about the specified file

type PhpDocInfo

type PhpDocInfo struct {
	Deprecated      bool
	DeprecationNote string
}

type PropertiesMap

type PropertiesMap map[string]PropertyInfo

type PropertyInfo

type PropertyInfo struct {
	Pos         ElementPosition
	Typ         TypesMap
	AccessLevel AccessLevel
}

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope contains variables with their types in the respective scope

func NewScope

func NewScope() *Scope

NewScope creates new empty scope

func (*Scope) AddVar

func (s *Scope) AddVar(v ir.Node, typ TypesMap, reason string, flags VarFlags)

AddVar adds variable with specified types to scope

func (*Scope) AddVarFromPHPDoc

func (s *Scope) AddVarFromPHPDoc(name string, typ TypesMap, reason string)

AddVarFromPHPDoc adds variable with specified types to the scope

func (*Scope) AddVarName

func (s *Scope) AddVarName(name string, typ TypesMap, reason string, flags VarFlags)

AddVarName adds variable with specified types to the scope

func (*Scope) Clone

func (s *Scope) Clone() *Scope

Clone creates a full scope copy (used in branches)

func (*Scope) DelVar

func (s *Scope) DelVar(v ir.Node, reason string)

DelVar deletes specified variable from scope

func (*Scope) DelVarName

func (s *Scope) DelVarName(name, reason string)

DelVarName deletes variable from the scope by it's name

func (*Scope) GetVarNameType

func (s *Scope) GetVarNameType(name string) (m TypesMap, ok bool)

GetVarNameType returns type map for variable if it exists

func (*Scope) GobDecode

func (s *Scope) GobDecode(buf []byte) error

GobDecode is custom gob unmarshaller

func (*Scope) GobEncode

func (s *Scope) GobEncode() ([]byte, error)

GobEncode is a custom gob marshaller

func (*Scope) HaveVar

func (s *Scope) HaveVar(v ir.Node) bool

HaveVar checks whether or not specified variable is present in the scope and that it is always defined

func (*Scope) HaveVarName

func (s *Scope) HaveVarName(name string) bool

HaveVarName checks whether or not specified variable is present in the scope and that it is always defined

func (*Scope) IsInClosure

func (s *Scope) IsInClosure() bool

IsInClosure returns whether or not this scope is inside a closure and thus $this can be late-bound.

func (*Scope) IsInInstanceMethod

func (s *Scope) IsInInstanceMethod() bool

IsInInstanceMethod returns whether or not this scope exists in instance method (and thus closures must capture $this)

func (*Scope) Iterate

func (s *Scope) Iterate(cb func(varName string, typ TypesMap, flags VarFlags))

func (*Scope) Len

func (s *Scope) Len() int

func (*Scope) MaybeHaveVar

func (s *Scope) MaybeHaveVar(v ir.Node) bool

MaybeHaveVar checks that variable is present in the scope (it may be not always defined)

func (*Scope) MaybeHaveVarName

func (s *Scope) MaybeHaveVarName(name string) bool

MaybeHaveVarName checks that variable is present in the scope (it may be not always defined)

func (*Scope) ReplaceVar

func (s *Scope) ReplaceVar(v ir.Node, typ TypesMap, reason string, flags VarFlags)

ReplaceVar replaces variable with specified types to scope

func (*Scope) ReplaceVarName

func (s *Scope) ReplaceVarName(name string, typ TypesMap, reason string, flags VarFlags)

ReplaceVarName replaces variable with specified types to the scope

func (*Scope) SetInClosure

func (s *Scope) SetInClosure(v bool)

SetInClosure updates "inClosure" flag that indicates whether or not we are inside a closure and thus late $this binding is possible.

func (*Scope) SetInInstanceMethod

func (s *Scope) SetInInstanceMethod(v bool)

SetInInstanceMethod updates "inInstanceMethod" flag that indicated whether or not scope is located inside instance method and that "$this" needs to be captured

func (*Scope) String

func (s *Scope) String() string

String returns vars contents (for debug purposes)

type Type added in v0.3.0

type Type struct {
	Elem string
	Dims int
}

type TypesMap

type TypesMap struct {
	// contains filtered or unexported fields
}

TypesMap holds a set of types and can be made immutable to prevent unexpected changes.

func MergeTypeMaps

func MergeTypeMaps(maps ...TypesMap) TypesMap

MergeTypeMaps creates a new types map from union of specified type maps

func NewEmptyTypesMap

func NewEmptyTypesMap(cap int) TypesMap

NewEmptyTypesMap creates new type map that has no types in it

func NewPreciseTypesMap added in v0.3.0

func NewPreciseTypesMap(str string) TypesMap

func NewTypesMap

func NewTypesMap(str string) TypesMap

NewTypesMap returns new TypesMap that is initialized with the provided types (separated by "|" symbol)

func NewTypesMapFromMap

func NewTypesMapFromMap(m map[string]struct{}) TypesMap

NewTypesMapFromMap creates TypesMap from provided map[string]struct{}

func NewTypesMapFromTypes added in v0.3.0

func NewTypesMapFromTypes(types []Type) TypesMap

func (TypesMap) Append

func (m TypesMap) Append(n TypesMap) TypesMap

Append adds provided types to current map and returns new one (immutable maps are always copied)

func (TypesMap) ArrayElemLazyType added in v0.3.0

func (m TypesMap) ArrayElemLazyType() TypesMap

ArrayElemLazyType returns type of array element. T[] -> T, T[][] -> T[]. For *Lazy* type.

func (TypesMap) Clone added in v0.3.0

func (m TypesMap) Clone() TypesMap

func (TypesMap) Contains added in v0.3.0

func (m TypesMap) Contains(typ string) bool

func (TypesMap) Equals added in v0.2.0

func (m TypesMap) Equals(m2 TypesMap) bool

Equals check if two typesmaps are the same

func (TypesMap) Find

func (m TypesMap) Find(pred func(typ string) bool) bool

Find applies a predicate function to every contained type. If callback returns true for any of them, this is a result of Find call. False is returned if none of the contained types made pred function return true.

func (*TypesMap) GobDecode

func (m *TypesMap) GobDecode(buf []byte) error

GobDecode is custom gob unmarshaller

func (TypesMap) GobEncode

func (m TypesMap) GobEncode() ([]byte, error)

GobEncode is a custom gob marshaller

func (TypesMap) Immutable

func (m TypesMap) Immutable() TypesMap

Immutable returns immutable view of TypesMap

func (TypesMap) Is

func (m TypesMap) Is(typ string) bool

Is reports whether m contains exactly one specified type.

Warning: typ must be a proper *lazy* or *solved* type.

func (TypesMap) IsArray added in v0.2.0

func (m TypesMap) IsArray() bool

IsArray checks if map contains only array of any type

Warning: use only for *lazy* types!

func (TypesMap) IsArrayOf added in v0.2.0

func (m TypesMap) IsArrayOf(typ string) bool

IsArrayOf checks if map contains only array of given type

Warning: use only for *lazy* types!

func (TypesMap) IsEmpty

func (m TypesMap) IsEmpty() bool

IsEmpty checks if map has no types at all

func (TypesMap) IsPrecise added in v0.3.0

func (m TypesMap) IsPrecise() bool

IsPrecise reports whether the type set represented by the map is precise enough to perform typecheck-like analysis.

Type precision determined by a type information source. For example, Int literal has a precise type of `int`, while having a phpdoc that promises some variable to have type `T` is not precise enough.

Adding an imprecise type to a types map makes the entire type map imprecise.

Important invariant: a precise map contains no lazy types.

func (TypesMap) IsResolved added in v0.3.0

func (m TypesMap) IsResolved() bool

IsResolved reports whether all types inside types map are resolved.

Users should not depend on the "false" result meaning. If "true" is returned, TypesMap is guaranteed to be free of lazy types.

func (TypesMap) Iterate

func (m TypesMap) Iterate(cb func(typ string))

Iterate applies cb to all contained types

func (TypesMap) Len

func (m TypesMap) Len() int

Len returns number of different types in map

func (TypesMap) Map added in v0.3.0

func (m TypesMap) Map(fn func(string) string) TypesMap

Map returns a new types map with the results of calling fn for every type contained inside m. The result type map is never marked as precise.

func (*TypesMap) MarkAsImprecise added in v0.3.0

func (m *TypesMap) MarkAsImprecise()

func (TypesMap) String

func (m TypesMap) String() string

String returns string representation of a map

type VarFlags added in v0.3.0

type VarFlags uint8
const (
	VarAlwaysDefined VarFlags
)

func (VarFlags) IsAlwaysDefined added in v0.3.0

func (flags VarFlags) IsAlwaysDefined() bool

func (VarFlags) IsNoReplace added in v0.3.0

func (flags VarFlags) IsNoReplace() bool

func (*VarFlags) SetAlwaysDefined added in v0.3.0

func (flags *VarFlags) SetAlwaysDefined(v bool)

Jump to

Keyboard shortcuts

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