|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phpactor\Rename\Adapter\WorseReflection; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use Phpactor\Rename\Model\LocatedTextEdit; |
| 7 | +use Phpactor\Rename\Model\Renamer; |
| 8 | +use Phpactor\TextDocument\ByteOffset; |
| 9 | +use Phpactor\TextDocument\ByteOffsetRange; |
| 10 | +use Phpactor\TextDocument\TextDocument; |
| 11 | +use Phpactor\TextDocument\TextEdit as PhpactorTextEdit; |
| 12 | +use Phpactor\WorseReflection\Core\Inference\Symbol; |
| 13 | +use Phpactor\WorseReflection\Core\Reflection\ReflectionMember; |
| 14 | +use Phpactor\WorseReflection\Core\Type\ClassLikeType; |
| 15 | +use Phpactor\WorseReflection\Reflector; |
| 16 | + |
| 17 | +class WorseReflectionMemberRenamer implements Renamer |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + private Reflector $reflector, |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + public function getRenameRange(TextDocument $textDocument, ByteOffset $offset): ?ByteOffsetRange |
| 25 | + { |
| 26 | + $member = $this->resolveMember($textDocument, $offset); |
| 27 | + |
| 28 | + if (null === $member) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + return $member->nameRange(); |
| 33 | + } |
| 34 | + |
| 35 | + public function rename(TextDocument $textDocument, ByteOffset $offset, string $newName): Generator |
| 36 | + { |
| 37 | + $member = $this->resolveMember($textDocument, $offset); |
| 38 | + |
| 39 | + if (null === $member) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $uri = $member->class()->sourceCode()->uri(); |
| 44 | + |
| 45 | + if (null === $uri) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $rangeStart = $member->nameRange()->start(); |
| 50 | + yield new LocatedTextEdit( |
| 51 | + $uri, |
| 52 | + PhpactorTextEdit::create( |
| 53 | + $rangeStart, |
| 54 | + $member->nameRange()->length(), |
| 55 | + $newName, |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + $accesses = match ($member->memberType()) { |
| 60 | + ReflectionMember::TYPE_METHOD => $this->reflector->navigate($textDocument)->methodCalls(), |
| 61 | + ReflectionMember::TYPE_PROPERTY => $this->reflector->navigate($textDocument)->propertyAccesses(), |
| 62 | + default => [], |
| 63 | + }; |
| 64 | + |
| 65 | + foreach ($accesses as $access) { |
| 66 | + if ($access->name() !== $member->name()) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + yield new LocatedTextEdit( |
| 70 | + $uri, |
| 71 | + PhpactorTextEdit::create($access->nameRange()->start(), $access->nameRange()->length(), $newName) |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function resolveMember(TextDocument $textDocument, ByteOffset $offset): ?ReflectionMember |
| 77 | + { |
| 78 | + $context = $this->reflector->reflectOffset($textDocument, $offset)->nodeContext(); |
| 79 | + |
| 80 | + $symbolType = $context->symbol()->symbolType(); |
| 81 | + if (!in_array($symbolType, [ |
| 82 | + Symbol::METHOD, |
| 83 | + Symbol::PROPERTY, |
| 84 | + Symbol::VARIABLE, // promoted properties 🙃 |
| 85 | + ])) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $containerType = $context->containerType(); |
| 90 | + |
| 91 | + if (!$containerType instanceof ClassLikeType) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $class = $this->reflector->reflectClassLike($containerType->name()); |
| 96 | + |
| 97 | + $memberType = $symbolType === Symbol::VARIABLE ? 'property' : $symbolType; |
| 98 | + $members = $class->members()->byMemberType($memberType)->byName($context->symbol()->name()); |
| 99 | + |
| 100 | + foreach ($members as $member) { |
| 101 | + if (!$member->visibility()->isPrivate()) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + return $member; |
| 105 | + } |
| 106 | + |
| 107 | + return null; |
| 108 | + } |
| 109 | +} |
0 commit comments