Completed
Push — master ( 81db43...f030be )
by Aleh
12s
created

GlobalFunctionsCompleter::canHandle()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 8.8571
cc 5
eloc 7
nc 8
nop 2
1
<?php
2
3
namespace Padawan\Domain\Completer;
4
5
use Padawan\Domain\Project;
6
use Padawan\Domain\Completion\Entry;
7
use Padawan\Domain\Completion\Context;
8
use Padawan\Domain\Project\ClassRepository;
9
use Padawan\Domain\Project\Node\FunctionData;
10
11
class GlobalFunctionsCompleter extends AbstractInCodeBodyCompleter
12
{
13
14
    /** @property ClassRepository */
15
    private $classRepository;
16
17
    public function __construct(
18
        ClassRepository $classRepository
19
    ) {
20
        $this->classRepository = $classRepository;
21
    }
22
23
    public function getEntries(Project $project, Context $context)
24
    {
25
        $entries = [];
26
        $postfix = trim($context->getData());
27
        foreach ($project->getIndex()->getFunctions() as $function) {
28
            /** @var FunctionData $function */
29
            $name = $function->name;
30
            if (!empty($postfix) && strpos($name, $postfix) !== 0) {
31
                continue;
32
            }
33
            $nameToComplete = str_replace($postfix, "", $name);
34
            $entries[$name] = new Entry(
35
                $nameToComplete,
36
                $function->getSignature(),
37
                $function->doc,
38
                $name
39
            );
40
        }
41
        $entries = array_values($entries);
42
        return $entries;
43
    }
44
45
    public function canHandle(Project $project, Context $context)
46
    {
47
        $postfix = "";
48
        if (is_string($context->getData())) {
49
            $postfix = trim($context->getData());
50
        }
51
        return parent::canHandle($project, $context)
52
            && ($context->isString() || $context->isEmpty())
53
            && strlen($postfix) > 0
54
            ;
55
    }
56
}
57