Skip to content

Commit 78e9242

Browse files
committed
Added support for type 'self' to @var and @return annotations, also initial support for 'static'
1 parent 1c3f231 commit 78e9242

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/collector/backend/parser/UnitCollectingVisitor.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class UnitCollectingVisitor extends NodeVisitorAbstract {
5252
/**
5353
* @var \TheSeer\phpDox\DocBlock\Parser
5454
*/
55-
private $dockblocParser;
55+
private $docBlockParser;
5656

5757
/**
5858
* @var array
@@ -85,7 +85,7 @@ class UnitCollectingVisitor extends NodeVisitorAbstract {
8585
* @param ParseResult $result
8686
*/
8787
public function __construct(DocBlockParser $parser, ParseResult $result) {
88-
$this->dockblocParser = $parser;
88+
$this->docBlockParser = $parser;
8989
$this->result = $result;
9090
}
9191

@@ -99,14 +99,17 @@ public function enterNode(\PhpParser\Node $node) {
9999
} else if ($node instanceof NodeType\UseUse) {
100100
$this->aliasMap[$node->alias] = join('\\', $node->name->parts);
101101
} else if ($node instanceof NodeType\Class_) {
102+
$this->aliasMap['::unit'] = (string)$node->namespacedName;
102103
$this->unit = $this->result->addClass((string)$node->namespacedName);
103104
$this->processUnit($node);
104105
return;
105106
} else if ($node instanceof NodeType\Interface_) {
107+
$this->aliasMap['::unit'] = (string)$node->namespacedName;
106108
$this->unit = $this->result->addInterface((string)$node->namespacedName);
107109
$this->processUnit($node);
108110
return;
109111
} else if ($node instanceof NodeType\Trait_) {
112+
$this->aliasMap['::unit'] = (string)$node->namespacedName;
110113
$this->unit = $this->result->addTrait((string)$node->namespacedName);
111114
$this->processUnit($node);
112115
return;
@@ -151,7 +154,7 @@ private function processUnit($node) {
151154

152155
$docComment = $node->getDocComment();
153156
if ($docComment !== NULL) {
154-
$block = $this->dockblocParser->parse($docComment, $this->aliasMap);
157+
$block = $this->docBlockParser->parse($docComment, $this->aliasMap);
155158
$this->unit->setDocBlock($block);
156159
}
157160

@@ -234,7 +237,7 @@ private function processMethod(NodeType\ClassMethod $node) {
234237
$method->setVisibility($visibility);
235238
$docComment = $node->getDocComment();
236239
if ($docComment !== NULL) {
237-
$block = $this->dockblocParser->parse($docComment, $this->aliasMap);
240+
$block = $this->docBlockParser->parse($docComment, $this->aliasMap);
238241
$method->setDocBlock($block);
239242
}
240243
$this->processMethodParams($method, $node->params);
@@ -280,7 +283,7 @@ private function processClassConstant(NodeType\ClassConst $node) {
280283
$const->setValue($constNode->getAttribute('originalValue'));
281284
$docComment = $node->getDocComment();
282285
if ($docComment !== NULL) {
283-
$block = $this->dockblocParser->parse($docComment, $this->aliasMap);
286+
$block = $this->docBlockParser->parse($docComment, $this->aliasMap);
284287
$const->setDocBlock($block);
285288
}
286289
}
@@ -299,7 +302,7 @@ private function processProperty(NodeType\Property $node) {
299302
$member->setVisibility($visibility);
300303
$docComment = $node->getDocComment();
301304
if ($docComment !== NULL) {
302-
$block = $this->dockblocParser->parse($docComment, $this->aliasMap);
305+
$block = $this->docBlockParser->parse($docComment, $this->aliasMap);
303306
$member->setDocBlock($block);
304307
}
305308
$member->setLine($node->getLine());

src/docblock/parser/GenericParser.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class GenericParser {
4545
protected $payload;
4646

4747
private $types = array(
48-
'', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int', 'float', 'string', 'boolean', 'resource'
48+
'', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int', 'float', 'string', 'boolean', 'resource', 'static'
4949
);
5050

5151
public function __construct(Factory $factory, $name) {
@@ -76,6 +76,10 @@ protected function buildObject($classname, array $buffer) {
7676
}
7777

7878
protected function lookupType($type) {
79+
if ($type === 'self') {
80+
return $this->aliasMap['::unit'];
81+
}
82+
7983
// Do not mess with scalar and fixed types
8084
if (in_array($type, $this->types)) {
8185
return $type;
@@ -102,4 +106,4 @@ protected function lookupType($type) {
102106

103107
}
104108

105-
}
109+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
class StaticAndSelf {
3+
4+
/**
5+
* @var self
6+
*/
7+
private $x;
8+
9+
/**
10+
* @return static
11+
*/
12+
public function foo() {
13+
return new static();
14+
}
15+
}

tests/data/static/test.xml

Lines changed: 15 additions & 0 deletions
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-issue172" 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)