Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
09f4d44
Test a constructor lookup forwards no static
Firehed Oct 29, 2026
acf612a
Look up a constructor without a member access
Firehed Oct 29, 2026
7ca7c33
Test a member target carries its access kind
Firehed Oct 29, 2026
ae8bcfe
Derive a member target's static from its kind
Firehed Oct 29, 2026
2c0fd2b
Test a member target is asked about its access
Firehed Oct 29, 2026
34c1d00
Decide a member access's kind from the access
Firehed Oct 29, 2026
fc2a4ea
Test completion rules read from the access kind
Firehed Oct 29, 2026
79c2570
Read completion rules from the access kind
Firehed Oct 29, 2026
4204c75
Test a static member name is a member slot
Firehed Oct 29, 2026
ca481a1
Merge the static member slot into the member slot
Firehed Oct 29, 2026
40f602a
Test a keyword constructor outside a class
Firehed Oct 29, 2026
167492d
Share one alias for a member access node
Firehed Oct 29, 2026
a9c6cf1
State which accesses forward late static binding
Firehed Oct 29, 2026
0fc858c
Locate the parent constructor by a cursor marker
Firehed Oct 29, 2026
ebbf56d
Test reading a member access from its node
Firehed Oct 29, 2026
5ebb7b0
Add a member access read from its node
Firehed Oct 29, 2026
0682a40
Test a located member access is read from its node
Firehed Oct 29, 2026
ea6408b
Read every member access through one type
Firehed Oct 29, 2026
5e36468
Test a member named by an expression is unresolved
Firehed Oct 29, 2026
719400c
Test a dynamic member name inside a class
Firehed Oct 29, 2026
0b6516a
Test a member access records whether it uses ::
Firehed Oct 29, 2026
e13de54
Type a member access's node and read its :: directly
Firehed Oct 29, 2026
de7ea19
Resolve a member name through its located access
Firehed Oct 29, 2026
2a4d5bd
Test a name not written resolves to nothing
Firehed Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Completion/MemberCandidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function __construct(
public function find(MemberAccessContext $context): array
{
$members = $this->symbols->getAccessibleMembers(
$context->type,
$context->from,
$context->memberFilter,
$context->static,
$context->target->type,
$context->target->from,
$context->memberFilter(),
$context->target->static(),
);

$snippetSupport = $this->capabilities->getSessionCapabilities()->snippetSupport;
Expand All @@ -44,7 +44,7 @@ public function find(MemberAccessContext $context): array
}
}

if ($context->offersClassConstant && PrefixMatcher::matches('class', $context->prefix)) {
if ($context->offersClassConstant() && PrefixMatcher::matches('class', $context->prefix)) {
$items[] = $this->itemFormatter->forClassConstant();
}

Expand Down
168 changes: 44 additions & 124 deletions src/Resolution/ExpressionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
use Firehed\PhpLsp\Domain\FunctionInfo;
use Firehed\PhpLsp\Domain\FunctionName;
use Firehed\PhpLsp\Domain\Location;
use Firehed\PhpLsp\Domain\MemberInfoInterface;
use Firehed\PhpLsp\Domain\MethodInfo;
use Firehed\PhpLsp\Domain\NameKind;
use Firehed\PhpLsp\Domain\ParameterInfo;
use Firehed\PhpLsp\Domain\PropertyInfo;
use Firehed\PhpLsp\Domain\ResolvedCallableInterface;
use Firehed\PhpLsp\Domain\ResolvedSymbolInterface;
use Firehed\PhpLsp\Domain\TypeInterface;
Expand Down Expand Up @@ -45,7 +43,6 @@
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\VarLikeIdentifier;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;

/**
Expand Down Expand Up @@ -77,30 +74,15 @@ public function resolve(Expr $expr, ParsedDocument $parsed): ?ResolvedSymbolInte
return $this->resolveNew($expr, $parsed);
}

if ($expr instanceof MethodCall || $expr instanceof NullsafeMethodCall) {
return $this->resolveMethodCall($expr, $parsed);
}

if ($expr instanceof StaticCall) {
return $this->resolveStaticCall($expr, $parsed);
$access = MemberAccess::of($expr);
if ($access !== null) {
return $this->resolveMember($access, $parsed);
}

if ($expr instanceof FuncCall) {
return $this->resolveFuncCall($expr, $parsed);
}

if ($expr instanceof PropertyFetch || $expr instanceof NullsafePropertyFetch) {
return $this->resolvePropertyFetch($expr, $parsed);
}

if ($expr instanceof StaticPropertyFetch) {
return $this->resolveStaticPropertyFetch($expr, $parsed);
}

if ($expr instanceof ClassConstFetch) {
return $this->resolveClassConstFetch($expr, $parsed);
}

if ($expr instanceof Expr\ConstFetch) {
return $this->resolveConstFetch($expr, $parsed);
}
Expand Down Expand Up @@ -144,22 +126,25 @@ public function resolveBinding(VariableBinding $binding, ParsedDocument $parsed)
return new ResolvedVariable($binding->name, $type, $location);
}

public function memberTarget(Expr|Name $receiver, ParsedDocument $parsed): ?MemberTarget
public function memberTarget(MemberAccess $access, ParsedDocument $parsed): ?MemberTarget
{
$scope = Scope::at($receiver, $parsed->tree);
$kind = $access->kind();
if ($kind === null) {
return null;
}
$scope = Scope::at($access->node, $parsed->tree);
$receiver = $access->receiver;
if ($receiver instanceof Name) {
$className = $scope->resolveClasslikeName($receiver);
return $className === null ? null : new MemberTarget(
new ClasslikeType($className),
$scope->getSelfName(),
$scope->staticFor($receiver),
);
return $className === null
? null
: new MemberTarget(new ClasslikeType($className), $scope->getSelfName(), $kind);
}
$type = $this->resolve($receiver, $parsed)?->getType();
if ($type === null || $type->getResolvableClasslikeNames() === []) {
return null;
}
return new MemberTarget($type, $scope->getSelfName(), null);
return new MemberTarget($type, $scope->getSelfName(), $kind);
}

public function resolveParameter(Param $param, ParsedDocument $parsed): ParameterInfo
Expand Down Expand Up @@ -301,38 +286,43 @@ private function resolveConstructor(New_|Attribute $call, ParsedDocument $parsed
if (!$classNameNode instanceof Name) {
return null;
}
return $this->resolveMember(
$classNameNode,
'__construct',
$this->memberResolver->findMethod(...),
$parsed,
);
$scope = Scope::at($call, $parsed->tree);
$class = $scope->resolveClasslikeName($classNameNode);
return $class === null
? null
: $this->memberResolver->findMethod($class, '__construct', $scope->getSelfName(), null);
}

private function resolveMethodCall(MethodCall|NullsafeMethodCall $expr, ParsedDocument $parsed): ?MethodInfo
/**
* The member an access names, looked up on what it is accessed on. Only a
* member visible from where the access is written is found.
*/
private function resolveMember(MemberAccess $access, ParsedDocument $parsed): ?ResolvedSymbolInterface
{
if (!$expr->name instanceof Identifier) {
if (!$access->name instanceof Identifier) {
return null;
}
return $this->resolveMember(
$expr->var,
$expr->name->toString(),
$this->memberResolver->findMethod(...),
$parsed,
);
}

private function resolveStaticCall(StaticCall $expr, ParsedDocument $parsed): ?MethodInfo
{
if (!$expr->name instanceof Identifier || !$expr->class instanceof Name) {
$target = $this->memberTarget($access, $parsed);
if ($target === null) {
return null;
}
return $this->resolveMember(
$expr->class,
$expr->name->toString(),
$this->memberResolver->findMethod(...),
$parsed,
);
$name = $access->name->toString();
$node = $access->node;
foreach ($target->type->getResolvableClasslikeNames() as $class) {
$member = match (true) {
$node instanceof MethodCall, $node instanceof NullsafeMethodCall, $node instanceof StaticCall
=> $this->memberResolver->findMethod($class, $name, $target->from, $target->static()),
$node instanceof ClassConstFetch => $this->memberResolver->findEnumCase($class, $name)
?? $this->memberResolver->findConstant($class, $name, $target->from, $target->static()),
$node instanceof PropertyFetch, $node instanceof NullsafePropertyFetch,
$node instanceof StaticPropertyFetch
=> $this->memberResolver->findProperty($class, $name, $target->from, $target->static()),
};
if ($member !== null) {
return $member;
}
}
return null;
}

private function resolveFuncCall(FuncCall $expr, ParsedDocument $parsed): ?FunctionInfo
Expand All @@ -356,76 +346,6 @@ private function resolveFuncCall(FuncCall $expr, ParsedDocument $parsed): ?Funct
return null;
}

private function resolvePropertyFetch(
PropertyFetch|NullsafePropertyFetch $expr,
ParsedDocument $parsed,
): ?PropertyInfo {
if (!$expr->name instanceof Identifier) {
return null;
}
return $this->resolveMember(
$expr->var,
$expr->name->toString(),
$this->memberResolver->findProperty(...),
$parsed,
);
}

private function resolveStaticPropertyFetch(StaticPropertyFetch $expr, ParsedDocument $parsed): ?PropertyInfo
{
if (!$expr->name instanceof VarLikeIdentifier || !$expr->class instanceof Name) {
return null;
}
return $this->resolveMember(
$expr->class,
$expr->name->toString(),
$this->memberResolver->findProperty(...),
$parsed,
);
}

/**
* Access a member on the receiver, delegating the kind-specific lookup to
* `$find`. Only a member visible from where the access is written is found.
*
* @template T of MemberInfoInterface
* @param callable(ClasslikeName, string, ?ClasslikeName, ?ClasslikeName): ?T $find
* @return ?T
*/
private function resolveMember(
Expr|Name $receiver,
string $memberName,
callable $find,
ParsedDocument $parsed,
): ?MemberInfoInterface {
$target = $this->memberTarget($receiver, $parsed);
if ($target === null) {
return null;
}
foreach ($target->type->getResolvableClasslikeNames() as $className) {
$member = $find($className, $memberName, $target->from, $target->static);
if ($member !== null) {
return $member;
}
}
return null;
}

private function resolveClassConstFetch(ClassConstFetch $expr, ParsedDocument $parsed): ?ResolvedSymbolInterface
{
if (!$expr->name instanceof Identifier || !$expr->class instanceof Name) {
return null;
}
return $this->resolveMember(
$expr->class,
$expr->name->toString(),
fn (ClasslikeName $class, string $name, ?ClasslikeName $from, ?ClasslikeName $static)
=> $this->memberResolver->findEnumCase($class, $name)
?? $this->memberResolver->findConstant($class, $name, $from, $static),
$parsed,
);
}

private function resolveConstFetch(Expr\ConstFetch $expr, ParsedDocument $parsed): ?ConstantInfo
{
// See resolveFuncCall for why toCodeString(): the same NameResolver may
Expand Down
9 changes: 4 additions & 5 deletions src/Resolution/ExpressionResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Param;

/**
Expand All @@ -35,11 +34,11 @@ public function resolveCallable(
): ?ResolvedCallableInterface;

/**
* What a member accessed on $receiver is looked up on, or null when the
* receiver names no class-like. A class named as written resolves where it
* is written.
* What a member access is looked up on, or null when what it is accessed
* on names no class-like or is an expression before `::`. A class named
* as written resolves where it is written.
*/
public function memberTarget(Expr|Name $receiver, ParsedDocument $parsed): ?MemberTarget;
public function memberTarget(MemberAccess $access, ParsedDocument $parsed): ?MemberTarget;

/**
* A parameter as its declaration declares it.
Expand Down
60 changes: 60 additions & 0 deletions src/Resolution/MemberAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Firehed\PhpLsp\Resolution;

use Firehed\PhpLsp\Domain\LateBindingKeyword;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;

/**
* A member access as written: `->`, `?->`, or `::` with what it is accessed on
* ($receiver) and the member's name.
*/
final readonly class MemberAccess
{
private function __construct(
public Expr\MethodCall|Expr\NullsafeMethodCall|Expr\PropertyFetch|Expr\NullsafePropertyFetch
|Expr\StaticCall|Expr\StaticPropertyFetch|Expr\ClassConstFetch $node,
public Expr|Name $receiver,
public Identifier|Expr $name,
public bool $doubleColon,
) {
}

/**
* The member access a node is, or null when it is none.
*/
public static function of(?Node $node): ?self
{
return match (true) {
$node instanceof Expr\MethodCall, $node instanceof Expr\NullsafeMethodCall,
$node instanceof Expr\PropertyFetch, $node instanceof Expr\NullsafePropertyFetch
=> new self($node, $node->var, $node->name, false),
$node instanceof Expr\StaticCall, $node instanceof Expr\StaticPropertyFetch,
$node instanceof Expr\ClassConstFetch
=> new self($node, $node->class, $node->name, true),
default => null,
};
}

/**
* How the access is written; null for `::` after an expression rather
* than a class name.
*/
public function kind(): ?MemberAccessKind
{
if (!$this->doubleColon) {
return MemberAccessKind::Instance;
}
if (!$this->receiver instanceof Name) {
return null;
}
return LateBindingKeyword::tryFromName($this->receiver->toString()) === LateBindingKeyword::Parent
? MemberAccessKind::Parent
: MemberAccessKind::Static;
}
}
Loading
Loading