Skip to content

Commit 1e64d2a

Browse files
Copilotlisachenko
andauthored
feat: add inline ClassFieldAccess phpdoc in property hook generation
Agent-Logs-Url: https://github.com/goaop/framework/sessions/0dfed7bf-8709-43b7-8d60-aefe9f468557 Co-authored-by: lisachenko <640114+lisachenko@users.noreply.github.com>
1 parent f1e60ff commit 1e64d2a

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/Proxy/Part/InterceptedPropertyGenerator.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Go\Proxy\Generator\PropertyNodeProvider;
1919
use Go\Proxy\Generator\TypeGenerator;
2020
use InvalidArgumentException;
21+
use PhpParser\Comment\Doc;
2122
use PhpParser\Node\Arg;
2223
use PhpParser\Node\Expr\ArrayDimFetch;
2324
use PhpParser\Node\Expr\Assign;
@@ -146,15 +147,17 @@ public function getNode(): PropertyNode
146147
private function createGetHook(bool $returnsByReference): PropertyHook
147148
{
148149
$propertyName = $this->property->getName();
150+
$fieldAccessExpression = new Expression(new Assign(
151+
new Variable('fieldAccess'),
152+
new ArrayDimFetch(
153+
new StaticPropertyFetch(new Name('self'), JoinPointPropertyGenerator::NAME),
154+
new String_('prop:' . $propertyName)
155+
)
156+
));
157+
$fieldAccessExpression->setDocComment(new Doc('/** @var ClassFieldAccess<self> $fieldAccess */'));
149158

150159
return new PropertyHook('get', [
151-
new Expression(new Assign(
152-
new Variable('fieldAccess'),
153-
new ArrayDimFetch(
154-
new StaticPropertyFetch(new Name('self'), JoinPointPropertyGenerator::NAME),
155-
new String_('prop:' . $propertyName)
156-
)
157-
)),
160+
$fieldAccessExpression,
158161
new Expression(new AssignRef(
159162
new Variable('value'),
160163
new MethodCall(new Variable('fieldAccess'), '__invoke', [
@@ -181,15 +184,17 @@ private function createGetHook(bool $returnsByReference): PropertyHook
181184
private function createSetHook(): PropertyHook
182185
{
183186
$propertyName = $this->property->getName();
187+
$fieldAccessExpression = new Expression(new Assign(
188+
new Variable('fieldAccess'),
189+
new ArrayDimFetch(
190+
new StaticPropertyFetch(new Name('self'), JoinPointPropertyGenerator::NAME),
191+
new String_('prop:' . $propertyName)
192+
)
193+
));
194+
$fieldAccessExpression->setDocComment(new Doc('/** @var ClassFieldAccess<self> $fieldAccess */'));
184195

185196
return new PropertyHook('set', [
186-
new Expression(new Assign(
187-
new Variable('fieldAccess'),
188-
new ArrayDimFetch(
189-
new StaticPropertyFetch(new Name('self'), JoinPointPropertyGenerator::NAME),
190-
new String_('prop:' . $propertyName)
191-
)
192-
)),
197+
$fieldAccessExpression,
193198
new Expression(new Assign(
194199
new PropertyFetch(new Variable('this'), $propertyName),
195200
new MethodCall(new Variable('fieldAccess'), '__invoke', [

tests/Proxy/ClassProxyGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ public function testGenerateWithPropertyInterception(): void
9797
);
9898
$this->assertStringContainsString("self::\$__joinPoints['prop:public'];", $proxyFileContent);
9999
$this->assertStringContainsString(
100-
"set {\n \$fieldAccess = self::\$__joinPoints['prop:public'];",
100+
"/** @var ClassFieldAccess<self> \$fieldAccess */",
101101
$proxyFileContent,
102102
'Proxy with property advices must route writes through join points in property hooks'
103103
);
104+
$this->assertStringContainsString("set {\n /** @var ClassFieldAccess<self> \$fieldAccess */", $proxyFileContent);
104105
$this->assertStringNotContainsString('PropertyInterceptionTrait', $proxyFileContent);
105106
$this->assertStringNotContainsString('__properties', $proxyFileContent);
106107
}

0 commit comments

Comments
 (0)