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