PropertyHook.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\Node\Stmt\Return_;
  4. use PhpParser\NodeAbstract;
  5. class PropertyHook extends NodeAbstract implements FunctionLike {
  6. /** @var AttributeGroup[] PHP attribute groups */
  7. public array $attrGroups;
  8. /** @var int Modifiers */
  9. public int $flags;
  10. /** @var bool Whether hook returns by reference */
  11. public bool $byRef;
  12. /** @var Identifier Hook name */
  13. public Identifier $name;
  14. /** @var Param[] Parameters */
  15. public array $params;
  16. /** @var null|Expr|Stmt[] Hook body */
  17. public $body;
  18. /**
  19. * Constructs a property hook node.
  20. *
  21. * @param string|Identifier $name Hook name
  22. * @param null|Expr|Stmt[] $body Hook body
  23. * @param array{
  24. * flags?: int,
  25. * byRef?: bool,
  26. * params?: Param[],
  27. * attrGroups?: AttributeGroup[],
  28. * } $subNodes Array of the following optional subnodes:
  29. * 'byRef' => false : Whether hook returns by reference
  30. * 'params' => array(): Parameters
  31. * 'attrGroups' => array(): PHP attribute groups
  32. * @param array<string, mixed> $attributes Additional attributes
  33. */
  34. public function __construct($name, $body, array $subNodes = [], array $attributes = []) {
  35. $this->attributes = $attributes;
  36. $this->name = \is_string($name) ? new Identifier($name) : $name;
  37. $this->body = $body;
  38. $this->flags = $subNodes['flags'] ?? 0;
  39. $this->byRef = $subNodes['byRef'] ?? false;
  40. $this->params = $subNodes['params'] ?? [];
  41. $this->attrGroups = $subNodes['attrGroups'] ?? [];
  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 null;
  51. }
  52. public function getStmts(): ?array {
  53. if ($this->body instanceof Expr) {
  54. return [new Return_($this->body)];
  55. }
  56. return $this->body;
  57. }
  58. public function getAttrGroups(): array {
  59. return $this->attrGroups;
  60. }
  61. public function getType(): string {
  62. return 'PropertyHook';
  63. }
  64. public function getSubNodeNames(): array {
  65. return ['attrGroups', 'flags', 'byRef', 'name', 'params', 'body'];
  66. }
  67. }