Function_.php 2.6 KB

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