12
12
class DefinitionResolver
13
13
{
14
14
/**
15
- * @var \LanguageServer\Index
15
+ * @var \LanguageServer\Index\ReadableIndex
16
16
*/
17
17
private $ index ;
18
18
@@ -48,6 +48,7 @@ public function getDeclarationLineFromNode(Node $node): string
48
48
// Properties and constants can have multiple declarations
49
49
// Use the parent node (that includes the modifiers), but only render the requested declaration
50
50
$ child = $ node ;
51
+ /** @var Node */
51
52
$ node = $ node ->getAttribute ('parentNode ' );
52
53
$ defLine = clone $ node ;
53
54
$ defLine ->props = [$ child ];
@@ -363,7 +364,7 @@ private static function getContainingClassFqn(Node $node)
363
364
* Returns the assignment or parameter node where a variable was defined
364
365
*
365
366
* @param Node\Expr\Variable|Node\Expr\ClosureUse $var The variable access
366
- * @return Node\Expr\Assign|Node\Param|Node\Expr\ClosureUse|null
367
+ * @return Node\Expr\Assign|Node\Expr\AssignOp|Node\ Param|Node\Expr\ClosureUse|null
367
368
*/
368
369
public static function resolveVariableToNode (Node \Expr $ var )
369
370
{
@@ -415,7 +416,7 @@ public static function resolveVariableToNode(Node\Expr $var)
415
416
* If the type could not be resolved, returns Types\Mixed.
416
417
*
417
418
* @param \PhpParser\Node\Expr $expr
418
- * @return \phpDocumentor\Type
419
+ * @return \phpDocumentor\Reflection\ Type
419
420
*/
420
421
public function resolveExpressionNodeToType (Node \Expr $ expr ): Type
421
422
{
@@ -539,7 +540,7 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
539
540
]);
540
541
}
541
542
if (
542
- $ expr instanceof Node \Expr \InstanceOf_
543
+ $ expr instanceof Node \Expr \Instanceof_
543
544
|| $ expr instanceof Node \Expr \Cast \Bool_
544
545
|| $ expr instanceof Node \Expr \BooleanNot
545
546
|| $ expr instanceof Node \Expr \Empty_
@@ -559,19 +560,18 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
559
560
return new Types \Boolean ;
560
561
}
561
562
if (
562
- $ expr instanceof Node \Expr \Concat
563
- || $ expr instanceof Node \Expr \Cast \String_
563
+ $ expr instanceof Node \Expr \Cast \String_
564
564
|| $ expr instanceof Node \Expr \BinaryOp \Concat
565
565
|| $ expr instanceof Node \Expr \AssignOp \Concat
566
- || $ expr instanceof Node \Expr \ Scalar \String_
567
- || $ expr instanceof Node \Expr \ Scalar \Encapsed
568
- || $ expr instanceof Node \Expr \ Scalar \EncapsedStringPart
569
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Class_
570
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Dir
571
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Function_
572
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Method
573
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Namespace_
574
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Trait_
566
+ || $ expr instanceof Node \Scalar \String_
567
+ || $ expr instanceof Node \Scalar \Encapsed
568
+ || $ expr instanceof Node \Scalar \EncapsedStringPart
569
+ || $ expr instanceof Node \Scalar \MagicConst \Class_
570
+ || $ expr instanceof Node \Scalar \MagicConst \Dir
571
+ || $ expr instanceof Node \Scalar \MagicConst \Function_
572
+ || $ expr instanceof Node \Scalar \MagicConst \Method
573
+ || $ expr instanceof Node \Scalar \MagicConst \Namespace_
574
+ || $ expr instanceof Node \Scalar \MagicConst \Trait_
575
575
) {
576
576
return new Types \String_ ;
577
577
}
@@ -580,23 +580,35 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
580
580
|| $ expr instanceof Node \Expr \BinaryOp \Plus
581
581
|| $ expr instanceof Node \Expr \BinaryOp \Pow
582
582
|| $ expr instanceof Node \Expr \BinaryOp \Mul
583
- || $ expr instanceof Node \Expr \AssignOp \Minus
583
+ ) {
584
+ if (
585
+ $ this ->resolveExpressionNodeToType ($ expr ->left ) instanceof Types \Integer
586
+ && $ this ->resolveExpressionNodeToType ($ expr ->right ) instanceof Types \Integer
587
+ ) {
588
+ return new Types \Integer ;
589
+ }
590
+ return new Types \Float_ ;
591
+ }
592
+
593
+ if (
594
+ $ expr instanceof Node \Expr \AssignOp \Minus
584
595
|| $ expr instanceof Node \Expr \AssignOp \Plus
585
596
|| $ expr instanceof Node \Expr \AssignOp \Pow
586
597
|| $ expr instanceof Node \Expr \AssignOp \Mul
587
598
) {
588
599
if (
589
- $ this ->resolveExpressionNodeToType ($ expr ->left ) instanceof Types \Integer_
590
- && $ this ->resolveExpressionNodeToType ($ expr ->right ) instanceof Types \Integer_
600
+ $ this ->resolveExpressionNodeToType ($ expr ->var ) instanceof Types \Integer
601
+ && $ this ->resolveExpressionNodeToType ($ expr ->expr ) instanceof Types \Integer
591
602
) {
592
603
return new Types \Integer ;
593
604
}
594
605
return new Types \Float_ ;
595
606
}
607
+
596
608
if (
597
609
$ expr instanceof Node \Scalar \LNumber
598
610
|| $ expr instanceof Node \Expr \Cast \Int_
599
- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Line
611
+ || $ expr instanceof Node \Scalar \MagicConst \Line
600
612
|| $ expr instanceof Node \Expr \BinaryOp \Spaceship
601
613
|| $ expr instanceof Node \Expr \BinaryOp \BitwiseAnd
602
614
|| $ expr instanceof Node \Expr \BinaryOp \BitwiseOr
@@ -606,7 +618,7 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
606
618
}
607
619
if (
608
620
$ expr instanceof Node \Expr \BinaryOp \Div
609
- || $ expr instanceof Node \Expr \DNumber
621
+ || $ expr instanceof Node \Scalar \DNumber
610
622
|| $ expr instanceof Node \Expr \Cast \Double
611
623
) {
612
624
return new Types \Float_ ;
@@ -702,7 +714,7 @@ private static function resolveClassNameToType(Node $class): Type
702
714
* Returns null if the node does not have a type.
703
715
*
704
716
* @param Node $node
705
- * @return \phpDocumentor\Type|null
717
+ * @return \phpDocumentor\Reflection\ Type|null
706
718
*/
707
719
public function getTypeFromNode (Node $ node )
708
720
{
@@ -720,6 +732,7 @@ public function getTypeFromNode(Node $node)
720
732
}
721
733
}
722
734
}
735
+ $ type = null ;
723
736
if ($ node ->type !== null ) {
724
737
// Use PHP7 return type hint
725
738
if (is_string ($ node ->type )) {
@@ -792,7 +805,7 @@ public function getTypeFromNode(Node $node)
792
805
}
793
806
} else if ($ node instanceof Node \Const_) {
794
807
return $ this ->resolveExpressionNodeToType ($ node ->value );
795
- } else if ( $ node instanceof Node \ Expr \Assign || $ node instanceof Node \ Expr \AssignOp) {
808
+ } else {
796
809
return $ this ->resolveExpressionNodeToType ($ node );
797
810
}
798
811
// TODO: read @property tags of class
0 commit comments