Skip to content

Commit 5bdad73

Browse files
committed
PromotedParameter can have hooks
1 parent fb66cbb commit 5bdad73

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/PhpGenerator/Printer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ private function formatParameters(Closure|GlobalFunction|Method|PropertyHook $fu
351351
. ($variadic ? '...' : '')
352352
. '$' . $param->getName()
353353
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '')
354+
. ($param instanceof PromotedParameter ? $this->printHooks($param) : '')
354355
. ($multiline ? ",\n" : ', ');
355356
}
356357

src/PhpGenerator/PromotedParameter.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final class PromotedParameter extends Parameter
2121

2222
private bool $readOnly = false;
2323

24+
/** @var array<string, ?PropertyHook> */
25+
private array $hooks = ['set' => null, 'get' => null];
26+
2427

2528
public function setReadOnly(bool $state = true): static
2629
{
@@ -35,6 +38,46 @@ public function isReadOnly(): bool
3538
}
3639

3740

41+
/**
42+
* Replaces all hooks
43+
* @param PropertyHook[] $hooks
44+
*/
45+
public function setHooks(array $hooks): static
46+
{
47+
(function (PropertyHook ...$hooks) {})(...$hooks);
48+
$this->hooks = $hooks;
49+
return $this;
50+
}
51+
52+
53+
/** @return array<string, PropertyHook> */
54+
public function getHooks(): array
55+
{
56+
return array_filter($this->hooks);
57+
}
58+
59+
60+
public function getHook(\PropertyHookType|string $type): ?PropertyHook
61+
{
62+
$type = is_string($type) ? $type : $type->value;
63+
return $this->hooks[$type] ?? null;
64+
}
65+
66+
67+
public function addHook(\PropertyHookType|string $type, string $body = '', bool $short = false): PropertyHook
68+
{
69+
$type = is_string($type) ? $type : $type->value;
70+
return $this->hooks[$type] = (new PropertyHook)
71+
->setBody($body, short: $short);
72+
}
73+
74+
75+
public function hasHook(\PropertyHookType|string $type): bool
76+
{
77+
return isset($this->hooks[$type]);
78+
}
79+
80+
3881
/** @throws Nette\InvalidStateException */
3982
public function validate(): void
4083
{

tests/PhpGenerator/Property.hooks.classes.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,42 @@ same(<<<'XX'
5757
}
5858

5959
XX, (string) $class);
60+
61+
62+
63+
// promoted properties
64+
65+
$class = new ClassType('Demo');
66+
67+
$method = $class->addMethod('__construct');
68+
69+
$method->addPromotedParameter('first')
70+
->setType('string')
71+
->addHook('get')
72+
->setBody('return $this->first . "x";')
73+
->setReturnReference();
74+
75+
$method->addPromotedParameter('second')
76+
->setType('string')
77+
->addHook('set', '$value', short: true)
78+
->setFinal()
79+
->addParameter('value')
80+
->setType('string');
81+
82+
same(<<<'XX'
83+
class Demo
84+
{
85+
public function __construct(
86+
public string $first {
87+
&get {
88+
return $this->first . "x";
89+
}
90+
},
91+
public string $second {
92+
final set(string $value) => $value;
93+
},
94+
) {
95+
}
96+
}
97+
98+
XX, (string) $class);

0 commit comments

Comments
 (0)