PropertyHook.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\Modifiers;
  4. use PhpParser\Node\Expr\Assign;
  5. use PhpParser\Node\Expr\PropertyFetch;
  6. use PhpParser\Node\Expr\Variable;
  7. use PhpParser\Node\Stmt\Expression;
  8. use PhpParser\Node\Stmt\Return_;
  9. use PhpParser\NodeAbstract;
  10. class PropertyHook extends NodeAbstract implements FunctionLike {
  11. /** @var AttributeGroup[] PHP attribute groups */
  12. public array $attrGroups;
  13. /** @var int Modifiers */
  14. public int $flags;
  15. /** @var bool Whether hook returns by reference */
  16. public bool $byRef;
  17. /** @var Identifier Hook name */
  18. public Identifier $name;
  19. /** @var Param[] Parameters */
  20. public array $params;
  21. /** @var null|Expr|Stmt[] Hook body */
  22. public $body;
  23. /**
  24. * Constructs a property hook node.
  25. *
  26. * @param string|Identifier $name Hook name
  27. * @param null|Expr|Stmt[] $body Hook body
  28. * @param array{
  29. * flags?: int,
  30. * byRef?: bool,
  31. * params?: Param[],
  32. * attrGroups?: AttributeGroup[],
  33. * } $subNodes Array of the following optional subnodes:
  34. * 'flags => 0 : Flags
  35. * 'byRef' => false : Whether hook returns by reference
  36. * 'params' => array(): Parameters
  37. * 'attrGroups' => array(): PHP attribute groups
  38. * @param array<string, mixed> $attributes Additional attributes
  39. */
  40. public function __construct($name, $body, array $subNodes = [], array $attributes = []) {
  41. $this->attributes = $attributes;
  42. $this->name = \is_string($name) ? new Identifier($name) : $name;
  43. $this->body = $body;
  44. $this->flags = $subNodes['flags'] ?? 0;
  45. $this->byRef = $subNodes['byRef'] ?? false;
  46. $this->params = $subNodes['params'] ?? [];
  47. $this->attrGroups = $subNodes['attrGroups'] ?? [];
  48. }
  49. public function returnsByRef(): bool {
  50. return $this->byRef;
  51. }
  52. public function getParams(): array {
  53. return $this->params;
  54. }
  55. public function getReturnType() {
  56. return null;
  57. }
  58. /**
  59. * Whether the property hook is final.
  60. */
  61. public function isFinal(): bool {
  62. return (bool) ($this->flags & Modifiers::FINAL);
  63. }
  64. public function getStmts(): ?array {
  65. if ($this->body instanceof Expr) {
  66. $name = $this->name->toLowerString();
  67. if ($name === 'get') {
  68. return [new Return_($this->body)];
  69. }
  70. if ($name === 'set') {
  71. if (!$this->hasAttribute('propertyName')) {
  72. throw new \LogicException(
  73. 'Can only use getStmts() on a "set" hook if the "propertyName" attribute is set');
  74. }
  75. $propName = $this->getAttribute('propertyName');
  76. $prop = new PropertyFetch(new Variable('this'), (string) $propName);
  77. return [new Expression(new Assign($prop, $this->body))];
  78. }
  79. throw new \LogicException('Unknown property hook "' . $name . '"');
  80. }
  81. return $this->body;
  82. }
  83. public function getAttrGroups(): array {
  84. return $this->attrGroups;
  85. }
  86. public function getType(): string {
  87. return 'PropertyHook';
  88. }
  89. public function getSubNodeNames(): array {
  90. return ['attrGroups', 'flags', 'byRef', 'name', 'params', 'body'];
  91. }
  92. }