Skip to content

Commit

Permalink
Refs #210: Avoid duplicate references due to doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Pichler committed Jan 6, 2017
1 parent ee81e96 commit e81411f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/php/PDepend/Source/AST/AbstractASTCallable.php
Expand Up @@ -261,7 +261,9 @@ public function getEndLine()
public function getDependencies()
{
return new ASTClassOrInterfaceReferenceIterator(
$this->findChildrenOfType('PDepend\\Source\\AST\\ASTClassOrInterfaceReference')
$this->findChildrenOfType(
'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'
)
);
}

Expand Down
29 changes: 21 additions & 8 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Expand Up @@ -5755,8 +5755,10 @@ private function parseCommentWithOptionalInlineClassOrInterfaceReference()

$comment = $this->builder->buildAstComment($token->image);
if (preg_match(self::REGEXP_INLINE_TYPE, $token->image, $match)) {
$image = $this->useSymbolTable->lookup($match[1]) ?: $match[1];

$comment->addChild(
$this->builder->buildAstClassOrInterfaceReference($match[1])
$this->builder->buildAstClassOrInterfaceReference($image)
);
}

Expand Down Expand Up @@ -6626,7 +6628,7 @@ private function parseThrowsAnnotations($comment)
$throws = array();
if (preg_match_all(self::REGEXP_THROWS_TYPE, $comment, $matches) > 0) {
foreach ($matches[1] as $match) {
$throws[] = $match;
$throws[] = $this->useSymbolTable->lookup($match) ?: $match;
}
}
return $throws;
Expand All @@ -6642,12 +6644,18 @@ private function parseThrowsAnnotations($comment)
*/
private function parseReturnAnnotation($comment)
{
if (preg_match(self::REGEXP_RETURN_TYPE, $comment, $match) > 0) {
foreach (explode('|', end($match)) as $type) {
if (Type::isScalarType($type) === false) {
return $type;
}
if (0 === preg_match(self::REGEXP_RETURN_TYPE, $comment, $match)) {
return null;
}

foreach (explode('|', end($match)) as $image) {
$image = $this->useSymbolTable->lookup($image) ?: $image;

if (Type::isScalarType($image)) {
continue;
}

return $image;
}
return null;
}
Expand All @@ -6662,7 +6670,12 @@ private function parseReturnAnnotation($comment)
private function parseVarAnnotation($comment)
{
if (preg_match(self::REGEXP_VAR_TYPE, $comment, $match) > 0) {
return array_map('trim', explode('|', end($match)));
return array_map(
function ($image) {
return $this->useSymbolTable->lookup($image) ?: $image;
},
array_map('trim', explode('|', end($match)))
);
}
return array();
}
Expand Down
Expand Up @@ -111,6 +111,20 @@ public function testListKeywordAsFunctionNameThrowsException()
$this->parseCodeResourceForTest();
}

/**
* @return \PDepend\Source\AST\AbstractASTClassOrInterface[]
*/
public function testParserResolvesDependenciesInDocComments()
{
$namespaces = $this->parseCodeResourceForTest();
$classes = $namespaces[0]->getClasses();
$dependencies = $classes[0]->findChildrenOfType('PDepend\\Source\\AST\\ASTClassOrInterfaceReference');

$this->assertSame(1, count($dependencies));

return $dependencies;
}

/**
* @param \PDepend\Source\Tokenizer\Tokenizer $tokenizer
* @param \PDepend\Source\Builder\Builder $builder
Expand Down
@@ -0,0 +1,22 @@
<?php
use A\Dependent\Property;
use A\Dependent\Exception;
use A\Dependent\ReturnValue;

class DependenciesInDocComments
{
/**
* @var Property
*/
public $foo;

/**
* @param Dependency $x
* @throws Exception
* @return ReturnValue
*/
public function foo($x)
{

}
}

0 comments on commit e81411f

Please sign in to comment.