Skip to content

Commit cda77c9

Browse files
committed
Fix type resolving.
This should also fix #300.
1 parent fd5fe41 commit cda77c9

File tree

5 files changed

+76
-26
lines changed

5 files changed

+76
-26
lines changed

src/collector/backend/parser/UnitCollectingVisitor.php

+35-21
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
*/
3737
namespace TheSeer\phpDox\Collector\Backend {
3838

39-
use TheSeer\DirectoryScanner\Exception;
4039
use TheSeer\phpDox\Collector\AbstractUnitObject;
4140
use TheSeer\phpDox\Collector\AbstractVariableObject;
4241
use TheSeer\phpDox\Collector\InlineComment;
@@ -92,13 +91,15 @@ public function __construct(DocBlockParser $parser, ParseResult $result) {
9291

9392
/**
9493
* @param \PhpParser\Node $node
94+
*
95+
* @return int|null|\PhpParser\Node|void
9596
*/
9697
public function enterNode(\PhpParser\Node $node) {
9798
if ($node instanceof NodeType\Namespace_ && $node->name != NULL) {
98-
$this->namespace = join('\\', $node->name->parts);
99+
$this->namespace = implode('\\', $node->name->parts);
99100
$this->aliasMap['::context'] = $this->namespace;
100101
} else if ($node instanceof NodeType\UseUse) {
101-
$this->aliasMap[$node->alias] = join('\\', $node->name->parts);
102+
$this->aliasMap[$node->alias] = implode('\\', $node->name->parts);
102103
} else if ($node instanceof NodeType\Class_) {
103104
$this->aliasMap['::unit'] = (string)$node->namespacedName;
104105
$this->unit = $this->result->addClass((string)$node->namespacedName);
@@ -129,6 +130,8 @@ public function enterNode(\PhpParser\Node $node) {
129130

130131
/**
131132
* @param \PhpParser\Node $node
133+
*
134+
* @return false|int|null|\PhpParser\Node|\PhpParser\Node[]|void
132135
*/
133136
public function leaveNode(\PhpParser\Node $node) {
134137
if ($node instanceof NodeType\Class_
@@ -159,20 +162,13 @@ private function processUnit($node) {
159162
$this->unit->setDocBlock($block);
160163
}
161164

162-
if ($node->getType() != 'Stmt_Trait' && $node->extends != NULL) {
163-
if (is_array($node->extends)) {
164-
foreach($node->extends as $extends) {
165-
$this->unit->addExtends(join('\\', $extends->parts));
166-
}
167-
} else {
168-
$this->unit->addExtends(join('\\', $node->extends->parts));
169-
}
170-
165+
if ($node->getType() !== 'Stmt_Trait' && $node->extends != NULL) {
166+
$this->unit->addExtends(implode('\\', $node->extends->parts));
171167
}
172168

173-
if ($node->getType() == 'Stmt_Class') {
169+
if ($node->getType() === 'Stmt_Class') {
174170
foreach($node->implements as $implements) {
175-
$this->unit->addImplements(join('\\', $implements->parts));
171+
$this->unit->addImplements(implode('\\', $implements->parts));
176172
}
177173
}
178174
}
@@ -345,22 +341,30 @@ private function processProperty(NodeType\Property $node) {
345341
}
346342

347343
private function setVariableType(AbstractVariableObject $variable, $type = NULL) {
344+
if ($type instanceof \PhpParser\Node\NullableType) {
345+
$variable->setNullable(true);
346+
$type = $type->type;
347+
}
348+
348349
if ($type === NULL) {
349350
$variable->setType('{unknown}');
350351
return;
351352
}
352-
if ($type === 'array') {
353-
$variable->setType('array');
353+
354+
if ($variable->isInternalType($type)) {
355+
$variable->setType($type);
354356
return;
355357
}
358+
356359
if ($type instanceof \PhpParser\Node\Name\FullyQualified) {
357360
$variable->setType( (string)$type);
358361
return;
359362
}
363+
360364
$type = (string)$type;
361365
if (isset($this->aliasMap[$type])) {
362366
$type = $this->aliasMap[$type];
363-
} elseif ($type[0]!='\\') {
367+
} elseif ($type[0]!=='\\') {
364368
$type = $this->namespace . '\\' . $type;
365369
}
366370
$variable->setType($type);
@@ -401,12 +405,17 @@ private function resolveExpressionValue(\PhpParser\Node\Expr $expr) {
401405
return array(
402406
'type' => '{unknown}',
403407
'value' => '',
404-
'constant' => join('\\', $expr->class->parts) . '::' . $expr->name
408+
'constant' => implode('\\', $expr->class->parts) . '::' . $expr->name
405409
);
406410
}
407411

408412
if ($expr instanceof \PhpParser\Node\Expr\ConstFetch) {
409-
$reference = join('\\', $expr->name->parts);
413+
$reference = implode('\\', $expr->name->parts);
414+
if (strtolower($reference) === 'null') {
415+
return array(
416+
'value' => 'NULL'
417+
);
418+
}
410419
if (in_array(strtolower($reference), array('true', 'false'))) {
411420
return array(
412421
'type' => 'boolean',
@@ -416,7 +425,7 @@ private function resolveExpressionValue(\PhpParser\Node\Expr $expr) {
416425
return array(
417426
'type' => '{unknown}',
418427
'value' => '',
419-
'constant' => join('\\', $expr->name->parts)
428+
'constant' => implode('\\', $expr->name->parts)
420429
);
421430
}
422431

@@ -452,9 +461,14 @@ private function setVariableDefaultValue(AbstractVariableObject $variable, \PhpP
452461
if ($default === NULL) {
453462
return;
454463
}
464+
455465
$resolved = $this->resolveExpressionValue($default);
456-
$variable->setType($resolved['type']);
457466
$variable->setDefault($resolved['value']);
467+
468+
if (isset($resolved['type'])) {
469+
$variable->setType($resolved['type']);
470+
}
471+
458472
if (isset($resolved['constant'])) {
459473
$variable->setConstant($resolved['constant']);
460474
}

src/collector/project/AbstractVariableObject.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ public function setConstant($const) {
110110
$this->ctx->setAttribute('constant', $const);
111111
}
112112

113+
public function isInternalType($type) {
114+
return in_array(mb_strtolower($type), $this->types);
115+
}
116+
113117
/**
114118
* @param $type
115119
*/
116120
public function setType($type) {
117-
if (!in_array(mb_strtolower($type), $this->types)) {
121+
if (!$this->isInternalType($type)) {
118122
$parts = explode('\\', $type);
119123
$local = array_pop($parts);
120124
$namespace = join('\\', $parts);
@@ -135,6 +139,10 @@ public function getType() {
135139
return $this->ctx->getAttribute('type');
136140
}
137141

142+
public function setNullable($isNullable) {
143+
$this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false');
144+
}
145+
138146
protected function addInternalType($type) {
139147
$this->types[] = $type;
140148
}

src/collector/project/ReturnTypeObject.php

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public function __construct(fDOMElement $ctx) {
4545
$this->addInternalType('void');
4646
}
4747

48-
public function setNullable($isNullable) {
49-
$this->ctx->setAttribute('nullable', $isNullable ? 'true' : 'false');
50-
}
51-
5248
}
5349

5450
}

tests/data/issue300/src/foo.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace bar;
3+
4+
class foo {
5+
6+
public function __construct(
7+
Request $request,
8+
array $roles,
9+
Router $router,
10+
FacebookHelper $facebookHelper,
11+
string $targetRoute = 'facebook_check',
12+
?Logger $logger = null
13+
) {
14+
parent::__construct($roles);
15+
}
16+
17+
}

tests/data/issue300/test.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config" silent="false">
3+
4+
<project name="phpDox-issue300" source="${basedir}/src" workdir="${basedir}/xml">
5+
6+
<collector publiconly="false" backend="parser" />
7+
8+
<generator output="${basedir}/docs">
9+
<build engine="html" enabled="true" output="html" />
10+
<build engine="xml" enabled="true" output="xml" />
11+
</generator>
12+
13+
</project>
14+
15+
</phpdox>

0 commit comments

Comments
 (0)