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