fix for bug #1672 in phpDocumentor2

When dealing with inline @see or @link tags and having a relative method or property type
like LinkDescriptor::setLink() and a namespace alias like phpDocumentor\Descriptor\Tag\LinkDescriptor
Collection::expand() was checking if LinkDescriptor::setLink() could be found in the namespace aliases.
This would never be true.
In this fix, the method or type (for instance ::setLink()) part is removed for comparison
and later re-added when generating the full name
This commit is contained in:
Marieke Bednarczyk
2015-12-13 15:52:57 +01:00
parent d68dbdc53d
commit 708c2c9253
2 changed files with 66 additions and 1 deletions
@@ -161,7 +161,8 @@ class Collection extends \ArrayObject
$namespace_aliases = $this->context->getNamespaceAliases(); $namespace_aliases = $this->context->getNamespaceAliases();
// if the first segment is not an alias; prepend namespace name and // if the first segment is not an alias; prepend namespace name and
// return // return
if (!isset($namespace_aliases[$type_parts[0]])) { if (!isset($namespace_aliases[$type_parts[0]]) &&
!isset($namespace_aliases[strstr($type_parts[0], '::', true)])) {
$namespace = $this->context->getNamespace(); $namespace = $this->context->getNamespace();
if ('' !== $namespace) { if ('' !== $namespace) {
$namespace .= self::OPERATOR_NAMESPACE; $namespace .= self::OPERATOR_NAMESPACE;
@@ -169,6 +170,12 @@ class Collection extends \ArrayObject
return self::OPERATOR_NAMESPACE . $namespace . $type; return self::OPERATOR_NAMESPACE . $namespace . $type;
} }
if (strpos($type_parts[0], '::')) {
$type_parts[] = strstr($type_parts[0], '::');
$type_parts[0] = $namespace_aliases[strstr($type_parts[0], '::', true)];
return implode('', $type_parts);
}
$type_parts[0] = $namespace_aliases[$type_parts[0]]; $type_parts[0] = $namespace_aliases[$type_parts[0]];
$type = implode(self::OPERATOR_NAMESPACE, $type_parts); $type = implode(self::OPERATOR_NAMESPACE, $type_parts);
} }
@@ -123,6 +123,26 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
$this->assertSame($expected, $collection->getArrayCopy()); $this->assertSame($expected, $collection->getArrayCopy());
} }
/**
* @param string $fixture
* @param array $expected
*
* @dataProvider provideTypesToExpandWithPropertyOrMethod
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::add
*
* @return void
*/
public function testAddMethodsAndProperties($fixture, $expected)
{
$collection = new Collection(
array(),
new Context(null, array('LinkDescriptor' => '\phpDocumentor\LinkDescriptor'))
);
$collection->add($fixture);
$this->assertSame($expected, $collection->getArrayCopy());
}
/** /**
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::add * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::add
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
@@ -177,6 +197,14 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
'DocBlock[]|int[]', 'DocBlock[]|int[]',
array($namespace.'DocBlock[]', 'int[]') array($namespace.'DocBlock[]', 'int[]')
), ),
array(
'LinkDescriptor::setLink()',
array($namespace.'LinkDescriptor::setLink()')
),
array(
'Alias\LinkDescriptor::setLink()',
array('\My\Space\Aliasing\LinkDescriptor::setLink()')
),
); );
} }
@@ -192,4 +220,34 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
{ {
return $this->provideTypesToExpand($method, '\\'); return $this->provideTypesToExpand($method, '\\');
} }
/**
* Returns the method and property types and their expected values to test
* the retrieval of types.
*
* @param string $method Name of the method consuming this data provider.
*
* @return string[]
*/
public function provideTypesToExpandWithPropertyOrMethod($method)
{
return array(
array(
'LinkDescriptor::setLink()',
array('\phpDocumentor\LinkDescriptor::setLink()')
),
array(
'phpDocumentor\LinkDescriptor::setLink()',
array('\phpDocumentor\LinkDescriptor::setLink()')
),
array(
'LinkDescriptor::$link',
array('\phpDocumentor\LinkDescriptor::$link')
),
array(
'phpDocumentor\LinkDescriptor::$link',
array('\phpDocumentor\LinkDescriptor::$link')
),
);
}
} }