1: <?php declare(strict_types=1);
2:
3: namespace Salient\PHPDoc\Tag;
4:
5: use Salient\Contract\Core\Immutable;
6: use Salient\Core\Concern\HasMutator;
7: use Stringable;
8:
9: /**
10: * @api
11: */
12: class MethodParam implements Immutable, Stringable
13: {
14: use HasMutator;
15:
16: protected string $Name;
17: protected ?string $Type;
18: protected ?string $Default;
19: protected bool $IsVariadic;
20:
21: /**
22: * Creates a new MethodParam object
23: */
24: public function __construct(
25: string $name,
26: ?string $type = null,
27: ?string $default = null,
28: bool $isVariadic = false
29: ) {
30: $this->Name = $name;
31: $this->Type = $type;
32: $this->Default = $default;
33: $this->IsVariadic = $isVariadic;
34: }
35:
36: /**
37: * Get the name of the parameter
38: */
39: public function getName(): string
40: {
41: return $this->Name;
42: }
43:
44: /**
45: * Get the PHPDoc type of the parameter
46: */
47: public function getType(): ?string
48: {
49: return $this->Type;
50: }
51:
52: /**
53: * Get the default value of the parameter
54: */
55: public function getDefault(): ?string
56: {
57: return $this->Default;
58: }
59:
60: /**
61: * Check if the parameter is variadic
62: */
63: public function isVariadic(): bool
64: {
65: return $this->IsVariadic;
66: }
67:
68: /**
69: * Get an instance with the given PHPDoc type
70: *
71: * @return static
72: */
73: public function withType(?string $type)
74: {
75: return $this->with('Type', $type);
76: }
77:
78: /**
79: * @inheritDoc
80: */
81: public function __toString(): string
82: {
83: $string = '';
84: if ($this->Type !== null) {
85: $string .= "{$this->Type} ";
86: }
87: if ($this->IsVariadic) {
88: $string .= '...';
89: }
90: $string .= "\${$this->Name}";
91: if ($this->Default !== null) {
92: $string .= " = {$this->Default}";
93: }
94: return $string;
95: }
96: }
97: