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 ObjectCompleter 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 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'); |
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
|
|
|
ksort($entries); |
55
|
|
|
return $entries; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function canHandle(Project $project, Context $context) |
59
|
|
|
{ |
60
|
|
|
return parent::canHandle($project, $context) && ($context->isThis() || $context->isObject()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Creates menu entry for MethodData |
65
|
|
|
* |
66
|
|
|
* @param MethodData $method a method |
67
|
|
|
* @return Entry |
68
|
|
|
*/ |
69
|
|
|
protected function createEntryForMethod(MethodData $method) |
70
|
|
|
{ |
71
|
|
|
return new Entry( |
72
|
|
|
$method->name, |
73
|
|
|
$method->getSignature(), |
74
|
|
|
sprintf("%s\n%s\n", $method->getSignature(), $method->doc) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
protected function createEntryForProperty(ClassProperty $prop) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$type = $prop->type instanceof FQCN ? $prop->type->getClassName() : 'mixed'; |
81
|
|
|
return new Entry( |
82
|
|
|
$prop->name, |
83
|
|
|
$type |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @property LoggerInterface */ |
88
|
|
|
private $logger; |
89
|
|
|
} |
90
|
|
|
|
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.