diff --git a/src/phpDocumentor/Reflection/DocBlock.php b/src/phpDocumentor/Reflection/DocBlock.php index dff4008..921e829 100644 --- a/src/phpDocumentor/Reflection/DocBlock.php +++ b/src/phpDocumentor/Reflection/DocBlock.php @@ -66,7 +66,7 @@ class DocBlock implements \Reflector public function __construct( $docblock, $namespace = '\\', - $namespace_aliases = array() + array $namespace_aliases = array() ) { if (is_object($docblock)) { if (!method_exists($docblock, 'getDocComment')) { @@ -304,85 +304,6 @@ class DocBlock implements \Reflector return false; } - /** - * Tries to expand a type to it's full namespaced equivalent (FQCN). - * - * This method will take the given type and examine the current namespace - * and namespace aliases to see whether it should expand it into a FQCN - * as defined by the rules in PHP. - * - * @param string $type Type to expand into full namespaced - * equivalent. - * @param string[] $ignore_keywords Whether to ignore given keywords, when - * null it will use the default keywords: - * 'string', 'int', 'integer', 'bool', 'boolean', 'float', 'double', - * 'object', 'mixed', 'array', 'resource', 'void', 'null', 'callback', - * 'false', 'true', 'self', '$this', 'callable'. - * Default value for this parameter is null. - * - * @return string - */ - public function expandType($type, $ignore_keywords = null) - { - if ($type === null) { - return null; - } - - if ($ignore_keywords === null) { - $ignore_keywords = array( - 'string', 'int', 'integer', 'bool', 'boolean', 'float', - 'double', 'object', 'mixed', 'array', 'resource', 'void', - 'null', 'callback', 'false', 'true', 'self', '$this', 'callable' - ); - } - - $namespace = '\\'; - if ($this->namespace != 'default' && $this->namespace != 'global') { - $namespace = rtrim($this->namespace, '\\') . '\\'; - } - - $type = explode('|', $type); - foreach ($type as &$item) { - $item = trim($item); - - // add support for array notation - $is_array = false; - if (substr($item, -2) == '[]') { - $item = substr($item, 0, -2); - $is_array = true; - } - - if ((substr($item, 0, 1) != '\\') - && (!in_array(strtolower($item), $ignore_keywords)) - ) { - $type_parts = explode('\\', $item); - - // if the first segment is an alias; replace with full name - if (isset($this->namespace_aliases[$type_parts[0]])) { - $type_parts[0] = $this->namespace_aliases[$type_parts[0]]; - $item = implode('\\', $type_parts); - } else { - // otherwise prepend the current namespace - $item = $namespace . $item; - } - } - - // full paths always start with a slash - if (isset($item[0]) && ($item[0] !== '\\') - && (!in_array(strtolower($item), $ignore_keywords)) - ) { - $item = '\\' . $item; - } - - // re-add the array notation markers - if ($is_array) { - $item .= '[]'; - } - } - - return implode('|', $type); - } - /** * Builds a string representation of this object. * diff --git a/src/phpDocumentor/Reflection/DocBlock/LongDescription.php b/src/phpDocumentor/Reflection/DocBlock/LongDescription.php index 4ec7b11..3c26035 100644 --- a/src/phpDocumentor/Reflection/DocBlock/LongDescription.php +++ b/src/phpDocumentor/Reflection/DocBlock/LongDescription.php @@ -120,6 +120,9 @@ class LongDescription implements \Reflector * * @todo this should become a more intelligent piece of code where the * configuration contains a setting what format long descriptions are. + * + * @codeCoverageIgnore Will be removed soon, in favor of adapters at + * PhpDocumentor itself that will process text in various formats. * * @return string */ diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/CoversTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/CoversTagTest.php index 06dac8e..ff257aa 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/CoversTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/CoversTagTest.php @@ -28,7 +28,6 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase * * @param string $type * @param string $content - * @param string $exName * @param string $exContent * @param string $exReference * @@ -59,7 +58,7 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase */ public function provideDataForConstuctor() { - // $type, $content, $exName, $exContent, $exDescription, $exReference + // $type, $content, $exContent, $exDescription, $exReference return array( array( 'covers', diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/LinkTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/LinkTagTest.php index 571c671..0578e1a 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/LinkTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/LinkTagTest.php @@ -28,7 +28,6 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase * * @param string $type * @param string $content - * @param string $exName * @param string $exContent * @param string $exDescription * @param string $exLink @@ -61,7 +60,7 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase */ public function provideDataForConstuctor() { - // $type, $content, $exName, $exContent, $exDescription, $exLink + // $type, $content, $exContent, $exDescription, $exLink return array( array( 'link', diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/MethodTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/MethodTagTest.php index 4bef7dc..56fb162 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/MethodTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/MethodTagTest.php @@ -23,19 +23,20 @@ namespace phpDocumentor\Reflection\DocBlock\Tag; class MethodTagTest extends \PHPUnit_Framework_TestCase { /** - * @param string $signature The signature to test + * @param string $signature The signature to test. * @param bool $valid Whether the given signature is expected to * be valid. * @param string $expected_name The method name that is expected from this - * signature + * signature. * @param string $expected_return The return type that is expected from this - * signature - * @param bool $has_params whether this signature features parameters. + * signature. + * @param bool $paramCount Number of parameters in the signature. * @param string $description The short description mentioned in the * signature. * * @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::__construct * @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getMethodName + * @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getArguments * * @dataProvider getTestSignatures * @@ -46,7 +47,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase $valid, $expected_name, $expected_return, - $has_params, + $paramCount, $description ) { ob_start(); @@ -66,11 +67,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected_name, $tag->getMethodName()); $this->assertEquals($expected_return, $tag->getType()); $this->assertEquals($description, $tag->getDescription()); - $this->assertSame( - $has_params, - (bool)(count($tag->getArguments()) > 0), - 'Number of found arguments should exceed 0' - ); + $this->assertCount($paramCount, $tag->getArguments()); } public function getTestSignatures() @@ -78,55 +75,55 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase return array( array( 'foo', - false, 'foo', '', false, '' + false, 'foo', '', 0, '' ), array( 'foo()', - true, 'foo', 'void', false, '' + true, 'foo', 'void', 0, '' ), array( 'foo() description', - true, 'foo', 'void', false, 'description' + true, 'foo', 'void', 0, 'description' ), array( 'int foo()', - true, 'foo', 'int', false, '' + true, 'foo', 'int', 0, '' ), array( 'int foo() description', - true, 'foo', 'int', false, 'description' + true, 'foo', 'int', 0, 'description' ), array( 'int foo($a, $b)', - true, 'foo', 'int', true, '' + true, 'foo', 'int', 2, '' ), array( 'int foo() foo(int $a, int $b)', - true, 'foo', 'int', true, '' + true, 'foo', 'int', 2, '' ), array( 'int foo(int $a, int $b)', - true, 'foo', 'int', true, '' + true, 'foo', 'int', 2, '' ), array( 'null|int foo(int $a, int $b)', - true, 'foo', 'null|int', true, '' + true, 'foo', 'null|int', 2, '' ), array( 'int foo(null|int $a, int $b)', - true, 'foo', 'int', true, '' + true, 'foo', 'int', 2, '' ), array( '\Exception foo() foo(Exception $a, Exception $b)', - true, 'foo', '\Exception', true, '' + true, 'foo', '\Exception', 2, '' ), array( 'int foo() foo(Exception $a, Exception $b) description', - true, 'foo', 'int', true, 'description' + true, 'foo', 'int', 2, 'description' ), array( 'int foo() foo(\Exception $a, \Exception $b) description', - true, 'foo', 'int', true, 'description' + true, 'foo', 'int', 2, 'description' ), ); } diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/ParamTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/ParamTagTest.php index b3c2503..8db7e46 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/ParamTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/ParamTagTest.php @@ -29,6 +29,7 @@ class ParamTagTest extends \PHPUnit_Framework_TestCase * @param string $type * @param string $content * @param string $extractedType + * @param string $extractedTypes * @param string $extractedVarName * @param string $extractedDescription * diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/ReturnTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/ReturnTagTest.php index 7fc5855..a84e333 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/ReturnTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/ReturnTagTest.php @@ -20,7 +20,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tag; * @license http://www.opensource.org/licenses/mit-license.php MIT * @link http://phpdoc.org */ -class ReturnTagTest extends ParamTagTest +class ReturnTagTest extends \PHPUnit_Framework_TestCase { /** * Test that the \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag can @@ -28,6 +28,7 @@ class ReturnTagTest extends ParamTagTest * * @param string $content * @param string $extractedType + * @param string $extractedTypes * @param string $extractedDescription * * @covers \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag::__construct diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/SeeTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/SeeTagTest.php index f789a65..c61fc4b 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/SeeTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/SeeTagTest.php @@ -28,7 +28,6 @@ class SeeTagTest extends \PHPUnit_Framework_TestCase * * @param string $type * @param string $content - * @param string $exName * @param string $exContent * @param string $exReference * diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/UsesTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/UsesTagTest.php index c059818..5f79d91 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/UsesTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/UsesTagTest.php @@ -28,7 +28,6 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase * * @param string $type * @param string $content - * @param string $exName * @param string $exContent * @param string $exReference * @@ -59,7 +58,7 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase */ public function provideDataForConstuctor() { - // $type, $content, $exName, $exContent, $exDescription, $exReference + // $type, $content, $exContent, $exDescription, $exReference return array( array( 'uses', diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/VarTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/VarTagTest.php index 603ef96..1847215 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Tag/VarTagTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/VarTagTest.php @@ -59,7 +59,7 @@ class VarTagTest extends \PHPUnit_Framework_TestCase */ public function provideDataForConstuctor() { - // $type, $content + // $type, $content, $exType, $exVariable, $exDescription return array( array( 'var', diff --git a/tests/phpDocumentor/Reflection/DocBlock/TagTest.php b/tests/phpDocumentor/Reflection/DocBlock/TagTest.php new file mode 100644 index 0000000..f50c6c7 --- /dev/null +++ b/tests/phpDocumentor/Reflection/DocBlock/TagTest.php @@ -0,0 +1,86 @@ + + * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ + +namespace phpDocumentor\Reflection\DocBlock; + +/** + * Test class for \phpDocumentor\Reflection\DocBlock\Tag\VarTag + * + * @author Daniel O'Connor + * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ +class TagTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidTagLine() + { + Tag::createInstance('Invalid tag line'); + } + /** + * Test that the \phpDocumentor\Reflection\DocBlock\Tag\VarTag can + * understand the @var doc block. + * + * @param string $type + * @param string $content + * @param string $exDescription + * + * @covers \phpDocumentor\Reflection\DocBlock\Tag::__construct + * @covers \phpDocumentor\Reflection\DocBlock\Tag::getDescription + * @covers \phpDocumentor\Reflection\DocBlock\Tag::getContent + * @dataProvider provideDataForConstuctor + * + * @return void + */ + public function testConstructorParesInputsIntoCorrectFields( + $type, + $content, + $exDescription + ) { + $tag = new Tag($type, $content); + + $this->assertEquals($type, $tag->getName()); + $this->assertEquals($content, $tag->getContent()); + $this->assertEquals($exDescription, $tag->getDescription()); + } + + /** + * Data provider for testConstructorParesInputsIntoCorrectFields + * + * @return array + */ + public function provideDataForConstuctor() + { + // $type, $content, $exDescription + return array( + array( + 'unknown', + 'some content', + 'some content', + ), + array( + 'unknown', + '', + '', + ), + array( + '', + 'unknown', + 'unknown', + ) + ); + } +} diff --git a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php index 2c13337..ffce80d 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php @@ -39,6 +39,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->assertCount(0, $collection->getNamespaceAliases()); } + /** + * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::__construct + * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::setNamespace + * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::getNamespace + * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::getNamespaceAliases + * + * @return void + */ + public function testGlobalIgnore() + { + $collection = new Collection(); + $collection->setNamespace('global'); + $this->assertCount(0, $collection); + $this->assertEquals('\\', $collection->getNamespace()); + $this->assertCount(0, $collection->getNamespaceAliases()); + } + /** * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::__construct * diff --git a/tests/phpDocumentor/Reflection/DocBlockTest.php b/tests/phpDocumentor/Reflection/DocBlockTest.php index 3015a25..b6179a2 100644 --- a/tests/phpDocumentor/Reflection/DocBlockTest.php +++ b/tests/phpDocumentor/Reflection/DocBlockTest.php @@ -34,7 +34,11 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase * @return void */ DOCBLOCK; - $object = new DocBlock($fixture); + $object = new DocBlock( + $fixture, + '\MyNamespace', + array('PHPDoc' => '\phpDocumentor') + ); $this->assertEquals( 'This is a short description.', $object->getShortDescription() @@ -43,10 +47,55 @@ DOCBLOCK; 'This is a long description.', $object->getLongDescription()->getContents() ); - $this->assertEquals(2, count($object->getTags())); + $this->assertCount(2, $object->getTags()); $this->assertTrue($object->hasTag('see')); $this->assertTrue($object->hasTag('return')); $this->assertFalse($object->hasTag('category')); + + $this->assertSame('\MyNamespace', $object->getNamespace()); + $this->assertSame( + array('PHPDoc' => '\phpDocumentor'), + $object->getNamespaceAliases() + ); + } + + /** + * @covers \phpDocumentor\Reflection\DocBlock::splitDocBlock + * + * @return void + */ + public function testConstructWithTagsOnly() + { + $fixture = <<assertEquals('', $object->getShortDescription()); + $this->assertEquals('', $object->getLongDescription()->getContents()); + $this->assertCount(2, $object->getTags()); + $this->assertTrue($object->hasTag('see')); + $this->assertTrue($object->hasTag('return')); + $this->assertFalse($object->hasTag('category')); + } + + /** + * @covers \phpDocumentor\Reflection\DocBlock::cleanInput + * + * @return void + */ + public function testConstructOneLiner() + { + $fixture = '/** Short description and nothing more. */'; + $object = new DocBlock($fixture); + $this->assertEquals( + 'Short description and nothing more.', + $object->getShortDescription() + ); + $this->assertEquals('', $object->getLongDescription()->getContents()); + $this->assertCount(0, $object->getTags()); } /** @@ -60,7 +109,7 @@ DOCBLOCK; $object->getShortDescription() ); $this->assertEquals('', $object->getLongDescription()->getContents()); - $this->assertEquals(4, count($object->getTags())); + $this->assertCount(4, $object->getTags()); $this->assertTrue($object->hasTag('author')); $this->assertTrue($object->hasTag('copyright')); $this->assertTrue($object->hasTag('license')); @@ -118,7 +167,7 @@ DOCBLOCK; $object->getLongDescription()->getContents() ); $tags = $object->getTags(); - $this->assertEquals(2, count($tags)); + $this->assertCount(2, $tags); $this->assertTrue($object->hasTag('method')); $this->assertTrue($object->hasTag('Method')); $this->assertInstanceOf( @@ -136,88 +185,66 @@ DOCBLOCK; } /** - * Tests whether a type is expanded with the given namespace and that a - * keyword is not expanded. - * - * @covers \phpDocumentor\Reflection\DocBlock::expandType() - * - * @return void + * @depends testConstructFromReflector + * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName */ - public function testExpandTypeUsingNamespace() + public function testGetTagsByNameZeroAndOneMatch() { - $docblock = new DocBlock('', '\My\Namespace'); - $this->assertEquals('\My\Namespace\Mine', $docblock->expandType('Mine')); + $object = new DocBlock(new \ReflectionClass($this)); + $this->assertEmpty($object->getTagsByName('category')); + $this->assertCount(1, $object->getTagsByName('author')); } /** - * Tests whether a type is expanded when no namespace is given. - * - * @covers \phpDocumentor\Reflection\DocBlock::expandType() - * - * @return void + * @depends testConstructWithTagsOnly + * @covers \phpDocumentor\Reflection\DocBlock::parseTags */ - public function testExpandTypeWithoutNamespace() + public function testParseMultilineTag() { - $docblock = new DocBlock(''); - $this->assertEquals('\Mine', $docblock->expandType('Mine')); + $fixture = <<assertCount(1, $object->getTags()); } /** - * Tests whether a type is expanded with the given namespace when an alias - * is provided. - * - * @covers \phpDocumentor\Reflection\DocBlock::expandType() - * - * @return void + * @depends testConstructWithTagsOnly + * @covers \phpDocumentor\Reflection\DocBlock::parseTags */ - public function testExpandTypeUsingNamespaceAlias() + public function testParseMultilineTagWithLineBreaks() { - $docblock = new DocBlock( - '', - '\My\Namespace', - array('Alias' => '\My\Namespace\Alias') - ); - - // first try a normal resolution without alias - $this->assertEquals( - '\My\Namespace\Al', - $docblock->expandType('Al') - ); - - // try to use the alias - $this->assertEquals( - '\My\Namespace\Alias\Al', - $docblock->expandType('Alias\Al') - ); + $fixture = <<assertCount(1, $object->getTags()); } /** - * Tests whether the keywords that should not be converted are not converted. - * - * @param string $keyword The keyword that is to be tested; this is provided - * by the dataprovider. - * - * @covers \phpDocumentor\Reflection\DocBlock::expandType() - * - * @dataProvider getNonExpandableKeywordsForExpandType - * - * @return void + * @depends testConstructWithTagsOnly + * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName */ - public function testThatExpandTypeDoesNotExpandAllKeywords($keyword) + public function testGetTagsByNameMultipleMatch() { - $docblock = new DocBlock('', '\My\Namespace'); - $this->assertSame($keyword, $docblock->expandType($keyword)); - } - - public function getNonExpandableKeywordsForExpandType() - { - return array( - array(null), - array('string'), array('int'), array('integer'), array('bool'), - array('boolean'), array('float'), array('double'), array('object'), - array('mixed'), array('array'), array('resource'), array('void'), - array('null'), array('callback'), array('false'), array('true'), - array('self'), array('$this'), array('callable') - ); + $fixture = <<assertEmpty($object->getTagsByName('category')); + $this->assertCount(1, $object->getTagsByName('return')); + $this->assertCount(2, $object->getTagsByName('param')); } }