diff --git a/src/collector/backend/parser/UnitCollectingVisitor.php b/src/collector/backend/parser/UnitCollectingVisitor.php index 2004ff57..cb30f1ce 100644 --- a/src/collector/backend/parser/UnitCollectingVisitor.php +++ b/src/collector/backend/parser/UnitCollectingVisitor.php @@ -36,7 +36,6 @@ */ namespace TheSeer\phpDox\Collector\Backend { - use TheSeer\DirectoryScanner\Exception; use TheSeer\phpDox\Collector\AbstractUnitObject; use TheSeer\phpDox\Collector\AbstractVariableObject; use TheSeer\phpDox\Collector\InlineComment; @@ -92,13 +91,15 @@ public function __construct(DocBlockParser $parser, ParseResult $result) { /** * @param \PhpParser\Node $node + * + * @return int|null|\PhpParser\Node|void */ public function enterNode(\PhpParser\Node $node) { if ($node instanceof NodeType\Namespace_ && $node->name != NULL) { - $this->namespace = join('\\', $node->name->parts); + $this->namespace = implode('\\', $node->name->parts); $this->aliasMap['::context'] = $this->namespace; } else if ($node instanceof NodeType\UseUse) { - $this->aliasMap[$node->alias] = join('\\', $node->name->parts); + $this->aliasMap[$node->alias] = implode('\\', $node->name->parts); } else if ($node instanceof NodeType\Class_) { $this->aliasMap['::unit'] = (string)$node->namespacedName; $this->unit = $this->result->addClass((string)$node->namespacedName); @@ -129,6 +130,8 @@ public function enterNode(\PhpParser\Node $node) { /** * @param \PhpParser\Node $node + * + * @return false|int|null|\PhpParser\Node|\PhpParser\Node[]|void */ public function leaveNode(\PhpParser\Node $node) { if ($node instanceof NodeType\Class_ @@ -159,20 +162,13 @@ private function processUnit($node) { $this->unit->setDocBlock($block); } - if ($node->getType() != 'Stmt_Trait' && $node->extends != NULL) { - if (is_array($node->extends)) { - foreach($node->extends as $extends) { - $this->unit->addExtends(join('\\', $extends->parts)); - } - } else { - $this->unit->addExtends(join('\\', $node->extends->parts)); - } - + if ($node->getType() !== 'Stmt_Trait' && $node->extends != NULL) { + $this->unit->addExtends(implode('\\', $node->extends->parts)); } - if ($node->getType() == 'Stmt_Class') { + if ($node->getType() === 'Stmt_Class') { foreach($node->implements as $implements) { - $this->unit->addImplements(join('\\', $implements->parts)); + $this->unit->addImplements(implode('\\', $implements->parts)); } } } @@ -345,22 +341,30 @@ private function processProperty(NodeType\Property $node) { } private function setVariableType(AbstractVariableObject $variable, $type = NULL) { + if ($type instanceof \PhpParser\Node\NullableType) { + $variable->setNullable(true); + $type = $type->type; + } + if ($type === NULL) { $variable->setType('{unknown}'); return; } - if ($type === 'array') { - $variable->setType('array'); + + if ($variable->isInternalType($type)) { + $variable->setType($type); return; } + if ($type instanceof \PhpParser\Node\Name\FullyQualified) { $variable->setType( (string)$type); return; } + $type = (string)$type; if (isset($this->aliasMap[$type])) { $type = $this->aliasMap[$type]; - } elseif ($type[0]!='\\') { + } elseif ($type[0]!=='\\') { $type = $this->namespace . '\\' . $type; } $variable->setType($type); @@ -401,12 +405,17 @@ private function resolveExpressionValue(\PhpParser\Node\Expr $expr) { return array( 'type' => '{unknown}', 'value' => '', - 'constant' => join('\\', $expr->class->parts) . '::' . $expr->name + 'constant' => implode('\\', $expr->class->parts) . '::' . $expr->name ); } if ($expr instanceof \PhpParser\Node\Expr\ConstFetch) { - $reference = join('\\', $expr->name->parts); + $reference = implode('\\', $expr->name->parts); + if (strtolower($reference) === 'null') { + return array( + 'value' => 'NULL' + ); + } if (in_array(strtolower($reference), array('true', 'false'))) { return array( 'type' => 'boolean', @@ -416,7 +425,7 @@ private function resolveExpressionValue(\PhpParser\Node\Expr $expr) { return array( 'type' => '{unknown}', 'value' => '', - 'constant' => join('\\', $expr->name->parts) + 'constant' => implode('\\', $expr->name->parts) ); } @@ -452,9 +461,14 @@ private function setVariableDefaultValue(AbstractVariableObject $variable, \PhpP if ($default === NULL) { return; } + $resolved = $this->resolveExpressionValue($default); - $variable->setType($resolved['type']); $variable->setDefault($resolved['value']); + + if (isset($resolved['type'])) { + $variable->setType($resolved['type']); + } + if (isset($resolved['constant'])) { $variable->setConstant($resolved['constant']); } diff --git a/src/collector/project/AbstractVariableObject.php b/src/collector/project/AbstractVariableObject.php index 6578f15a..7b2f250c 100644 --- a/src/collector/project/AbstractVariableObject.php +++ b/src/collector/project/AbstractVariableObject.php @@ -110,11 +110,15 @@ public function setConstant($const) { $this->ctx->setAttribute('constant', $const); } + public function isInternalType($type) { + return in_array(mb_strtolower($type), $this->types); + } + /** * @param $type */ public function setType($type) { - if (!in_array(mb_strtolower($type), $this->types)) { + if (!$this->isInternalType($type)) { $parts = explode('\\', $type); $local = array_pop($parts); $namespace = join('\\', $parts); @@ -135,6 +139,10 @@ public function getType() { return $this->ctx->getAttribute('type'); } + public function setNullable($isNullable) { + $this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false'); + } + protected function addInternalType($type) { $this->types[] = $type; } diff --git a/src/collector/project/ReturnTypeObject.php b/src/collector/project/ReturnTypeObject.php index 304fa287..234a131d 100644 --- a/src/collector/project/ReturnTypeObject.php +++ b/src/collector/project/ReturnTypeObject.php @@ -45,10 +45,6 @@ public function __construct(fDOMElement $ctx) { $this->addInternalType('void'); } - public function setNullable($isNullable) { - $this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false'); - } - } } diff --git a/tests/data/issue300/src/foo.php b/tests/data/issue300/src/foo.php new file mode 100644 index 00000000..79fd95d5 --- /dev/null +++ b/tests/data/issue300/src/foo.php @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + +