ArrowFunction.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /** @var Node\AttributeGroup[] */
  19. public $attrGroups;
  20. /**
  21. * @param array $subNodes Array of the following optional subnodes:
  22. * 'static' => false : Whether the closure is static
  23. * 'byRef' => false : Whether to return by reference
  24. * 'params' => array() : Parameters
  25. * 'returnType' => null : Return type
  26. * 'expr' => Expr : Expression body
  27. * 'attrGroups' => array() : PHP attribute groups
  28. * @param array $attributes Additional attributes
  29. */
  30. public function __construct(array $subNodes = [], array $attributes = []) {
  31. $this->attributes = $attributes;
  32. $this->static = $subNodes['static'] ?? false;
  33. $this->byRef = $subNodes['byRef'] ?? false;
  34. $this->params = $subNodes['params'] ?? [];
  35. $returnType = $subNodes['returnType'] ?? null;
  36. $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
  37. $this->expr = $subNodes['expr'] ?? null;
  38. $this->attrGroups = $subNodes['attrGroups'] ?? [];
  39. }
  40. public function getSubNodeNames() : array {
  41. return ['attrGroups', 'static', 'byRef', 'params', 'returnType', 'expr'];
  42. }
  43. public function returnsByRef() : bool {
  44. return $this->byRef;
  45. }
  46. public function getParams() : array {
  47. return $this->params;
  48. }
  49. public function getReturnType() {
  50. return $this->returnType;
  51. }
  52. public function getAttrGroups() : array {
  53. return $this->attrGroups;
  54. }
  55. /**
  56. * @return Node\Stmt\Return_[]
  57. */
  58. public function getStmts() : array {
  59. return [new Node\Stmt\Return_($this->expr)];
  60. }
  61. public function getType() : string {
  62. return 'Expr_ArrowFunction';
  63. }
  64. }