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

UseCompleter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 37
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getEntries() 0 29 5
A canHandle() 0 4 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