From 3972812a009f199f75d9a94006f2a61f54a17e4c Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 10 Feb 2025 21:12:51 -0500 Subject: [PATCH 1/3] find nearest class def --- app/Contexts/AbstractContext.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/Contexts/AbstractContext.php b/app/Contexts/AbstractContext.php index d861f97..fe32c3c 100644 --- a/app/Contexts/AbstractContext.php +++ b/app/Contexts/AbstractContext.php @@ -129,6 +129,15 @@ public function addPropertyToNearestClassDefinition(string $name, $types = []) } } + public function nearestClassDefinition() + { + if ($this instanceof ClassDefinition) { + return $this; + } + + return $this->parent?->nearestClassDefinition() ?? null; + } + public function searchForProperty(string $name) { if ($this instanceof ClassDefinition) { From 009c4861458ff943d6dd46e67e5f029bfbca8164 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 10 Feb 2025 21:12:58 -0500 Subject: [PATCH 2/3] Update MemberAccessExpressionParser.php --- app/Parsers/MemberAccessExpressionParser.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/Parsers/MemberAccessExpressionParser.php b/app/Parsers/MemberAccessExpressionParser.php index 734b888..db6b730 100644 --- a/app/Parsers/MemberAccessExpressionParser.php +++ b/app/Parsers/MemberAccessExpressionParser.php @@ -5,6 +5,7 @@ use App\Contexts\AbstractContext; use App\Contexts\AssignmentValue; use App\Contexts\MethodCall; +use Microsoft\PhpParser\Node\Expression\CallExpression; use Microsoft\PhpParser\Node\Expression\MemberAccessExpression; use Microsoft\PhpParser\Node\Expression\Variable; use Microsoft\PhpParser\Node\QualifiedName; @@ -29,6 +30,17 @@ public function parse(MemberAccessExpression $node) if ($child instanceof Variable) { if ($child->getName() === 'this') { + if ($node->getParent() instanceof CallExpression) { + // They are calling a method on the current class + $result = $this->context->nearestClassDefinition(); + + if ($result) { + $this->context->className = $result->className; + } + + continue; + } + $propName = $child->getParent()->memberName->getFullText($node->getRoot()->getFullText()); $result = $this->context->searchForProperty($propName); From f78306a8de6cbc30ca5f7bac3c9648768b11337b Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 10 Feb 2025 21:23:39 -0500 Subject: [PATCH 3/3] check child parent not node parent --- app/Parsers/MemberAccessExpressionParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Parsers/MemberAccessExpressionParser.php b/app/Parsers/MemberAccessExpressionParser.php index db6b730..e087dc4 100644 --- a/app/Parsers/MemberAccessExpressionParser.php +++ b/app/Parsers/MemberAccessExpressionParser.php @@ -30,7 +30,7 @@ public function parse(MemberAccessExpression $node) if ($child instanceof Variable) { if ($child->getName() === 'this') { - if ($node->getParent() instanceof CallExpression) { + if ($child->getParent()->getParent() instanceof CallExpression) { // They are calling a method on the current class $result = $this->context->nearestClassDefinition();