ObjectCompleter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Padawan\Domain\Completer;
4
5
use Padawan\Domain\Project;
6
use Padawan\Domain\Project\FQCN;
7
use Padawan\Domain\Project\ClassRepository;
8
use Padawan\Domain\Project\Node\MethodData;
9
use Padawan\Domain\Project\Node\ClassProperty;
10
use Padawan\Domain\Project\Node\InterfaceData;
11
use Padawan\Domain\Completion\Context;
12
use Padawan\Domain\Completion\Entry;
13
use Padawan\Domain\Project\Collection\Specification;
14
use Psr\Log\LoggerInterface;
15
16
class ObjectCompleter extends AbstractInCodeBodyCompleter
17
{
18
19
    /** @property LoggerInterface */
20
    private $logger;
21
22
    /** @property ClassRepository */
23
    private $classRepository;
24
25
    public function __construct(
26
        LoggerInterface $logger,
27
        ClassRepository $classRepository
28
    ) {
29
        $this->logger = $logger;
30
        $this->classRepository = $classRepository;
31
    }
32
    public function getEntries(Project $project, Context $context)
33
    {
34
        /** @var FQCN $fqcn */
35
        list($fqcn, $isThis) = $context->getData();
36
        $this->logger->debug('creating entries');
37
        if (!$fqcn instanceof FQCN) {
38
            return [];
39
        }
40
        $index = $project->getIndex();
0 ignored issues
show
Unused Code introduced by
$index is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
        $this->logger->debug('Creating completion for ' . $fqcn->toString());
42
        $class = $this->classRepository->findByName($project, $fqcn);
43
        if (empty($class)) {
44
            return [];
45
        }
46
        $entries = [];
47
        $spec = new Specification($isThis ? 'private' : 'public');
48 View Code Duplication
        if ($class->methods !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            foreach ($class->methods->all($spec) as $method) {
50
                $entry = $this->createEntryForMethod($method);
51
                $entries[$method->name] = $entry;
52
            }
53
        }
54
        if ($class instanceof InterfaceData) {
55
            return $entries;
56
        }
57 View Code Duplication
        if ($class->properties !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            foreach ($class->properties->all($spec) as $property) {
59
                $entries[$property->name] = $this->createEntryForProperty($property);
60
            }
61
        }
62
        ksort($entries);
63
        return $entries;
64
    }
65
66
    public function canHandle(Project $project, Context $context)
67
    {
68
        return parent::canHandle($project, $context) && ($context->isThis() || $context->isObject());
69
    }
70
71
    /**
72
     * Creates menu entry for MethodData
73
     *
74
     * @param MethodData $method a method
75
     * @return Entry
76
     */
77
    protected function createEntryForMethod(MethodData $method)
78
    {
79
        return new Entry(
80
            $method->name,
81
            $method->getSignature(),
82
            sprintf("%s\n%s\n", $method->getSignature(), $method->doc)
83
        );
84
    }
85
86
    protected function createEntryForProperty(ClassProperty $prop)
87
    {
88
        $type = $prop->type instanceof FQCN ? $prop->type->getClassName() : 'mixed';
89
        return new Entry(
90
            $prop->name,
91
            $type
92
        );
93
    }
94
}
95