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(); |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.