diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index bbf90764..5a6f88bc 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -725,6 +725,19 @@ private static function resolveClassNameToType(Node $class): Type */ public function getTypeFromNode(Node $node) { + if ( + $node instanceof Node\Expr\FuncCall + && $node->name instanceof Node\Name + && strtolower((string)$node->name) === 'define' + && isset($node->args[0]) + && $node->args[0]->value instanceof Node\Scalar\String_ + && isset($node->args[1]) + ) { + // constants with define() like + // define('TEST_DEFINE_CONSTANT', false); + return $this->resolveExpressionNodeToType($node->args[1]->value); + } + if ($node instanceof Node\Param) { // Parameters $docBlock = $node->getAttribute('parentNode')->getAttribute('docBlock'); @@ -882,11 +895,16 @@ public static function getDefinedFqn(Node $node) } return (string)$class->namespacedName . '::' . $node->name; } - } else if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && strtolower((string)$node->name) === 'define') { - if (!isset($node->args[0]) || !($node->args[0]->value instanceof Node\Scalar\String_)) { - return null; - } + } else if ( + $node instanceof Node\Expr\FuncCall + && $node->name instanceof Node\Name + && strtolower((string)$node->name) === 'define' + && isset($node->args[0]) + && $node->args[0]->value instanceof Node\Scalar\String_ + && isset($node->args[1]) + ) { return (string)$node->args[0]->value->value; } + return null; } } diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 4fb1d3fc..da044049 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -57,6 +57,7 @@ public static function fromNode(Node $node, string $fqn = null) && strtolower((string)$node->name) === 'define' && isset($node->args[0]) && $node->args[0]->value instanceof Node\Scalar\String_ + && isset($node->args[1]) ) { // constants with define() like // define('TEST_DEFINE_CONSTANT', false); @@ -90,7 +91,7 @@ public static function fromNode(Node $node, string $fqn = null) } else { return null; } - + if (!isset($symbol->name)) { if ($node instanceof Node\Name) { $symbol->name = (string)$node; diff --git a/tests/DefinitionResolverTest.php b/tests/DefinitionResolverTest.php new file mode 100644 index 00000000..0046ef08 --- /dev/null +++ b/tests/DefinitionResolverTest.php @@ -0,0 +1,64 @@ +parse("setAttribute('ownerDocument', new MockPhpDocument); + + $index = new Index; + $definitionResolver = new DefinitionResolver($index); + $def = $definitionResolver->createDefinitionFromNode($stmts[0], '\TEST_DEFINE'); + + $this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $def->type); + } + + public function testGetTypeFromNode() + { + $parser = new Parser; + $stmts = $parser->parse("setAttribute('ownerDocument', new MockPhpDocument); + + $index = new Index; + $definitionResolver = new DefinitionResolver($index); + $type = $definitionResolver->getTypeFromNode($stmts[0]); + + $this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $type); + } + + public function testGetDefinedFqnForIncompleteDefine() + { + // define('XXX') (only one argument) must not introduce a new symbol + $parser = new Parser; + $stmts = $parser->parse("setAttribute('ownerDocument', new MockPhpDocument); + + $index = new Index; + $definitionResolver = new DefinitionResolver($index); + $fqn = $definitionResolver->getDefinedFqn($stmts[0]); + + $this->assertNull($fqn); + } + + public function testGetDefinedFqnForDefine() + { + $parser = new Parser; + $stmts = $parser->parse("setAttribute('ownerDocument', new MockPhpDocument); + + $index = new Index; + $definitionResolver = new DefinitionResolver($index); + $fqn = $definitionResolver->getDefinedFqn($stmts[0]); + + $this->assertEquals('TEST_DEFINE', $fqn); + } +} diff --git a/tests/MockPhpDocument.php b/tests/MockPhpDocument.php new file mode 100644 index 00000000..48d4b70d --- /dev/null +++ b/tests/MockPhpDocument.php @@ -0,0 +1,20 @@ +