Completed
Pull Request — master (#79)
by
unknown
02:42
created

UseCompleter::getEntries()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 8.439
c 1
b 0
f 0
cc 5
eloc 22
nc 3
nop 2
1
<?php
2
3
namespace Padawan\Domain\Completer;
4
5
use Padawan\Domain\Project;
6
use Padawan\Domain\Completion\Context;
7
use Padawan\Domain\Completion\Entry;
8
9
class UseCompleter extends AbstractFileInfoCompleter
10
{
11
    public function getEntries(Project $project, Context $context)
12
    {
13
        $entries = [];
14
        $postfix = trim($context->getData());
15
        $index = $project->getIndex();
16
        $fqcns = array_merge($index->getClasses(), $index->getInterfaces());
17
        foreach ($fqcns as $fqcn => $class) {
18
            if (!empty($postfix) && strpos($fqcn, $postfix) === false) {
19
                continue;
20
            }
21
            $complete = str_replace($postfix, "", $fqcn);
22
            $entries[] = new Entry(
23
                $complete,
24
                '',
25
                '',
26
                $fqcn
27
            );
28
        }
29
        usort($entries, function($a, $b) {
30
            $aname = $a->getName();
31
            $bname = $b->getName();
32
            $strlenDiff = strlen($aname) - strlen($bname);
33
            if ($strlenDiff === 0) {
34
                return $aname > $bname;
35
            }
36
            return $strlenDiff;
37
        });
38
        return $entries;
39
    }
40
41
    public function canHandle(Project $project, Context $context)
42
    {
43
        return parent::canHandle($project, $context) && $context->isUse();
44
    }
45
}
46