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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Features:
- [vim-plugin] Maintain correct cursor position after certain text diffs (#770) - @elythyr
- [code-transform|rpc] Generate multiple accessors for a class - @elythyr
- [code-tranform] Generate static methods if the call was static (#25) - @einenlum
- [completion] Use declared classes as completion source
- [import-class] Import declared classes (as long as they can be statically
resolved).

Bug fixes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public function classSearch(string $filesystemName, string $name)
$files = $filesystem->fileList('{' . $name . '}')->named($name . '.php');

$results = [];

if ($reflectionResult = $this->tryAndReflect($name)) {
$results[] = $reflectionResult;
}
$results = $this->builtInResults($results, $name);

foreach ($files as $file) {
if (isset($results[(string) $file->path()])) {
Expand Down Expand Up @@ -87,4 +84,42 @@ private function tryAndReflect(string $name)
'class_namespace' => (string) $reflectionClass->name()->namespace(),
];
}

private function builtInResults(array $results, string $name)
{
$declared = array_merge(
get_declared_classes(),
get_declared_traits(),
get_declared_interfaces()
);

foreach ($declared as $declaredClass) {
$short = $this->resolveShortName($declaredClass);

$namespace = substr($declaredClass, 0, strrpos($declaredClass, '\\'));

if ($name !== $short) {
continue;
}

if (!$this->tryAndReflect($name)) {
continue;
}

$results[] = $this->tryAndReflect($name);
}

return $results;
}

private function resolveShortName($declaredClass): string
{
$offset = strrpos($declaredClass, '\\');

if (false === $offset) {
return $declaredClass;
}

return substr($declaredClass, $offset + 1);
}
}