From 708c2c925382802dd587591f77641ab6342b86f2 Mon Sep 17 00:00:00 2001 From: Marieke Bednarczyk Date: Sun, 13 Dec 2015 15:52:57 +0100 Subject: [PATCH] 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 --- .../Reflection/DocBlock/Type/Collection.php | 9 ++- .../DocBlock/Type/CollectionTest.php | 58 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php b/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php index 90ead3f..327819c 100644 --- a/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php +++ b/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php @@ -161,7 +161,8 @@ class Collection extends \ArrayObject $namespace_aliases = $this->context->getNamespaceAliases(); // if the first segment is not an alias; prepend namespace name and // 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(); if ('' !== $namespace) { $namespace .= self::OPERATOR_NAMESPACE; @@ -169,6 +170,12 @@ class Collection extends \ArrayObject 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 = implode(self::OPERATOR_NAMESPACE, $type_parts); } diff --git a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php index 78c7306..383a6c0 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php @@ -123,6 +123,26 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $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 * @expectedException InvalidArgumentException @@ -177,6 +197,14 @@ class CollectionTest extends \PHPUnit_Framework_TestCase '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, '\\'); } + + /** + * 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') + ), + ); + } }