1212class DefinitionResolver
1313{
1414 /**
15- * @var \LanguageServer\Index
15+ * @var \LanguageServer\Index\ReadableIndex
1616 */
1717 private $ index ;
1818
@@ -48,6 +48,7 @@ public function getDeclarationLineFromNode(Node $node): string
4848 // Properties and constants can have multiple declarations
4949 // Use the parent node (that includes the modifiers), but only render the requested declaration
5050 $ child = $ node ;
51+ /** @var Node */
5152 $ node = $ node ->getAttribute ('parentNode ' );
5253 $ defLine = clone $ node ;
5354 $ defLine ->props = [$ child ];
@@ -363,7 +364,7 @@ private static function getContainingClassFqn(Node $node)
363364 * Returns the assignment or parameter node where a variable was defined
364365 *
365366 * @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
367368 */
368369 public static function resolveVariableToNode (Node \Expr $ var )
369370 {
@@ -415,7 +416,7 @@ public static function resolveVariableToNode(Node\Expr $var)
415416 * If the type could not be resolved, returns Types\Mixed.
416417 *
417418 * @param \PhpParser\Node\Expr $expr
418- * @return \phpDocumentor\Type
419+ * @return \phpDocumentor\Reflection\ Type
419420 */
420421 public function resolveExpressionNodeToType (Node \Expr $ expr ): Type
421422 {
@@ -539,7 +540,7 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
539540 ]);
540541 }
541542 if (
542- $ expr instanceof Node \Expr \InstanceOf_
543+ $ expr instanceof Node \Expr \Instanceof_
543544 || $ expr instanceof Node \Expr \Cast \Bool_
544545 || $ expr instanceof Node \Expr \BooleanNot
545546 || $ expr instanceof Node \Expr \Empty_
@@ -559,19 +560,18 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
559560 return new Types \Boolean ;
560561 }
561562 if (
562- $ expr instanceof Node \Expr \Concat
563- || $ expr instanceof Node \Expr \Cast \String_
563+ $ expr instanceof Node \Expr \Cast \String_
564564 || $ expr instanceof Node \Expr \BinaryOp \Concat
565565 || $ 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_
575575 ) {
576576 return new Types \String_ ;
577577 }
@@ -580,23 +580,35 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
580580 || $ expr instanceof Node \Expr \BinaryOp \Plus
581581 || $ expr instanceof Node \Expr \BinaryOp \Pow
582582 || $ 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
584595 || $ expr instanceof Node \Expr \AssignOp \Plus
585596 || $ expr instanceof Node \Expr \AssignOp \Pow
586597 || $ expr instanceof Node \Expr \AssignOp \Mul
587598 ) {
588599 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
591602 ) {
592603 return new Types \Integer ;
593604 }
594605 return new Types \Float_ ;
595606 }
607+
596608 if (
597609 $ expr instanceof Node \Scalar \LNumber
598610 || $ expr instanceof Node \Expr \Cast \Int_
599- || $ expr instanceof Node \Expr \ Scalar \MagicConst \Line
611+ || $ expr instanceof Node \Scalar \MagicConst \Line
600612 || $ expr instanceof Node \Expr \BinaryOp \Spaceship
601613 || $ expr instanceof Node \Expr \BinaryOp \BitwiseAnd
602614 || $ expr instanceof Node \Expr \BinaryOp \BitwiseOr
@@ -606,7 +618,7 @@ public function resolveExpressionNodeToType(Node\Expr $expr): Type
606618 }
607619 if (
608620 $ expr instanceof Node \Expr \BinaryOp \Div
609- || $ expr instanceof Node \Expr \DNumber
621+ || $ expr instanceof Node \Scalar \DNumber
610622 || $ expr instanceof Node \Expr \Cast \Double
611623 ) {
612624 return new Types \Float_ ;
@@ -702,7 +714,7 @@ private static function resolveClassNameToType(Node $class): Type
702714 * Returns null if the node does not have a type.
703715 *
704716 * @param Node $node
705- * @return \phpDocumentor\Type|null
717+ * @return \phpDocumentor\Reflection\ Type|null
706718 */
707719 public function getTypeFromNode (Node $ node )
708720 {
@@ -720,6 +732,7 @@ public function getTypeFromNode(Node $node)
720732 }
721733 }
722734 }
735+ $ type = null ;
723736 if ($ node ->type !== null ) {
724737 // Use PHP7 return type hint
725738 if (is_string ($ node ->type )) {
@@ -792,7 +805,7 @@ public function getTypeFromNode(Node $node)
792805 }
793806 } else if ($ node instanceof Node \Const_) {
794807 return $ this ->resolveExpressionNodeToType ($ node ->value );
795- } else if ( $ node instanceof Node \ Expr \Assign || $ node instanceof Node \ Expr \AssignOp) {
808+ } else {
796809 return $ this ->resolveExpressionNodeToType ($ node );
797810 }
798811 // TODO: read @property tags of class
0 commit comments