Skip to content

Commit b1a1f8c

Browse files
Merge pull request #8 from laravel/scope-resolution-qualifier-get-resolved-name-missing
`ScopedPropertyAccessExpressionParser` improvements
2 parents 953716d + aa5e625 commit b1a1f8c

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

Diff for: app/Contexts/AbstractContext.php

+12
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public function searchForVar(string $name): AssignmentValue|string|null
117117
return $this->parent?->searchForVar($name) ?? null;
118118
}
119119

120+
public function addPropertyToNearestClassDefinition(string $name, $types = [])
121+
{
122+
if ($this instanceof ClassDefinition) {
123+
$this->properties[] = [
124+
'name' => $name,
125+
'types' => $types,
126+
];
127+
} else {
128+
$this->parent?->addPropertyToNearestClassDefinition($name, $types);
129+
}
130+
}
131+
120132
public function searchForProperty(string $name)
121133
{
122134
if ($this instanceof ClassDefinition) {

Diff for: app/Contexts/AssignmentValue.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public function getValue()
1818
{
1919
$child = $this->children[0] ?? null;
2020

21-
if ($child) {
22-
return [
23-
'name' => $child->name ?? $child->className ?? null,
24-
'type' => $child->type(),
25-
];
21+
if (!$child) {
22+
return null;
2623
}
2724

28-
return null;
25+
return [
26+
'name' => $child->name ?? $child->className ?? null,
27+
'type' => $child->type(),
28+
];
2929
}
3030
}

Diff for: app/Parsers/ParameterParser.php

+10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ public function parse(NodeParameter $node)
1919
{
2020
$this->context->name = $node->getName();
2121

22+
$constructorProperty = $node->visibilityToken !== null;
23+
2224
if (!$node->typeDeclarationList) {
25+
if ($constructorProperty) {
26+
$this->context->addPropertyToNearestClassDefinition($this->context->name);
27+
}
28+
2329
return $this->context->value;
2430
}
2531

@@ -31,6 +37,10 @@ public function parse(NodeParameter $node)
3137
}
3238
}
3339

40+
if ($constructorProperty) {
41+
$this->context->addPropertyToNearestClassDefinition($this->context->name, $this->context->types);
42+
}
43+
3444
return $this->context->value;
3545
}
3646

Diff for: app/Parsers/ScopedPropertyAccessExpressionParser.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use App\Contexts\AbstractContext;
66
use App\Contexts\Argument;
7+
use App\Contexts\AssignmentValue;
78
use App\Contexts\MethodCall;
9+
use Microsoft\PhpParser\Node\Expression\MemberAccessExpression;
810
use Microsoft\PhpParser\Node\Expression\ScopedPropertyAccessExpression;
11+
use Microsoft\PhpParser\Node\Expression\Variable;
912

1013
class ScopedPropertyAccessExpressionParser extends AbstractParser
1114
{
@@ -17,14 +20,46 @@ class ScopedPropertyAccessExpressionParser extends AbstractParser
1720
public function parse(ScopedPropertyAccessExpression $node)
1821
{
1922
$this->context->methodName = $node->memberName->getFullText($node->getRoot()->getFullText());
20-
$this->context->className = (string) ($node->scopeResolutionQualifier?->getResolvedName() ?? $node->scopeResolutionQualifier?->getText());
23+
$this->context->className = $this->resolveClassName($node);
2124

2225
return $this->context;
2326
}
2427

28+
protected function resolveClassName(ScopedPropertyAccessExpression $node)
29+
{
30+
if (method_exists($node->scopeResolutionQualifier, 'getResolvedName')) {
31+
return (string) $node->scopeResolutionQualifier->getResolvedName();
32+
}
33+
34+
if ($node->scopeResolutionQualifier instanceof Variable) {
35+
$result = $this->context->searchForVar($node->scopeResolutionQualifier->getName());
36+
37+
if ($result instanceof AssignmentValue) {
38+
return $result->getValue()['name'] ?? null;
39+
}
40+
41+
return $result;
42+
}
43+
44+
if ($node->scopeResolutionQualifier instanceof MemberAccessExpression) {
45+
$parser = new MemberAccessExpressionParser;
46+
$context = new MethodCall;
47+
$context->parent = clone $this->context;
48+
$parser->context($context);
49+
$result = $parser->parseNode($node->scopeResolutionQualifier);
50+
51+
return $result->className ?? null;
52+
}
53+
54+
return $node->scopeResolutionQualifier->getText();
55+
}
56+
2557
public function initNewContext(): ?AbstractContext
2658
{
27-
if ($this->context instanceof Argument) {
59+
if (
60+
$this->context instanceof Argument
61+
|| $this->context instanceof AssignmentValue
62+
) {
2863
return new MethodCall;
2964
}
3065

0 commit comments

Comments
 (0)