Skip to content

Commit

Permalink
Fix type resolving.
Browse files Browse the repository at this point in the history
This should also fix #300.
  • Loading branch information
theseer committed Jul 2, 2017
1 parent fd5fe41 commit cda77c9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
56 changes: 35 additions & 21 deletions src/collector/backend/parser/UnitCollectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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)
);
}

Expand Down Expand Up @@ -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']);
}
Expand Down
10 changes: 9 additions & 1 deletion src/collector/project/AbstractVariableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions src/collector/project/ReturnTypeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public function __construct(fDOMElement $ctx) {
$this->addInternalType('void');
}

public function setNullable($isNullable) {
$this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false');
}

}

}
17 changes: 17 additions & 0 deletions tests/data/issue300/src/foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace bar;

class foo {

public function __construct(
Request $request,
array $roles,
Router $router,
FacebookHelper $facebookHelper,
string $targetRoute = 'facebook_check',
?Logger $logger = null
) {
parent::__construct($roles);
}

}
15 changes: 15 additions & 0 deletions tests/data/issue300/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpdox xmlns="http://xml.phpdox.net/config" silent="false">

<project name="phpDox-issue300" source="${basedir}/src" workdir="${basedir}/xml">

<collector publiconly="false" backend="parser" />

<generator output="${basedir}/docs">
<build engine="html" enabled="true" output="html" />
<build engine="xml" enabled="true" output="xml" />
</generator>

</project>

</phpdox>

0 comments on commit cda77c9

Please sign in to comment.