ArrowFunction.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. {
  8. /** @var bool */
  9. public $static;
  10. /** @var bool */
  11. public $byRef;
  12. /** @var Node\Param[] */
  13. public $params = [];
  14. /** @var null|Node\Identifier|Node\Name|Node\NullableType|Node\UnionType */
  15. public $returnType;
  16. /** @var Expr */
  17. public $expr;
  18. /**
  19. * @param array $subNodes Array of the following optional subnodes:
  20. * 'static' => false : Whether the closure is static
  21. * 'byRef' => false : Whether to return by reference
  22. * 'params' => array() : Parameters
  23. * 'returnType' => null : Return type
  24. * 'expr' => Expr : Expression body
  25. * @param array $attributes Additional attributes
  26. */
  27. public function __construct(array $subNodes = [], array $attributes = []) {
  28. $this->attributes = $attributes;
  29. $this->static = $subNodes['static'] ?? false;
  30. $this->byRef = $subNodes['byRef'] ?? false;
  31. $this->params = $subNodes['params'] ?? [];
  32. $returnType = $subNodes['returnType'] ?? null;
  33. $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
  34. $this->expr = $subNodes['expr'] ?? null;
  35. }
  36. public function getSubNodeNames() : array {
  37. return ['static', 'byRef', 'params', 'returnType', 'expr'];
  38. }
  39. public function returnsByRef() : bool {
  40. return $this->byRef;
  41. }
  42. public function getParams() : array {
  43. return $this->params;
  44. }
  45. public function getReturnType() {
  46. return $this->returnType;
  47. }
  48. /**
  49. * @return Node\Stmt\Return_[]
  50. */
  51. public function getStmts() : array {
  52. return [new Node\Stmt\Return_($this->expr)];
  53. }
  54. public function getType() : string {
  55. return 'Expr_ArrowFunction';
  56. }
  57. }