Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit 010fe9d

Browse files
committed
extract the logic into a trait
I want to extract the logic in order to reuse it in DoctrineAnnotationCompletor Inheritance is not suited in this case, the only remaining options I can think of are: * Using a static method in an helper class * Injecting a new dependency I don't think injecting a dependency is a good solution here since I can't think of a different implementation I might want to inject. The helper class with a static method is a possibility but I think trait are also meant for scenarios like this one. So I kept the trait to open a new debate :)
1 parent 48fbc68 commit 010fe9d

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

lib/Bridge/TolerantParser/ChainTolerantCompletor.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Generator;
66
use Microsoft\PhpParser\Node;
77
use Microsoft\PhpParser\Parser;
8-
use Microsoft\PhpParser\ResolvedName;
98
use Phpactor\Completion\Core\Completor;
10-
use Phpactor\Completion\Core\Suggestion;
119
use Phpactor\Completion\Core\Util\OffsetHelper;
1210
use Phpactor\TextDocument\ByteOffset;
1311
use Phpactor\TextDocument\TextDocument;
1412

1513
class ChainTolerantCompletor implements Completor
1614
{
15+
use ResolveAliasSuggestionsTrait;
16+
1717
/**
1818
* @var Parser
1919
*/
@@ -55,11 +55,13 @@ public function complete(TextDocument $source, ByteOffset $byteOffset): Generato
5555
continue;
5656
}
5757

58+
$importTable = $this->getClassImportTablesForNode($completionNode);
5859
$suggestions = $tolerantCompletor->complete($completionNode, $source, $byteOffset);
60+
5961
foreach ($suggestions as $suggestion) {
6062
// Trick to avoid any BC break when converting to an array
6163
// https://www.php.net/manual/fr/language.generators.syntax.php#control-structures.yield.from
62-
foreach ($this->resolveAliasSuggestions($completionNode, $suggestion) as $resolvedSuggestion) {
64+
foreach ($this->resolveAliasSuggestions($importTable, $suggestion) as $resolvedSuggestion) {
6365
yield $resolvedSuggestion;
6466
}
6567
}
@@ -97,39 +99,4 @@ private function filterNonQualifyingClasses(Node $node): array
9799
return $completor->qualifier()->couldComplete($node);
98100
});
99101
}
100-
101-
/**
102-
* Add suggestions when a class is already imported with an alias or when a relative name is abailable.
103-
*
104-
* Will update the suggestion to remove the import_name option if already imported.
105-
* Will add a suggestion if the class is imported under an alias.
106-
* Will add a suggestion if part of the namespace is imported (i.e. ORM\Column is a relative name).
107-
*
108-
* @return Suggestion[]
109-
*/
110-
private function resolveAliasSuggestions(Node $completionNode, Suggestion $suggestion): array
111-
{
112-
if (Suggestion::TYPE_CLASS !== $suggestion->type()) {
113-
return [$suggestion];
114-
}
115-
116-
/** @var ResolvedName[] $importTable */
117-
[$importTable] = $completionNode->getImportTablesForCurrentScope();
118-
119-
$suggestionFqcn = $suggestion->classImport();
120-
$suggestions = [$suggestion->name() => $suggestion];
121-
foreach ($importTable as $alias => $resolvedName) {
122-
$importFqcn = $resolvedName->getFullyQualifiedNameText();
123-
124-
if (0 !== strpos($suggestionFqcn, $importFqcn)) {
125-
continue;
126-
}
127-
128-
$name = $alias.substr($suggestionFqcn, strlen($importFqcn));
129-
130-
$suggestions[$alias] = $suggestion->withoutNameImport()->withName($name);
131-
}
132-
133-
return array_values($suggestions);
134-
}
135102
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Phpactor\Completion\Bridge\TolerantParser;
4+
5+
use Microsoft\PhpParser\Node;
6+
use Microsoft\PhpParser\ResolvedName;
7+
use Phpactor\Completion\Core\Suggestion;
8+
9+
trait ResolveAliasSuggestionsTrait
10+
{
11+
/**
12+
* Add suggestions when a class is already imported with an alias or when a relative name is abailable.
13+
*
14+
* Will update the suggestion to remove the import_name option if already imported.
15+
* Will add a suggestion if the class is imported under an alias.
16+
* Will add a suggestion if part of the namespace is imported (i.e. ORM\Column is a relative name).
17+
*
18+
* @param ResolvedName[] $importTable
19+
*
20+
* @return Suggestion[]
21+
*/
22+
private function resolveAliasSuggestions(array $importTable, Suggestion $suggestion): array
23+
{
24+
if (Suggestion::TYPE_CLASS !== $suggestion->type()) {
25+
return [$suggestion];
26+
}
27+
28+
$suggestionFqcn = $suggestion->classImport();
29+
$suggestions = [$suggestion->name() => $suggestion];
30+
foreach ($importTable as $alias => $resolvedName) {
31+
$importFqcn = $resolvedName->getFullyQualifiedNameText();
32+
33+
if (0 !== strpos($suggestionFqcn, $importFqcn)) {
34+
continue;
35+
}
36+
37+
$name = $alias.substr($suggestionFqcn, strlen($importFqcn));
38+
39+
$suggestions[$alias] = $suggestion->withoutNameImport()->withName($name);
40+
}
41+
42+
return array_values($suggestions);
43+
}
44+
45+
/**
46+
* @return ResolvedName[]
47+
*/
48+
private function getClassImportTablesForNode(Node $node): array
49+
{
50+
try {
51+
[$importTable] = $node->getImportTablesForCurrentScope();
52+
} catch (\Exception $e) {
53+
$importTable = [];
54+
}
55+
56+
return $importTable;
57+
}
58+
}

0 commit comments

Comments
 (0)