Skip to content

Commit

Permalink
Specify more types
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 17, 2022
1 parent fc6b489 commit f98341f
Show file tree
Hide file tree
Showing 26 changed files with 209 additions and 110 deletions.
12 changes: 5 additions & 7 deletions lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace PhpParser;

use PhpParser\Parser\Tokens;

require __DIR__ . '/compatibility_tokens.php';

class Lexer {
Expand Down Expand Up @@ -36,11 +34,11 @@ class Lexer {
/**
* Creates a Lexer.
*
* @param array $options Options array. Currently only the 'usedAttributes' option is supported,
* which is an array of attributes to add to the AST nodes. Possible
* attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the
* first three. For more info see getNextToken() docs.
* @param array{usedAttributes?: string[]} $options Options array. Currently only the
* 'usedAttributes' option is supported, which is an array of attributes to add to the
* AST nodes. Possible attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the first three.
* For more info see getNextToken() docs.
*/
public function __construct(array $options = []) {
// map of tokens to drop while lexing (the map is only used for isset lookup,
Expand Down
8 changes: 4 additions & 4 deletions lib/PhpParser/Lexer/Emulative.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use PhpParser\PhpVersion;

class Emulative extends Lexer {
/** @var mixed[] Patches used to reverse changes introduced in the code */
/** @var array{int, string, string}[] Patches used to reverse changes introduced in the code */
private $patches = [];

/** @var TokenEmulator[] */
Expand All @@ -32,9 +32,9 @@ class Emulative extends Lexer {
private $hostPhpVersion;

/**
* @param mixed[] $options Lexer options. In addition to the usual options, accepts a
* 'phpVersion' (PhpVersion object or string) that specifies the
* version to emulate. Defaults to newest supported.
* @param array{usedAttributes?: string[], phpVersion?: PhpVersion|string} $options Lexer options.
* In addition to the usual options, accepts a 'phpVersion' (PhpVersion object or string)
* that specifies the version to emulate. Defaults to newest supported.
*/
public function __construct(array $options = []) {
$version = $options['phpVersion'] ?? PhpVersion::getNewestSupported();
Expand Down
2 changes: 2 additions & 0 deletions lib/PhpParser/Lexer/TokenEmulator/TokenEmulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ abstract public function isEmulationNeeded(string $code): bool;
abstract public function emulate(string $code, array $tokens): array;

/**
* @param Token[] $tokens Original tokens
* @return Token[] Modified Tokens
*/
abstract public function reverseEmulate(string $code, array $tokens): array;

/** @param array{int, string, string}[] $patches */
public function preprocessCode(string $code, array &$patches): string {
return $code;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function startNamespace(?Name $namespace = null): void {
* @param Name $name Original name
* @param string $aliasName Aliased name
* @param int $type One of Stmt\Use_::TYPE_*
* @param array $errorAttrs Attributes to use to report an error
* @param array<string, mixed> $errorAttrs Attributes to use to report an error
*/
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []): void {
// Constant names are case sensitive, everything else case insensitive
Expand Down
23 changes: 15 additions & 8 deletions lib/PhpParser/Node/Expr/ArrowFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ class ArrowFunction extends Expr implements FunctionLike {
public $attrGroups;

/**
* @param array $subNodes Array of the following optional subnodes:
* 'static' => false : Whether the closure is static
* 'byRef' => false : Whether to return by reference
* 'params' => array() : Parameters
* 'returnType' => null : Return type
* 'expr' => Expr : Expression body
* 'attrGroups' => array() : PHP attribute groups
* @param array{
* expr: Expr,
* static?: bool,
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* attrGroups?: Node\AttributeGroup[]
* } $subNodes Array of the following subnodes:
* 'expr' : Expression body
* 'static' => false : Whether the closure is static
* 'byRef' => false : Whether to return by reference
* 'params' => array() : Parameters
* 'returnType' => null : Return type
* 'attrGroups' => array() : PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(array $subNodes = [], array $attributes = []) {
public function __construct(array $subNodes, array $attributes = []) {
$this->attributes = $attributes;
$this->static = $subNodes['static'] ?? false;
$this->byRef = $subNodes['byRef'] ?? false;
Expand Down
24 changes: 16 additions & 8 deletions lib/PhpParser/Node/Expr/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ class Closure extends Expr implements FunctionLike {
/**
* Constructs a lambda function node.
*
* @param array $subNodes Array of the following optional subnodes:
* 'static' => false : Whether the closure is static
* 'byRef' => false : Whether to return by reference
* 'params' => array(): Parameters
* 'uses' => array(): use()s
* 'returnType' => null : Return type
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attributes groups
* @param array{
* static?: bool,
* byRef?: bool,
* params?: Node\Param[],
* uses?: ClosureUse[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'static' => false : Whether the closure is static
* 'byRef' => false : Whether to return by reference
* 'params' => array(): Parameters
* 'uses' => array(): use()s
* 'returnType' => null : Return type
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attributes groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(array $subNodes = [], array $attributes = []) {
Expand Down
5 changes: 3 additions & 2 deletions lib/PhpParser/Node/Expr/ShellExec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace PhpParser\Node\Expr;

use PhpParser\Node\Expr;
use PhpParser\Node\InterpolatedStringPart;

class ShellExec extends Expr {
/** @var array Encapsed string array */
/** @var (Expr|InterpolatedStringPart)[] Interpolated string array */
public $parts;

/**
* Constructs a shell exec (backtick) node.
*
* @param array $parts Encapsed string array
* @param (Expr|InterpolatedStringPart)[] $parts Interpolated string array
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(array $parts, array $attributes = []) {
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/Node/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Identifier extends NodeAbstract {
/** @var string Identifier as string */
public $name;

/** @var array<string, bool> */
private static $specialClassNames = [
'self' => true,
'parent' => true,
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/Node/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Name extends NodeAbstract {
/** @var string[] Parts of the name */
public $parts;

/** @var array<string, bool> */
private static $specialClassNames = [
'self' => true,
'parent' => true,
Expand Down
2 changes: 2 additions & 0 deletions lib/PhpParser/Node/Scalar/String_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class String_ extends Scalar {
/** @var string String value */
public $value;

/** @var array<string, string> Escaped character to its decoded value */
protected static $replacements = [
'\\' => '\\',
'$' => '$',
Expand Down Expand Up @@ -42,6 +43,7 @@ public function getSubNodeNames(): array {
}

/**
* @param array<string, mixed> $attributes
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
*/
public static function fromString(string $str, array $attributes = [], bool $parseUnicodeEscape = true): self {
Expand Down
22 changes: 15 additions & 7 deletions lib/PhpParser/Node/Stmt/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike {
/** @var Node\AttributeGroup[] PHP attribute groups */
public $attrGroups;

/** @var array<string, bool> */
private static $magicNames = [
'__construct' => true,
'__destruct' => true,
Expand All @@ -46,13 +47,20 @@ class ClassMethod extends Node\Stmt implements FunctionLike {
* Constructs a class method node.
*
* @param string|Node\Identifier $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'flags => 0 : Flags
* 'byRef' => false : Whether to return by reference
* 'params' => array() : Parameters
* 'returnType' => null : Return type
* 'stmts' => array() : Statements
* 'attrGroups' => array() : PHP attribute groups
* @param array{
* flags?: int,
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'flags => 0 : Flags
* 'byRef' => false : Whether to return by reference
* 'params' => array() : Parameters
* 'returnType' => null : Return type
* 'stmts' => array() : Statements
* 'attrGroups' => array() : PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
18 changes: 12 additions & 6 deletions lib/PhpParser/Node/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ class Class_ extends ClassLike {
* Constructs a class node.
*
* @param string|Node\Identifier|null $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'flags' => 0 : Flags
* 'extends' => null : Name of extended class
* 'implements' => array(): Names of implemented interfaces
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array{
* flags?: int,
* extends?: Node\Name|null,
* implements?: Node\Name[],
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'flags' => 0 : Flags
* 'extends' => null : Name of extended class
* 'implements' => array(): Names of implemented interfaces
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
17 changes: 11 additions & 6 deletions lib/PhpParser/Node/Stmt/Enum_.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ class Enum_ extends ClassLike {
public $implements;

/**
* @param string|Node\Identifier|null $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'scalarType' => null : Scalar type
* 'implements' => array() : Names of implemented interfaces
* 'stmts' => array() : Statements
* 'attrGroups' => array() : PHP attribute groups
* @param string|Node\Identifier|null $name Name
* @param array{
* scalarType?: Node\Identifier|null,
* implements?: Node\Name[],
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'scalarType' => null : Scalar type
* 'implements' => array() : Names of implemented interfaces
* 'stmts' => array() : Statements
* 'attrGroups' => array() : PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
15 changes: 10 additions & 5 deletions lib/PhpParser/Node/Stmt/For_.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ class For_ extends Node\Stmt {
/**
* Constructs a for loop node.
*
* @param array $subNodes Array of the following optional subnodes:
* 'init' => array(): Init expressions
* 'cond' => array(): Loop conditions
* 'loop' => array(): Loop expressions
* 'stmts' => array(): Statements
* @param array{
* init?: Node\Expr[],
* cond?: Node\Expr[],
* loop?: Node\Expr[],
* stmts?: Node\Stmt[],
* } $subNodes Array of the following optional subnodes:
* 'init' => array(): Init expressions
* 'cond' => array(): Loop conditions
* 'loop' => array(): Loop expressions
* 'stmts' => array(): Statements
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(array $subNodes = [], array $attributes = []) {
Expand Down
12 changes: 8 additions & 4 deletions lib/PhpParser/Node/Stmt/Foreach_.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class Foreach_ extends Node\Stmt {
*
* @param Node\Expr $expr Expression to iterate
* @param Node\Expr $valueVar Variable to assign value to
* @param array $subNodes Array of the following optional subnodes:
* 'keyVar' => null : Variable to assign key to
* 'byRef' => false : Whether to assign value by reference
* 'stmts' => array(): Statements
* @param array{
* keyVar?: Node\Expr|null,
* byRef?: bool,
* stmts?: Node\Stmt[],
* } $subNodes Array of the following optional subnodes:
* 'keyVar' => null : Variable to assign key to
* 'byRef' => false : Whether to assign value by reference
* 'stmts' => array(): Statements
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node\Expr $expr, Node\Expr $valueVar, array $subNodes = [], array $attributes = []) {
Expand Down
18 changes: 12 additions & 6 deletions lib/PhpParser/Node/Stmt/Function_.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ class Function_ extends Node\Stmt implements FunctionLike {
* Constructs a function node.
*
* @param string|Node\Identifier $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'byRef' => false : Whether to return by reference
* 'params' => array(): Parameters
* 'returnType' => null : Return type
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array{
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'byRef' => false : Whether to return by reference
* 'params' => array(): Parameters
* 'returnType' => null : Return type
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
14 changes: 9 additions & 5 deletions lib/PhpParser/Node/Stmt/If_.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class If_ extends Node\Stmt {
/**
* Constructs an if node.
*
* @param Node\Expr $cond Condition
* @param array $subNodes Array of the following optional subnodes:
* 'stmts' => array(): Statements
* 'elseifs' => array(): Elseif clauses
* 'else' => null : Else clause
* @param Node\Expr $cond Condition
* @param array{
* stmts?: Node\Stmt[],
* elseifs?: ElseIf_[],
* else?: Else_|null,
* } $subNodes Array of the following optional subnodes:
* 'stmts' => array(): Statements
* 'elseifs' => array(): Elseif clauses
* 'else' => null : Else clause
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node\Expr $cond, array $subNodes = [], array $attributes = []) {
Expand Down
12 changes: 8 additions & 4 deletions lib/PhpParser/Node/Stmt/Interface_.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class Interface_ extends ClassLike {
* Constructs a class node.
*
* @param string|Node\Identifier $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'extends' => array(): Name of extended interfaces
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array{
* extends?: Node\Name[],
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'extends' => array(): Name of extended interfaces
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
9 changes: 6 additions & 3 deletions lib/PhpParser/Node/Stmt/Trait_.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ class Trait_ extends ClassLike {
* Constructs a trait node.
*
* @param string|Node\Identifier $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array{
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
* 'stmts' => array(): Statements
* 'attrGroups' => array(): PHP attribute groups
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
Expand Down
Loading

0 comments on commit f98341f

Please sign in to comment.