Param.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\Modifiers;
  4. use PhpParser\Node;
  5. use PhpParser\NodeAbstract;
  6. class Param extends NodeAbstract {
  7. /** @var null|Identifier|Name|ComplexType Type declaration */
  8. public ?Node $type;
  9. /** @var bool Whether parameter is passed by reference */
  10. public bool $byRef;
  11. /** @var bool Whether this is a variadic argument */
  12. public bool $variadic;
  13. /** @var Expr\Variable|Expr\Error Parameter variable */
  14. public Expr $var;
  15. /** @var null|Expr Default value */
  16. public ?Expr $default;
  17. /** @var int Optional visibility flags */
  18. public int $flags;
  19. /** @var AttributeGroup[] PHP attribute groups */
  20. public array $attrGroups;
  21. /** @var PropertyHook[] Property hooks for promoted properties */
  22. public array $hooks;
  23. /**
  24. * Constructs a parameter node.
  25. *
  26. * @param Expr\Variable|Expr\Error $var Parameter variable
  27. * @param null|Expr $default Default value
  28. * @param null|Identifier|Name|ComplexType $type Type declaration
  29. * @param bool $byRef Whether is passed by reference
  30. * @param bool $variadic Whether this is a variadic argument
  31. * @param array<string, mixed> $attributes Additional attributes
  32. * @param int $flags Optional visibility flags
  33. * @param list<AttributeGroup> $attrGroups PHP attribute groups
  34. * @param PropertyHook[] $hooks Property hooks for promoted properties
  35. */
  36. public function __construct(
  37. Expr $var, ?Expr $default = null, ?Node $type = null,
  38. bool $byRef = false, bool $variadic = false,
  39. array $attributes = [],
  40. int $flags = 0,
  41. array $attrGroups = [],
  42. array $hooks = []
  43. ) {
  44. $this->attributes = $attributes;
  45. $this->type = $type;
  46. $this->byRef = $byRef;
  47. $this->variadic = $variadic;
  48. $this->var = $var;
  49. $this->default = $default;
  50. $this->flags = $flags;
  51. $this->attrGroups = $attrGroups;
  52. $this->hooks = $hooks;
  53. }
  54. public function getSubNodeNames(): array {
  55. return ['attrGroups', 'flags', 'type', 'byRef', 'variadic', 'var', 'default', 'hooks'];
  56. }
  57. public function getType(): string {
  58. return 'Param';
  59. }
  60. /**
  61. * Whether this parameter uses constructor property promotion.
  62. */
  63. public function isPromoted(): bool {
  64. return $this->flags !== 0 || $this->hooks !== [];
  65. }
  66. public function isPublic(): bool {
  67. $public = (bool) ($this->flags & Modifiers::PUBLIC);
  68. if ($public) {
  69. return true;
  70. }
  71. if ($this->hooks === []) {
  72. return false;
  73. }
  74. return ($this->flags & Modifiers::VISIBILITY_MASK) === 0;
  75. }
  76. public function isProtected(): bool {
  77. return (bool) ($this->flags & Modifiers::PROTECTED);
  78. }
  79. public function isPrivate(): bool {
  80. return (bool) ($this->flags & Modifiers::PRIVATE);
  81. }
  82. public function isReadonly(): bool {
  83. return (bool) ($this->flags & Modifiers::READONLY);
  84. }
  85. /**
  86. * Whether the promoted property has explicit public(set) visibility.
  87. */
  88. public function isPublicSet(): bool {
  89. return (bool) ($this->flags & Modifiers::PUBLIC_SET);
  90. }
  91. /**
  92. * Whether the promoted property has explicit protected(set) visibility.
  93. */
  94. public function isProtectedSet(): bool {
  95. return (bool) ($this->flags & Modifiers::PROTECTED_SET);
  96. }
  97. /**
  98. * Whether the promoted property has explicit private(set) visibility.
  99. */
  100. public function isPrivateSet(): bool {
  101. return (bool) ($this->flags & Modifiers::PRIVATE_SET);
  102. }
  103. }