xref: /PHP-Parser/lib/PhpParser/Node/Expr/Closure.php (revision ea778075)
1<?php declare(strict_types=1);
2
3namespace PhpParser\Node\Expr;
4
5use PhpParser\Node;
6use PhpParser\Node\ClosureUse;
7use PhpParser\Node\Expr;
8use PhpParser\Node\FunctionLike;
9
10class Closure extends Expr implements FunctionLike {
11    /** @var bool Whether the closure is static */
12    public bool $static;
13    /** @var bool Whether to return by reference */
14    public bool $byRef;
15    /** @var Node\Param[] Parameters */
16    public array $params;
17    /** @var ClosureUse[] use()s */
18    public array $uses;
19    /** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
20    public ?Node $returnType;
21    /** @var Node\Stmt[] Statements */
22    public array $stmts;
23    /** @var Node\AttributeGroup[] PHP attribute groups */
24    public array $attrGroups;
25
26    /**
27     * Constructs a lambda function node.
28     *
29     * @param array{
30     *     static?: bool,
31     *     byRef?: bool,
32     *     params?: Node\Param[],
33     *     uses?: ClosureUse[],
34     *     returnType?: null|Node\Identifier|Node\Name|Node\ComplexType,
35     *     stmts?: Node\Stmt[],
36     *     attrGroups?: Node\AttributeGroup[],
37     * } $subNodes Array of the following optional subnodes:
38     *             'static'     => false  : Whether the closure is static
39     *             'byRef'      => false  : Whether to return by reference
40     *             'params'     => array(): Parameters
41     *             'uses'       => array(): use()s
42     *             'returnType' => null   : Return type
43     *             'stmts'      => array(): Statements
44     *             'attrGroups' => array(): PHP attributes groups
45     * @param array<string, mixed> $attributes Additional attributes
46     */
47    public function __construct(array $subNodes = [], array $attributes = []) {
48        $this->attributes = $attributes;
49        $this->static = $subNodes['static'] ?? false;
50        $this->byRef = $subNodes['byRef'] ?? false;
51        $this->params = $subNodes['params'] ?? [];
52        $this->uses = $subNodes['uses'] ?? [];
53        $this->returnType = $subNodes['returnType'] ?? null;
54        $this->stmts = $subNodes['stmts'] ?? [];
55        $this->attrGroups = $subNodes['attrGroups'] ?? [];
56    }
57
58    public function getSubNodeNames(): array {
59        return ['attrGroups', 'static', 'byRef', 'params', 'uses', 'returnType', 'stmts'];
60    }
61
62    public function returnsByRef(): bool {
63        return $this->byRef;
64    }
65
66    public function getParams(): array {
67        return $this->params;
68    }
69
70    public function getReturnType() {
71        return $this->returnType;
72    }
73
74    /** @return Node\Stmt[] */
75    public function getStmts(): array {
76        return $this->stmts;
77    }
78
79    public function getAttrGroups(): array {
80        return $this->attrGroups;
81    }
82
83    public function getType(): string {
84        return 'Expr_Closure';
85    }
86}
87