Closure.php 2.8 KB

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