Skip to content

Commit ba08896

Browse files
committed
fixed interfaces' methods completion
Added interface method parsing and completion
1 parent fa448a2 commit ba08896

12 files changed

Lines changed: 127 additions & 54 deletions

File tree

src/Complete/Completer/ObjectCompleter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getEntries(Project $project, Context $context){
2121
/** @var FQCN $fqcn */
2222
list($fqcn, $isThis) = $context->getData();
2323
$this->logger->addDebug('creating entries');
24-
if(!($fqcn instanceof FQCN)){
24+
if(!$fqcn instanceof FQCN){
2525
return [];
2626
}
2727
$index = $project->getIndex();

src/Complete/Resolver/ContextResolver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ public function getContext($badLine, Index $index, Scope $scope = null){
3737
* @return Token
3838
*/
3939
protected function getLastToken($badLine){
40-
$symbols = token_get_all($this->prepareLine($badLine));
40+
try {
41+
$symbols = @token_get_all($this->prepareLine($badLine));
42+
}
43+
catch(\Exception $e){
44+
$symbols = [0,0];
45+
}
4146
$token = null;
4247
array_shift($symbols);
4348
do {

src/Complete/Resolver/NodeTypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ protected function getVarType($name, Scope $scope){
115115
}
116116
protected function getMethodType($name, FQCN $type, Index $index){
117117
$class = $index->findClassByFQCN($type);
118+
if(empty($class)){
119+
$class = $index->findInterfaceByFQCN($type);
120+
}
118121
if(empty($class)){
119122
return null;
120123
}
@@ -136,6 +139,8 @@ protected function getPropertyType($name, FQCN $type, Index $index){
136139
return $prop->getType();
137140
}
138141

142+
/** @property LoggerInterface */
139143
private $logger;
144+
/** @property UseParser */
140145
private $useParser;
141146
}

src/Entity/Collection/MethodsCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function remove(MethodData $method){
2121
unset($this->methods[$method->name]);
2222
}
2323
}
24-
public function get($name, Specifiaction $spec = null){
24+
public function get($name, Specification $spec = null){
2525
if($spec === null){
2626
$spec = new Specification;
2727
}

src/Entity/FQCN.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct($className, $namespace = "", $isArray=false){
2626
case "void":
2727
case "object":
2828
case "bool":
29+
case "null":
30+
case "false":
31+
case "true":
2932
$this->_isScalar = true;
3033
break;
3134
}
@@ -50,6 +53,13 @@ public function getNamespace(){
5053
array_pop($parts);
5154
return implode("\\", $parts);
5255
}
56+
public function toString(){
57+
$str = parent::toString();
58+
if($this->isArray()){
59+
$str .= '[]';
60+
}
61+
return $str;
62+
}
5363
public function isArray(){
5464
return $this->_isArray;
5565
}

src/Entity/Index.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function findClassByFQCN(FQCN $fqcn){
4444
if(array_key_exists($str, $this->classes)){
4545
return $this->classes[$str];
4646
}
47-
return null;
4847
}
4948

5049
/**
@@ -56,6 +55,10 @@ public function findInterfaceByFQCN(FQCN $fqcn){
5655
return $this->interfaces[$str];
5756
}
5857
}
58+
59+
/**
60+
* @return ClassData[]
61+
*/
5962
public function findClassChildren(FQCN $class){
6063
if(!array_key_exists($class->toString(), $this->extends)
6164
|| !is_array($this->extends[$class->toString()])
@@ -64,24 +67,53 @@ public function findClassChildren(FQCN $class){
6467
}
6568
return $this->extends[$class->toString()];
6669
}
70+
71+
/**
72+
* @return ClassData[]
73+
*/
74+
public function findInterfaceChildrenClasses(FQCN $interface){
75+
if(!array_key_exists($interface->toString(), $this->implements)
76+
|| !is_array($this->implements[$interface->toString()])
77+
){
78+
$this->implements[$interface->toString()] = [];
79+
}
80+
return $this->implements[$interface->toString()];
81+
}
82+
83+
/**
84+
* @return ClassData[]
85+
*/
6786
public function getClasses(){
6887
return $this->classes;
6988
}
89+
90+
/**
91+
* @return InterfaceData[]
92+
*/
93+
public function getInterfaces(){
94+
return $this->interfaces;
95+
}
96+
7097
public function addClass(ClassData $class){
7198
$this->classes[$class->fqcn->toString()] = $class;
7299
if(!empty($class->getParent())){
73100
$this->addExtend($class, $class->getParent());
74101
}
102+
foreach($class->getInterfaces() as $interface){
103+
$this->addImplement($class, $interface);
104+
}
75105
foreach($this->findClassChildren($class->fqcn) AS $child){
76106
$child->setParent($class);
77107
}
78108
}
109+
79110
public function addInterface(InterfaceData $interface){
80111
$this->interfaces[$interface->fqcn->toString()] = $interface;
112+
foreach($this->findInterfaceChildrenClasses($interface->fqcn) as $child){
113+
$child->addInterface($interface);
114+
}
81115
}
82-
public function getInterfaces(){
83-
return $this->interfaces;
84-
}
116+
85117
public function addFQCN(FQCN $fqcn){
86118
$this->fqcns[$fqcn->toString()] = $fqcn;
87119
}
@@ -112,13 +144,15 @@ protected function addExtend(ClassData $class, FQCN $parent){
112144
}
113145
}
114146

115-
protected function addImplement($class, $interface){
116-
if(!array_key_exists($interface, $this->implements)
117-
|| !is_array($this->implements[$interface])){
118-
$this->implements[$interface] = [];
147+
protected function addImplement(ClassData $class, FQCN $interface){
148+
if(!array_key_exists($interface->toString(), $this->implements)
149+
|| !is_array($this->implements[$interface->toString()])){
150+
$this->implements[$interface->toString()] = [];
119151
}
120-
if(!in_array($class, $this->implements[$interface])){
121-
$this->implements[$interface][] = $class;
152+
$this->implements[$interface->toString()][$class->fqcn->toString()] = $class;
153+
$interface = $this->findInterfaceByFQCN($interface);
154+
if($interface instanceof InterfaceData){
155+
$class->addInterface($interface);
122156
}
123157
}
124158

src/Entity/Node/ClassData.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,30 @@ public function __construct(FQCN $fqcn, $file){
3232
$this->methods = new MethodsCollection($this);
3333
$this->properties = new PropertiesCollection($this);
3434
}
35+
3536
/**
3637
* @return ClassData
3738
*/
3839
public function getParent(){
3940
return $this->parent;
4041
}
42+
43+
/**
44+
* @return InterfaceData[]
45+
*/
46+
public function getInterfaces(){
47+
return $this->interfaces;
48+
}
49+
4150
public function getName(){
4251
return $this->fqcn->getClassName();
4352
}
4453
public function setParent($parent){
4554
$this->parent = $parent;
4655
}
4756
public function addInterface($interface){
48-
if(!in_array($interface, $this->interfaces)){
49-
$this->interfaces[] = $interface;
50-
}
57+
$fqcn = $interface instanceof InterfaceData ? $interface->fqcn : $interface;
58+
$this->interfaces[$fqcn->toString()] = $interface;
5159
}
5260
public function addMethod(MethodData $method){
5361
$this->methods->add($method);

src/Entity/Node/Comment.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Entity\Node;
44

55
use Entity\FQCN;
6+
use Entity\Node\ClassProperty;
7+
use Entity\Node\Variable;
68

79
class Comment {
810

@@ -24,20 +26,39 @@ public function getProperties(){
2426
}
2527

2628
/**
27-
* @return Property
29+
* @return ClassProperty
2830
*/
2931
public function getProperty($name){
32+
$prop = null;
3033
if(array_key_exists($name, $this->properties)){
31-
return $this->properties[$name];
34+
$prop = $this->properties[$name];
3235
}
36+
if(!$prop instanceof ClassProperty && array_key_exists('', $this->properties)){
37+
$prop = $this->properties[''];
38+
}
39+
if(empty($prop)){
40+
$var = $this->getVar($name);
41+
if($var instanceof Variable){
42+
$prop = new ClassProperty;
43+
$prop->name = $var->getName();
44+
$prop->setType($var->getType());
45+
}
46+
}
47+
return $prop;
3348
}
49+
3450
/**
3551
* @return Variable
3652
*/
3753
public function getVar($name){
54+
$var = null;
3855
if(array_key_exists($name, $this->vars)){
39-
return $this->vars[$name];
56+
$var = $this->vars[$name];
57+
}
58+
if($var instanceof Variable && array_key_exists('', $this->vars)){
59+
$var = $this->vars[''];
4060
}
61+
return $var;
4162
}
4263
public function setReturn(FQCN $fqcn){
4364
$this->return = $fqcn;

src/Generator/IndexGenerator.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,26 +174,5 @@ protected function populateClassMapIndex(Project $project){
174174
$index = $project->getIndex();
175175
$index->setClassMap($classMap);
176176
}
177-
protected function populateExtendsIndex(Index $index, FQCN $fqcn, array $parentClasses = []){
178-
if(empty($parentClasses))
179-
return;
180-
foreach ($parentClasses as $parentClass) {
181-
if(empty($parentClass)) {
182-
continue;
183-
}
184-
$index->addExtend($fqcn->toString(), $parentClass);
185-
}
186-
}
187-
188-
protected function populateImplementsIndex(Index $index, FQCN $fqcn, array $interfaces = []){
189-
if(empty($interfaces))
190-
return;
191-
foreach ($interfaces as $interface) {
192-
if(empty($interface)) {
193-
return;
194-
}
195-
$index->addImplement($fqcn->toString(), $interface);
196-
}
197-
}
198177
}
199178

src/Parser/CommentParser.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ protected function createVar(Tag $tag){
8787
}
8888
protected function createProperty(Tag $tag){
8989
$name = trim($tag->getVariableName(), '$');
90-
if(empty($name)){
91-
$name = '-$';
92-
}
9390
$prop = new ClassProperty;
9491
$prop->name = $name;
9592
$prop->setType($this->getFQCN($tag->getType()));

0 commit comments

Comments
 (0)