From 00bc8537a6c299e71c90801f7083792e90778728 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 19 Nov 2016 11:45:25 +0100 Subject: [PATCH] Support compound types when resolving FQNs --- src/DefinitionResolver.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 6362daf..d5a908f 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -183,8 +183,27 @@ class DefinitionResolver } // Get the type of the left-hand expression $varType = $this->resolveExpressionNodeToType($node->var); - if ($varType instanceof Types\This) { - // $this is resolved to the containing class + if ($varType instanceof Types\Compound) { + // For compound types, use the first FQN we find + // (popular use case is ClassName|null) + for ($i = 0; $t = $varType->get($i); $i++) { + if ( + $t instanceof Types\This + || $t instanceof Types\Object_ + || $t instanceof Types\Static_ + || $t instanceof Types\Self_ + ) { + $varType = $t; + break; + } + } + } + if ( + $varType instanceof Types\This + || $varType instanceof Types\Static_ + || $varType instanceof Types\Self_ + ) { + // $this/static/self is resolved to the containing class $classFqn = self::getContainingClassFqn($node); } else if (!($varType instanceof Types\Object_) || $varType->getFqsen() === null) { // Left-hand expression could not be resolved to a class