Skip to content

Commit 6b707f7

Browse files
committed
Change behaviour of @param parsing
In issue report phpDocumentor/phpDocumentor#620 @bobef reported that when he used just a Type as content of the @param that it would be recognized as description instead of the Type. According to the unit tests this is correct behaviour but after reviewing the pattern of the output his version is more consistent. As such I have altered the behaviour to act as following: If only one word is found after an @param (word means white-space bounded series of characters) then interpret that as the type and not description. During this item several issues in unit tests were fixed and a new 'Type' Collection was introduced that is capable of expanding types based on a given namespace and series of aliases. This should be re-used in phpDocumentor's Transformer as a duplication exists there with the expanding of the Types. Please note: the suggested format by @bobef is not valid according to the PHPDoc Standard but is provided for convenience.
1 parent 5236694 commit 6b707f7

File tree

17 files changed

+541
-116
lines changed

17 files changed

+541
-116
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea
1+
.idea
2+
vendor

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"dflydev/markdown": "1.0.*"
1111
},
1212
"autoload": {
13-
"psr-0": {"phpDocumentor\\Reflection": "src/"}
13+
"psr-0": {"phpDocumentor": ["src/"]}
1414
}
1515
}

composer.lock

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

3-
<phpunit colors="true" strict="true">
3+
<phpunit colors="true" strict="true" bootstrap="vendor/autoload.php">
44
<testsuites>
55
<testsuite name="phpDocumentor\Reflection\DocBlock">
66
<directory>./tests/</directory>

src/phpDocumentor/Reflection/DocBlock.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function expandType($type, $ignore_keywords = null)
327327
);
328328
}
329329

330-
$namespace = '';
330+
$namespace = '\\';
331331
if ($this->namespace != 'default' && $this->namespace != 'global') {
332332
$namespace = rtrim($this->namespace, '\\') . '\\';
333333
}
@@ -398,4 +398,16 @@ public function __toString()
398398
return 'Not yet implemented';
399399
}
400400

401+
/**
402+
* @return string
403+
*/
404+
public function getNamespace()
405+
{
406+
return $this->namespace;
407+
}
408+
409+
public function getNamespaceAliases()
410+
{
411+
return $this->namespace_aliases;
412+
}
401413
}

src/phpDocumentor/Reflection/DocBlock/Tag/ParamTag.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class ParamTag extends Tag
2424
{
2525
/** @var string */
26-
protected $type = null;
26+
protected $type = '';
2727

2828
/**
2929
* @var string
@@ -33,7 +33,7 @@ class ParamTag extends Tag
3333
/**
3434
* Parses a tag and populates the member variables.
3535
*
36-
* @param string $type Tag identifier for this tag (should be 'return')
36+
* @param string $type Tag identifier for this tag (should be 'param')
3737
* @param string $content Contents for this tag.
3838
*/
3939
public function __construct($type, $content)
@@ -42,13 +42,13 @@ public function __construct($type, $content)
4242
$this->content = $content;
4343
$content = preg_split('/\s+/u', $content);
4444

45-
// if there is only 1, it is either a piece of content or a variable name
46-
if (count($content) > 1) {
45+
// if the first item that is encountered is not a variable; it is a type
46+
if (isset($content[0]) && (strlen($content[0]) > 0) && ($content[0][0] !== '$')) {
4747
$this->type = array_shift($content);
4848
}
4949

5050
// if the next item starts with a $ it must be the variable name
51-
if ((strlen($content[0]) > 0) && ($content[0][0] == '$')) {
51+
if (isset($content[0]) && (strlen($content[0]) > 0) && ($content[0][0] == '$')) {
5252
$this->variableName = array_shift($content);
5353
}
5454

@@ -62,14 +62,13 @@ public function __construct($type, $content)
6262
*/
6363
public function getTypes()
6464
{
65-
$types = explode('|', $this->type);
66-
foreach ($types as &$type) {
67-
$type = !empty($type) && $this->docblock
68-
? $this->docblock->expandType($type)
69-
: trim($type);
70-
}
65+
$types = new \phpDocumentor\Reflection\DocBlock\Type\Collection(
66+
array($this->type),
67+
$this->docblock ? $this->docblock->getNamespace() : null,
68+
$this->docblock ? $this->docblock->getNamespaceAliases() : array()
69+
);
7170

72-
return array_values(array_unique($types, SORT_REGULAR));
71+
return $types->getArrayCopy();
7372
}
7473

7574
/**
@@ -79,7 +78,7 @@ public function getTypes()
7978
*/
8079
public function getType()
8180
{
82-
return $this->docblock->expandType($this->type);
81+
return implode('|', $this->getTypes());
8382
}
8483

8584
/**

0 commit comments

Comments
 (0)