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