abstract class Parser (View source)

Base class for actual parsers that extract informations from package controller files.

Properties

protected Strings $stringValidator

The string validator service to be used to check the validity of handles.

Methods

__construct(Strings $stringValidator)

Initialize the instance.

bool
canParseTokens(array $tokens)

Determine if this parser can be used for the specified PHP tokens of the package controlller.php file.

extractInfo(array $tokens)

Extract package details from the PHP tokens of the package controller.php file.

string
getControllerClassNameRegularExpression()

Get the regular expression that the controller class name must match (without the namespace).

string
getDefaultPackageMinimumCodeVersion()

Get the default minimum core version when the package doesn't specify it.

getPackageHandleFromClassName(array $tokens, string $className, int $classStart)

Extract the package handle from the fully-qualified class name.

array[]
findControllerClass(array $tokens)

Find the package controller class.

string
getPackageHandle(array $tokens, string $className, int $classStart, int $classEnd)

Get the package handle.

string
getPackageVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the package version.

string
getPackageName(array $tokens, string $className, int $classStart, int $classEnd)

Get the package name.

string
getPackageDescription(array $tokens, string $className, int $classStart, int $classEnd)

Get the package description.

string
getPackageMinimumCodeVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the minimum supported core version.

string
getPackageMinimumPHPVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the minimum supported PHP version.

mixed
getPropertyValue(array $tokens, string $propertyName, int $classStart, int $classEnd)

Get the value of a class property.

mixed
getSimpleMethodReturnValue(array $tokens, string $methodName, int $classStart, int $classEnd)

Get the return value of a class method.

int[]|null
getSimpleMethodBodyTokenRange(array $tokens, string $methodName, int $classStart, int $classEnd)

Get token range of the body of a very simple class method (that is, containing only a "return something;").

bool
isVersionValid(string|mixed $version)

Check if a version is valid.

int|null
findTypedToken(array $tokens, array $tokenIDs, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Find the index of a token given its type.

int|null
findTextToken(array $tokens, array $tokenContent, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Find the index of a text token given its contents.

array
stripNonCodeTokens(array $tokens, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Strip whitespaces and comments from a list of tokens.

array
stripTFunctionCall(array $tokens)

Strip t() function calls at the beginning of a list of tokens tokens (if any).

array
stripEnclosingParenthesis(array $tokens)

Strip enclosing parenthesis ("(...)") (if any) in a list of tokens.

int|null
skipNonCodeTokens(array $tokens, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Skip to the next non-whitespace and non-comment token.

mixed
decodeValueToken(array $token, string $associatedName)

Get the actual PHP value described by a value token.

string
decodeString(string $string)

Decode the value of a T_CONSTANT_ENCAPSED_STRING token.

Details

__construct(Strings $stringValidator)

Initialize the instance.

Parameters

Strings $stringValidator

the string validator service to be used to check the validity of handles

abstract bool canParseTokens(array $tokens)

Determine if this parser can be used for the specified PHP tokens of the package controlller.php file.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

Return Value

bool

PackageInfo extractInfo(array $tokens)

Extract package details from the PHP tokens of the package controller.php file.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

Return Value

PackageInfo

Exceptions

Exception

abstract protected string getControllerClassNameRegularExpression()

Get the regular expression that the controller class name must match (without the namespace).

Return Value

string

abstract protected string getDefaultPackageMinimumCodeVersion()

Get the default minimum core version when the package doesn't specify it.

Return Value

string

abstract protected PackageInfo getPackageHandleFromClassName(array $tokens, string $className, int $classStart)

Extract the package handle from the fully-qualified class name.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

Return Value

PackageInfo

Exceptions

Exception

protected array[] findControllerClass(array $tokens)

Find the package controller class.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

Return Value

array[]

[$className, $classStart, $classEnd] returns the class name, the index of the first PHP token of the class body (its first '{'}, and the index of the last PHP token of the class body (its last '}'}

Exceptions

Exception

protected string getPackageHandle(array $tokens, string $className, int $classStart, int $classEnd)

Get the package handle.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected string getPackageVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the package version.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected string getPackageName(array $tokens, string $className, int $classStart, int $classEnd)

Get the package name.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected string getPackageDescription(array $tokens, string $className, int $classStart, int $classEnd)

Get the package description.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected string getPackageMinimumCodeVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the minimum supported core version.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected string getPackageMinimumPHPVersion(array $tokens, string $className, int $classStart, int $classEnd)

Get the minimum supported PHP version.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $className

the class name of the package controller

int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

string

Exceptions

Exception

protected mixed getPropertyValue(array $tokens, string $propertyName, int $classStart, int $classEnd)

Get the value of a class property.

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $propertyName
int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

mixed

Exceptions

Exception

protected mixed getSimpleMethodReturnValue(array $tokens, string $methodName, int $classStart, int $classEnd)

Get the return value of a class method.

Get the value returned from a very simple class method (that is, containing only a "return something;").

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $methodName
int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

mixed

Exceptions

Exception

protected int[]|null getSimpleMethodBodyTokenRange(array $tokens, string $methodName, int $classStart, int $classEnd)

Get token range of the body of a very simple class method (that is, containing only a "return something;").

Parameters

array $tokens

the PHP tokens of the package controlller.php file

string $methodName
int $classStart

the index of the first PHP token of the class body (its first '{')

int $classEnd

the index of the last PHP token of the class body (its last '}'}

Return Value

int[]|null

returns null if it can't be detected (for example, for abstract methods), the token index of the opening '{' and the token index of the closing '}' otherwise

Exceptions

Exception

protected bool isVersionValid(string|mixed $version)

Check if a version is valid.

Parameters

string|mixed $version

Return Value

bool

protected int|null findTypedToken(array $tokens, array $tokenIDs, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Find the index of a token given its type.

Parameters

array $tokens

the list of PHP tokens where the search should be performed

array $tokenIDs

the list of token identifiers to be searched

int $startIndexInclusive

the initial index where the search should start (inclusive)

int|null $endIndexExclusive

the final index where the search should end (exclusive); if null we'll search until the end of the token list

Return Value

int|null

the index of the first token with an ID included in $tokenIDs otherwise; return NULL of none of the token ID have been found

protected int|null findTextToken(array $tokens, array $tokenContent, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Find the index of a text token given its contents.

Parameters

array $tokens

the list of PHP tokens where the search should be performed

array $tokenContent

the list of token values to be searched

int $startIndexInclusive

the initial index where the search should start (inclusive)

int|null $endIndexExclusive

the final index where the search should end (exclusive); if null we'll search until the end of the token list

Return Value

int|null

the index of the first string token found; return NULL of none of the strings have been found

protected array stripNonCodeTokens(array $tokens, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Strip whitespaces and comments from a list of tokens.

Parameters

array $tokens

the list of PHP tokens to be processed

int $startIndexInclusive

the initial index where the search should start (inclusive)

int|null $endIndexExclusive

the final index where the search should end (exclusive); if null we'll search until the end of the token list

Return Value

array

protected array stripTFunctionCall(array $tokens)

Strip t() function calls at the beginning of a list of tokens tokens (if any).

Parameters

array $tokens

the list of PHP tokens to be processed

Return Value

array

protected array stripEnclosingParenthesis(array $tokens)

Strip enclosing parenthesis ("(...)") (if any) in a list of tokens.

Parameters

array $tokens

the list of PHP tokens to be processed

Return Value

array

protected int|null skipNonCodeTokens(array $tokens, int $startIndexInclusive = 0, int|null $endIndexExclusive = null)

Skip to the next non-whitespace and non-comment token.

Parameters

array $tokens

the whole list of tokens

int $startIndexInclusive

the initial index where the search should start (inclusive)

int|null $endIndexExclusive

the final index where the search should end (exclusive); if null we'll search until the end of the token list

Return Value

int|null

the next index of non-whitespace and non-comment token; NULL when arriving to the end of the array

protected mixed decodeValueToken(array $token, string $associatedName)

Get the actual PHP value described by a value token.

Parameters

array $token

the token containing the value

string $associatedName

the name associated to the value (used to display a useful error message),

Return Value

mixed

Exceptions

Exception

protected string decodeString(string $string)

Decode the value of a T_CONSTANT_ENCAPSED_STRING token.

Parameters

string $string

the value of a T_CONSTANT_ENCAPSED_STRING token

Return Value

string

Exceptions

Exception