Foreach_.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node;
  4. class Foreach_ extends Node\Stmt {
  5. /** @var Node\Expr Expression to iterate */
  6. public Node\Expr $expr;
  7. /** @var null|Node\Expr Variable to assign key to */
  8. public ?Node\Expr $keyVar;
  9. /** @var bool Whether to assign value by reference */
  10. public bool $byRef;
  11. /** @var Node\Expr Variable to assign value to */
  12. public Node\Expr $valueVar;
  13. /** @var Node\Stmt[] Statements */
  14. public array $stmts;
  15. /**
  16. * Constructs a foreach node.
  17. *
  18. * @param Node\Expr $expr Expression to iterate
  19. * @param Node\Expr $valueVar Variable to assign value to
  20. * @param array{
  21. * keyVar?: Node\Expr|null,
  22. * byRef?: bool,
  23. * stmts?: Node\Stmt[],
  24. * } $subNodes Array of the following optional subnodes:
  25. * 'keyVar' => null : Variable to assign key to
  26. * 'byRef' => false : Whether to assign value by reference
  27. * 'stmts' => array(): Statements
  28. * @param array<string, mixed> $attributes Additional attributes
  29. */
  30. public function __construct(Node\Expr $expr, Node\Expr $valueVar, array $subNodes = [], array $attributes = []) {
  31. $this->attributes = $attributes;
  32. $this->expr = $expr;
  33. $this->keyVar = $subNodes['keyVar'] ?? null;
  34. $this->byRef = $subNodes['byRef'] ?? false;
  35. $this->valueVar = $valueVar;
  36. $this->stmts = $subNodes['stmts'] ?? [];
  37. }
  38. public function getSubNodeNames(): array {
  39. return ['expr', 'keyVar', 'byRef', 'valueVar', 'stmts'];
  40. }
  41. public function getType(): string {
  42. return 'Stmt_Foreach';
  43. }
  44. }