Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
76 changes: 76 additions & 0 deletions src/Domain/MemberKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,82 @@ public function checksAccessByFirstDeclaration(): bool
return $this === self::Method || $this === self::Property;
}

/**
* Whether a class's `as` and `insteadof` rules adapt the members of this
* kind a trait brings in: they adapt only methods.
*/
public function takesTraitAdaptations(): bool
{
return $this === self::Method;
}

/**
* Whether PHP binds $member, brought in by a trait, before the using class
* inherits from its parent. A trait's abstract methods are bound after, so
* they only fill a name nothing else provides.
*/
public function bindsBeforeInheritance(MemberInfoInterface $member): bool
{
if ($this !== self::Method) {
return true;
}
assert($member instanceof MethodInfo, 'a method is described by a MethodInfo');
return !$member->isAbstract;
}

/**
* Whether a subclass keeps a parent's private member of this kind: PHP
* keeps private methods and properties, but does not inherit private
* constants, which enum cases are.
*/
public function inheritsPrivate(): bool
{
return $this->checksAccessByFirstDeclaration();
}

/**
* Whether a member overriding $inherited takes $root, the inherited
* member's prototype or else itself, as its own prototype, which decides
* the class protected access is checked against. A private method that is
* neither abstract nor a constructor gives none, nor does a constructor
* whose root is not abstract; constants and enum cases have none.
*/
public function inheritsPrototype(MemberInfoInterface $inherited, MemberInfoInterface $root): bool
{
if ($this === self::Property) {
return $inherited->getVisibility() !== Visibility::Private;
}
if ($this !== self::Method) {
return false;
}
assert($inherited instanceof MethodInfo && $root instanceof MethodInfo, 'methods are described by MethodInfo');
if ($this->normalize($inherited->name->name) === $this->normalize('__construct')) {
return $root->isAbstract;
}
return $inherited->visibility !== Visibility::Private || $inherited->isAbstract;
}

/**
* Whether $member, brought in by a trait, replaces a member of its name the
* class already has. A static property does, as the trait declares it
* again; anything else yields.
*/
public function traitReplacesExisting(MemberInfoInterface $member): bool
{
return $this === self::Property && $member->isStatic();
}

/**
* For a kind {@see self::checksAccessByFirstDeclaration()} applies to:
* whether code reaching $found by name, on an instance of a subclass of its
* own class, uses that class's private $own instead. A private static
* property is not used beside an instance one.
*/
public function prefersOwnPrivate(MemberInfoInterface $own, MemberInfoInterface $found): bool
{
return $this === self::Method || !$own->isStatic() || $found->isStatic();
}

/**
* Where a class-like holds the members of this kind, keyed by declared name.
*
Expand Down
46 changes: 46 additions & 0 deletions src/Repository/MemberEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Firehed\PhpLsp\Repository;

use Firehed\PhpLsp\Domain\ClassInfo;
use Firehed\PhpLsp\Domain\ClasslikeName;
use Firehed\PhpLsp\Domain\MemberInfoInterface;

/**
* One name in a class-like's member table: the member, the class-like whose
* table holds it as its own (the class using a trait, for a member the trait
* brings in), and the entry it overrides as its prototype.
*/
final readonly class MemberEntry
{
public function __construct(
public MemberInfoInterface $member,
public ClassInfo $declaring,
private ?MemberEntry $prototype = null,
) {
}

/**
* The entry at the root of this one's overrides: its prototype, else
* itself. A member overriding this one takes it as its own prototype.
*/
public function rootEntry(): self
{
return $this->prototype ?? $this;
}

/**
* The class protected access to the member is checked against.
*/
public function root(): ClasslikeName
{
return $this->rootEntry()->declaring->name;
}

public function withPrototype(self $prototype): self
{
return new self($this->member, $this->declaring, $prototype);
}
}
Loading
Loading