Function_.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node;
  4. use PhpParser\Node\FunctionLike;
  5. /**
  6. * @property Node\Name $namespacedName Namespaced name (if using NameResolver)
  7. */
  8. class Function_ extends Node\Stmt implements FunctionLike
  9. {
  10. /** @var bool Whether function returns by reference */
  11. public $byRef;
  12. /** @var Node\Identifier Name */
  13. public $name;
  14. /** @var Node\Param[] Parameters */
  15. public $params;
  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 function node.
  22. *
  23. * @param string|Node\Identifier $name Name
  24. * @param array $subNodes Array of the following optional subnodes:
  25. * 'byRef' => false : Whether to return by reference
  26. * 'params' => array(): Parameters
  27. * 'returnType' => null : Return type
  28. * 'stmts' => array(): Statements
  29. * @param array $attributes Additional attributes
  30. */
  31. public function __construct($name, array $subNodes = [], array $attributes = []) {
  32. parent::__construct($attributes);
  33. $this->byRef = $subNodes['byRef'] ?? false;
  34. $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
  35. $this->params = $subNodes['params'] ?? [];
  36. $returnType = $subNodes['returnType'] ?? null;
  37. $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
  38. $this->stmts = $subNodes['stmts'] ?? [];
  39. }
  40. public function getSubNodeNames() : array {
  41. return ['byRef', 'name', 'params', 'returnType', 'stmts'];
  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. /** @return Node\Stmt[] */
  53. public function getStmts() : array {
  54. return $this->stmts;
  55. }
  56. public function getType() : string {
  57. return 'Stmt_Function';
  58. }
  59. }