|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padawan\Domain\Completer; |
|
4
|
|
|
|
|
5
|
|
|
use Padawan\Domain\Core\Project; |
|
6
|
|
|
use Padawan\Domain\Core\FQCN; |
|
7
|
|
|
use Padawan\Domain\Core\Node\MethodData; |
|
8
|
|
|
use Padawan\Domain\Core\Node\ClassProperty; |
|
9
|
|
|
use Padawan\Domain\Core\Node\InterfaceData; |
|
10
|
|
|
use Padawan\Domain\Core\Completion\Context; |
|
11
|
|
|
use Padawan\Domain\Core\Completion\Entry; |
|
12
|
|
|
use Padawan\Domain\Core\Collection\Specification; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
|
|
15
|
|
|
class StaticCompleter extends AbstractInCodeBodyCompleter |
|
16
|
|
|
{ |
|
17
|
|
|
public function __construct(LoggerInterface $logger) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->logger = $logger; |
|
20
|
|
|
} |
|
21
|
|
|
public function getEntries(Project $project, Context $context) |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var FQCN $fqcn */ |
|
24
|
|
|
list($fqcn, $isThis) = $context->getData(); |
|
25
|
|
|
$this->logger->debug('creating static entries'); |
|
26
|
|
|
if (!$fqcn instanceof FQCN) { |
|
27
|
|
|
return []; |
|
28
|
|
|
} |
|
29
|
|
|
$index = $project->getIndex(); |
|
30
|
|
|
$this->logger->debug('Creating completion for ' . $fqcn->toString()); |
|
31
|
|
|
$class = $index->findClassByFQCN($fqcn); |
|
32
|
|
|
if (empty($class)) { |
|
33
|
|
|
$class = $index->findInterfaceByFQCN($fqcn); |
|
34
|
|
|
} |
|
35
|
|
|
if (empty($class)) { |
|
36
|
|
|
return []; |
|
37
|
|
|
} |
|
38
|
|
|
$entries = []; |
|
39
|
|
|
$spec = new Specification($isThis ? 'private' : 'public', 1); |
|
|
|
|
|
|
40
|
|
View Code Duplication |
if ($class->methods !== null) { |
|
|
|
|
|
|
41
|
|
|
foreach ($class->methods->all($spec) as $method) { |
|
42
|
|
|
$entry = $this->createEntryForMethod($method); |
|
43
|
|
|
$entries[$method->name] = $entry; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
if ($class instanceof InterfaceData) { |
|
47
|
|
|
return $entries; |
|
48
|
|
|
} |
|
49
|
|
View Code Duplication |
if ($class->properties !== null) { |
|
|
|
|
|
|
50
|
|
|
foreach ($class->properties->all($spec) as $property) { |
|
|
|
|
|
|
51
|
|
|
$entries[$property->name] = $this->createEntryForProperty($property); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
if ($class->constants !== null) { |
|
|
|
|
|
|
55
|
|
|
foreach ($class->constants->all() as $const) { |
|
|
|
|
|
|
56
|
|
|
$entries[$const] = $this->createEntryForConst($const); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
ksort($entries); |
|
60
|
|
|
return $entries; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function canHandle(Project $project, Context $context) |
|
64
|
|
|
{ |
|
65
|
|
|
return parent::canHandle($project, $context) && $context->isClassStatic(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Creates menu entry for MethodData |
|
70
|
|
|
* |
|
71
|
|
|
* @param MethodData $method a method |
|
72
|
|
|
* @return Entry |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createEntryForMethod(MethodData $method) |
|
75
|
|
|
{ |
|
76
|
|
|
return new Entry( |
|
77
|
|
|
$method->name, |
|
78
|
|
|
$method->getSignature(), |
|
79
|
|
|
sprintf("%s\n%s\n", $method->getSignature(), $method->doc) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
protected function createEntryForProperty(ClassProperty $prop) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$type = $prop->type instanceof FQCN ? $prop->type->toString() : 'mixed'; |
|
86
|
|
|
return new Entry( |
|
87
|
|
|
'$' . $prop->name, |
|
88
|
|
|
$type |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function createEntryForConst($const) |
|
93
|
|
|
{ |
|
94
|
|
|
return new Entry($const); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** @property LoggerInterface */ |
|
98
|
|
|
private $logger; |
|
99
|
|
|
} |
|
100
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: