ArrowFunction.php 2.5 KB

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