Function_.php 2.5 KB

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