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. class Function_ extends Node\Stmt implements FunctionLike
  6. {
  7. /** @var bool Whether function returns by reference */
  8. public $byRef;
  9. /** @var Node\Identifier Name */
  10. public $name;
  11. /** @var Node\Param[] Parameters */
  12. public $params;
  13. /** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
  14. public $returnType;
  15. /** @var Node\Stmt[] Statements */
  16. public $stmts;
  17. /** @var Node\AttributeGroup[] PHP attribute groups */
  18. public $attrGroups;
  19. /** @var Node\Name Namespaced name (if using NameResolver) */
  20. public $namespacedName;
  21. /**
  22. * Constructs a function node.
  23. *
  24. * @param string|Node\Identifier $name Name
  25. * @param array $subNodes Array of the following optional subnodes:
  26. * 'byRef' => false : Whether to return by reference
  27. * 'params' => array(): Parameters
  28. * 'returnType' => null : Return type
  29. * 'stmts' => array(): Statements
  30. * 'attrGroups' => array(): PHP attribute groups
  31. * @param array $attributes Additional attributes
  32. */
  33. public function __construct($name, array $subNodes = [], array $attributes = []) {
  34. $this->attributes = $attributes;
  35. $this->byRef = $subNodes['byRef'] ?? false;
  36. $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
  37. $this->params = $subNodes['params'] ?? [];
  38. $returnType = $subNodes['returnType'] ?? null;
  39. $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
  40. $this->stmts = $subNodes['stmts'] ?? [];
  41. $this->attrGroups = $subNodes['attrGroups'] ?? [];
  42. }
  43. public function getSubNodeNames() : array {
  44. return ['attrGroups', 'byRef', 'name', 'params', 'returnType', 'stmts'];
  45. }
  46. public function returnsByRef() : bool {
  47. return $this->byRef;
  48. }
  49. public function getParams() : array {
  50. return $this->params;
  51. }
  52. public function getReturnType() {
  53. return $this->returnType;
  54. }
  55. public function getAttrGroups() : array {
  56. return $this->attrGroups;
  57. }
  58. /** @return Node\Stmt[] */
  59. public function getStmts() : array {
  60. return $this->stmts;
  61. }
  62. public function getType() : string {
  63. return 'Stmt_Function';
  64. }
  65. }