From 2e6cecb9ef7a47b2c58b97f20c3b60be3b5f61c4 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Tue, 1 Sep 2020 17:52:33 +0200 Subject: [PATCH 01/10] Param: do not resolve types if it's not possible -> https://travis-ci.org/github/JetBrains/phpstorm-stubs/builds/723069982 -> https://github.com/JetBrains/phpstorm-stubs/pull/892 --- src/DocBlock/Tags/Param.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index 392b4f9..dea81a7 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -75,7 +75,7 @@ final class Param extends TagWithType implements Factory\StaticMethod $isReference = false; // if the first item that is encountered is not a variable; it is a type - if ($firstPart && $firstPart[0] !== '$') { + if ($firstPart && !self::strStartsWithVariable($firstPart)) { $type = $typeResolver->resolve($firstPart, $context); } else { // first part is not a type; we should prepend it to the parts array for further processing @@ -83,18 +83,7 @@ final class Param extends TagWithType implements Factory\StaticMethod } // if the next item starts with a $ or ...$ or &$ or &...$ it must be the variable name - if (isset($parts[0]) - && - ( - strpos($parts[0], '$') === 0 - || - strpos($parts[0], '...$') === 0 - || - strpos($parts[0], '&$') === 0 - || - strpos($parts[0], '&...$') === 0 - ) - ) { + if (isset($parts[0]) && self::strStartsWithVariable($parts[0])) { $variableName = array_shift($parts); array_shift($parts); @@ -155,4 +144,20 @@ final class Param extends TagWithType implements Factory\StaticMethod . ($this->variableName !== null ? '$' . $this->variableName : '') . ($this->description ? ' ' . $this->description : ''); } + + /** + * @param string $str + * + * @return bool + */ + private static function strStartsWithVariable(string $str): bool + { + return strpos($str, '$') === 0 + || + strpos($str, '...$') === 0 + || + strpos($str, '&$') === 0 + || + strpos($str, '&...$') === 0; + } } From acf538a461688a8a56ffef2cabb01f8df674f7d6 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Tue, 1 Sep 2020 19:42:29 +0200 Subject: [PATCH 02/10] Param: do not resolve types if it's not possible (fix code style) --- src/DocBlock/Tags/Param.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index dea81a7..281756f 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -145,12 +145,7 @@ final class Param extends TagWithType implements Factory\StaticMethod . ($this->description ? ' ' . $this->description : ''); } - /** - * @param string $str - * - * @return bool - */ - private static function strStartsWithVariable(string $str): bool + private static function strStartsWithVariable(string $str) : bool { return strpos($str, '$') === 0 || From be6ed8b0d88b41bf785f4fcfb71181b5efa64d29 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Wed, 2 Sep 2020 22:23:55 +0200 Subject: [PATCH 03/10] add more unit tests and fixed the output --- src/DocBlock/Tags/Param.php | 8 +- src/DocBlock/Tags/Property.php | 8 +- src/DocBlock/Tags/PropertyRead.php | 6 +- src/DocBlock/Tags/PropertyWrite.php | 6 +- src/DocBlock/Tags/Var_.php | 8 +- tests/unit/DocBlock/Tags/ParamTest.php | 125 ++++++++++++++++++ tests/unit/DocBlock/Tags/PropertyReadTest.php | 61 +++++++++ tests/unit/DocBlock/Tags/PropertyTest.php | 61 +++++++++ .../unit/DocBlock/Tags/PropertyWriteTest.php | 61 +++++++++ tests/unit/DocBlock/Tags/VarTest.php | 61 +++++++++ 10 files changed, 392 insertions(+), 13 deletions(-) diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index 281756f..650506f 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -85,7 +85,9 @@ final class Param extends TagWithType implements Factory\StaticMethod // if the next item starts with a $ or ...$ or &$ or &...$ it must be the variable name if (isset($parts[0]) && self::strStartsWithVariable($parts[0])) { $variableName = array_shift($parts); - array_shift($parts); + if ($type) { + array_shift($parts); + } Assert::notNull($variableName); @@ -141,8 +143,8 @@ final class Param extends TagWithType implements Factory\StaticMethod return ($this->type ? $this->type . ' ' : '') . ($this->isReference() ? '&' : '') . ($this->isVariadic() ? '...' : '') - . ($this->variableName !== null ? '$' . $this->variableName : '') - . ($this->description ? ' ' . $this->description : ''); + . ($this->variableName ? '$' . $this->variableName : '') + . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); } private static function strStartsWithVariable(string $str) : bool diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index d8b83b6..e358d98 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -68,10 +68,12 @@ final class Property extends TagWithType implements Factory\StaticMethod array_unshift($parts, $firstPart); } - // if the next item starts with a $ or ...$ it must be the variable name + // if the next item starts with a $ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); - array_shift($parts); + if ($type) { + array_shift($parts); + } Assert::notNull($variableName); @@ -98,6 +100,6 @@ final class Property extends TagWithType implements Factory\StaticMethod { return ($this->type ? $this->type . ' ' : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ' ' . $this->description : ''); + . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); } } diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 087803c..6118d32 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -71,7 +71,9 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod // if the next item starts with a $ or ...$ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); - array_shift($parts); + if ($type) { + array_shift($parts); + } Assert::notNull($variableName); @@ -98,6 +100,6 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod { return ($this->type ? $this->type . ' ' : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ' ' . $this->description : ''); + . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); } } diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index 176b63d..d1b5a4e 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -71,7 +71,9 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod // if the next item starts with a $ or ...$ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); - array_shift($parts); + if ($type) { + array_shift($parts); + } Assert::notNull($variableName); @@ -98,6 +100,6 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod { return ($this->type ? $this->type . ' ' : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ' ' . $this->description : ''); + . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); } } diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index ac1c438..f939a25 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -72,7 +72,9 @@ final class Var_ extends TagWithType implements Factory\StaticMethod // if the next item starts with a $ or ...$ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); - array_shift($parts); + if ($type) { + array_shift($parts); + } Assert::notNull($variableName); @@ -98,7 +100,7 @@ final class Var_ extends TagWithType implements Factory\StaticMethod public function __toString() : string { return ($this->type ? $this->type . ' ' : '') - . (empty($this->variableName) ? '' : '$' . $this->variableName) - . ($this->description ? ' ' . $this->description : ''); + . ($this->variableName ? '$' . $this->variableName : '') + . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); } } diff --git a/tests/unit/DocBlock/Tags/ParamTest.php b/tests/unit/DocBlock/Tags/ParamTest.php index 246b57e..99a84da 100644 --- a/tests/unit/DocBlock/Tags/ParamTest.php +++ b/tests/unit/DocBlock/Tags/ParamTest.php @@ -16,8 +16,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -270,6 +273,128 @@ class ParamTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithReferenceWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Param::create( + '&$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('&$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertFalse($fixture->isVariadic()); + $this->assertTrue($fixture->isReference()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithVariadicReferenceWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Param::create( + '&...$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('&...$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertTrue($fixture->isVariadic()); + $this->assertTrue($fixture->isReference()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Param::create( + '$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertFalse($fixture->isVariadic()); + $this->assertFalse($fixture->isReference()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Param::create( + 'int My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int My Description', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: * @uses \phpDocumentor\Reflection\TypeResolver diff --git a/tests/unit/DocBlock/Tags/PropertyReadTest.php b/tests/unit/DocBlock/Tags/PropertyReadTest.php index 901b090..d2b2ee8 100644 --- a/tests/unit/DocBlock/Tags/PropertyReadTest.php +++ b/tests/unit/DocBlock/Tags/PropertyReadTest.php @@ -16,8 +16,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -169,6 +172,64 @@ class PropertyReadTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = PropertyRead::create( + '$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = PropertyRead::create( + 'int My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int My Description', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\PropertyRead:: * @uses \phpDocumentor\Reflection\TypeResolver diff --git a/tests/unit/DocBlock/Tags/PropertyTest.php b/tests/unit/DocBlock/Tags/PropertyTest.php index ec0e7fc..bf5258b 100644 --- a/tests/unit/DocBlock/Tags/PropertyTest.php +++ b/tests/unit/DocBlock/Tags/PropertyTest.php @@ -16,8 +16,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -164,6 +167,64 @@ class PropertyTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Property::create( + '$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Property::create( + 'int My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int My Description', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Property:: * @uses \phpDocumentor\Reflection\TypeResolver diff --git a/tests/unit/DocBlock/Tags/PropertyWriteTest.php b/tests/unit/DocBlock/Tags/PropertyWriteTest.php index 1c6124c..e25b360 100644 --- a/tests/unit/DocBlock/Tags/PropertyWriteTest.php +++ b/tests/unit/DocBlock/Tags/PropertyWriteTest.php @@ -16,8 +16,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -169,6 +172,64 @@ class PropertyWriteTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = PropertyWrite::create( + '$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = PropertyWrite::create( + 'int My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int My Description', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite:: * @uses \phpDocumentor\Reflection\TypeResolver diff --git a/tests/unit/DocBlock/Tags/VarTest.php b/tests/unit/DocBlock/Tags/VarTest.php index 409263e..4a8235a 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -16,8 +16,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -176,6 +179,64 @@ class VarTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithoutType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Var_::create( + '$myParameter My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('$myParameter My Description', (string) $fixture); + $this->assertSame('myParameter', $fixture->getVariableName()); + $this->assertNull($fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithType() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Var_::create( + 'int My Description', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int My Description', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('My Description', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_:: * @uses \phpDocumentor\Reflection\TypeResolver From bbe0f54877d0db64ba59ae560222ea2300acc45e Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Wed, 2 Sep 2020 22:55:00 +0200 Subject: [PATCH 04/10] add more unit tests and fixed the output v2 --- src/DocBlock/Tags/Param.php | 4 ++-- src/DocBlock/Tags/Property.php | 4 ++-- src/DocBlock/Tags/PropertyRead.php | 4 ++-- src/DocBlock/Tags/PropertyWrite.php | 4 ++-- src/DocBlock/Tags/Var_.php | 4 ++-- tests/unit/DocBlock/Tags/VarTest.php | 29 ++++++++++++++++++++++++++++ 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index 650506f..3f272d0 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -140,11 +140,11 @@ final class Param extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ' ' : '') + return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') . ($this->isReference() ? '&' : '') . ($this->isVariadic() ? '...' : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); + . (('' . $this->description) ? ' ' . $this->description : ''); } private static function strStartsWithVariable(string $str) : bool diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index e358d98..866b919 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -98,8 +98,8 @@ final class Property extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ' ' : '') + return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); + . (('' . $this->description) ? ' ' . $this->description : ''); } } diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 6118d32..9a10d6e 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -98,8 +98,8 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ' ' : '') + return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); + . (('' . $this->description) ? ' ' . $this->description : ''); } } diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index d1b5a4e..b4f6296 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -98,8 +98,8 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ' ' : '') + return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); + . (('' . $this->description) ? ' ' . $this->description : ''); } } diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index f939a25..1683d10 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -99,8 +99,8 @@ final class Var_ extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ' ' : '') + return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') . ($this->variableName ? '$' . $this->variableName : '') - . ($this->description ? ($this->variableName ? ' ' : '') . $this->description : ''); + . (('' . $this->description) ? ' ' . $this->description : ''); } } diff --git a/tests/unit/DocBlock/Tags/VarTest.php b/tests/unit/DocBlock/Tags/VarTest.php index 4a8235a..63d3b97 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -237,6 +237,35 @@ class VarTest extends TestCase $this->assertSame('My Description', $fixture->getDescription() . ''); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Param:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + * + * @covers ::create + */ + public function testFactoryMethodWithTypeWithoutComment() : void + { + $typeResolver = new TypeResolver(); + $fqsenResolver = new FqsenResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $context = new Context(''); + + $fixture = Var_::create( + 'int', + $typeResolver, + $descriptionFactory, + $context + ); + + $this->assertSame('int', (string) $fixture); + $this->assertSame('', $fixture->getVariableName()); + $this->assertInstanceOf(Integer::class, $fixture->getType()); + $this->assertSame('', $fixture->getDescription() . ''); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_:: * @uses \phpDocumentor\Reflection\TypeResolver From 97863e0c448c91824bc8d924acd4bb9a6792a4f9 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 00:33:09 +0200 Subject: [PATCH 05/10] add more unit tests and normalize the "__toString" methods --- src/DocBlock/Tags/Author.php | 10 +++++- src/DocBlock/Tags/Covers.php | 10 +++++- src/DocBlock/Tags/Deprecated.php | 10 +++++- src/DocBlock/Tags/Example.php | 5 ++- src/DocBlock/Tags/Generic.php | 8 ++++- src/DocBlock/Tags/Link.php | 10 +++++- src/DocBlock/Tags/Method.php | 23 +++++++++++--- src/DocBlock/Tags/Param.php | 22 ++++++++++--- src/DocBlock/Tags/Property.php | 20 ++++++++++-- src/DocBlock/Tags/PropertyRead.php | 20 ++++++++++-- src/DocBlock/Tags/PropertyWrite.php | 20 ++++++++++-- src/DocBlock/Tags/Return_.php | 10 +++++- src/DocBlock/Tags/See.php | 10 +++++- src/DocBlock/Tags/Since.php | 10 +++++- src/DocBlock/Tags/Source.php | 16 ++++++++-- src/DocBlock/Tags/Throws.php | 10 +++++- src/DocBlock/Tags/Uses.php | 10 +++++- src/DocBlock/Tags/Var_.php | 20 ++++++++++-- src/DocBlock/Tags/Version.php | 11 +++++-- tests/unit/DocBlock/Tags/AuthorTest.php | 17 +++++++++++ tests/unit/DocBlock/Tags/CoversTest.php | 34 +++++++++++++++++++++ tests/unit/DocBlock/Tags/DeprecatedTest.php | 31 +++++++++++++++++++ tests/unit/DocBlock/Tags/ExampleTest.php | 12 ++++---- tests/unit/DocBlock/Tags/GenericTest.php | 20 ++++++++++++ tests/unit/DocBlock/Tags/LinkTest.php | 19 ++++++++++++ tests/unit/DocBlock/Tags/MethodTest.php | 30 ++++++++++++++++++ tests/unit/DocBlock/Tags/ReturnTest.php | 19 ++++++++++++ tests/unit/DocBlock/Tags/SeeTest.php | 21 +++++++++++++ tests/unit/DocBlock/Tags/SinceTest.php | 19 ++++++++++++ tests/unit/DocBlock/Tags/SourceTest.php | 26 ++++++++++++++++ tests/unit/DocBlock/Tags/ThrowsTest.php | 25 +++++++++++++++ tests/unit/DocBlock/Tags/UsesTest.php | 19 ++++++++++++ tests/unit/DocBlock/Tags/VarTest.php | 26 ++++++++++++++++ tests/unit/DocBlock/Tags/VersionTest.php | 19 ++++++++++++ 34 files changed, 548 insertions(+), 44 deletions(-) diff --git a/src/DocBlock/Tags/Author.php b/src/DocBlock/Tags/Author.php index f3c49ad..d120757 100644 --- a/src/DocBlock/Tags/Author.php +++ b/src/DocBlock/Tags/Author.php @@ -71,7 +71,15 @@ final class Author extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->authorName . ($this->authorEmail !== '' ? ' <' . $this->authorEmail . '>' : ''); + if ($this->authorEmail) { + $authorEmail = '<' . $this->authorEmail . '>'; + } else { + $authorEmail = ''; + } + + $authorName = (string) $this->authorName; + + return $authorName . ($authorEmail !== '' ? ($authorName !== '' ? ' ' : '') . $authorEmail : ''); } /** diff --git a/src/DocBlock/Tags/Covers.php b/src/DocBlock/Tags/Covers.php index 820d595..0482353 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -72,6 +72,14 @@ final class Covers extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->refers . ($this->description ? ' ' . $this->description->render() : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $refers = (string) $this->refers; + + return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Deprecated.php b/src/DocBlock/Tags/Deprecated.php index 9b05d22..68e8f03 100644 --- a/src/DocBlock/Tags/Deprecated.php +++ b/src/DocBlock/Tags/Deprecated.php @@ -95,6 +95,14 @@ final class Deprecated extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return ($this->version ?? '') . ($this->description ? ' ' . $this->description->render() : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $version = (string) $this->version; + + return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index 8ccb4fd..2853673 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -142,7 +142,10 @@ final class Example implements Tag, Factory\StaticMethod */ public function __toString() : string { - return $this->filePath . ($this->content ? ' ' . $this->content : ''); + $filePath = (string) $this->filePath; + $content = (string) $this->content; + + return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : ''); } /** diff --git a/src/DocBlock/Tags/Generic.php b/src/DocBlock/Tags/Generic.php index 7509ff1..a7b423f 100644 --- a/src/DocBlock/Tags/Generic.php +++ b/src/DocBlock/Tags/Generic.php @@ -64,7 +64,13 @@ final class Generic extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->description ? $this->description->render() : ''; + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + return $description; } /** diff --git a/src/DocBlock/Tags/Link.php b/src/DocBlock/Tags/Link.php index e912e48..226bbe0 100644 --- a/src/DocBlock/Tags/Link.php +++ b/src/DocBlock/Tags/Link.php @@ -65,6 +65,14 @@ final class Link extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->link . ($this->description ? ' ' . $this->description->render() : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $link = (string) $this->link; + + return $link . ($description !== '' ? ($link !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 834f1bd..e568363 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -212,12 +212,25 @@ final class Method extends BaseTag implements Factory\StaticMethod foreach ($this->arguments as $argument) { $arguments[] = $argument['type'] . ' $' . $argument['name']; } + $argumentStr = '(' . implode(', ', $arguments) . ')'; - return trim(($this->isStatic() ? 'static ' : '') - . (string) $this->returnType . ' ' - . $this->methodName - . '(' . implode(', ', $arguments) . ')' - . ($this->description ? ' ' . $this->description->render() : '')); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $static = $this->isStatic ? 'static' : ''; + + $returnType = (string) $this->returnType; + + $methodName = (string) $this->methodName; + + return $static + . ($returnType !== '' ? ($static !== '' ? ' ' : '') . $returnType : '') + . ($methodName !== '' ? ($static !== '' || $returnType !== '' ? ' ' : '') . $methodName : '') + . $argumentStr + . ($description !== '' ? ' ' . $description : ''); } /** diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index 3f272d0..9b5d8b7 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -140,11 +140,23 @@ final class Param extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') - . ($this->isReference() ? '&' : '') - . ($this->isVariadic() ? '...' : '') - . ($this->variableName ? '$' . $this->variableName : '') - . (('' . $this->description) ? ' ' . $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $variableName = ''; + if ($this->variableName) { + $variableName .= ($this->isReference ? '&' : '') . ($this->isVariadic ? '...' : ''); + $variableName .= ($this->variableName ? '$' . $this->variableName : ''); + } + + $type = (string) $this->type; + + return $type + . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') + . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); } private static function strStartsWithVariable(string $str) : bool diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index 866b919..b832156 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -98,8 +98,22 @@ final class Property extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') - . ($this->variableName ? '$' . $this->variableName : '') - . (('' . $this->description) ? ' ' . $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + if ($this->variableName) { + $variableName = ($this->variableName ? '$' . $this->variableName : ''); + } else { + $variableName = ''; + } + + $type = (string) $this->type; + + return $type + . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') + . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 9a10d6e..674a7b0 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -98,8 +98,22 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') - . ($this->variableName ? '$' . $this->variableName : '') - . (('' . $this->description) ? ' ' . $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + if ($this->variableName) { + $variableName = ($this->variableName ? '$' . $this->variableName : ''); + } else { + $variableName = ''; + } + + $type = (string) $this->type; + + return $type + . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') + . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index b4f6296..4962461 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -98,8 +98,22 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') - . ($this->variableName ? '$' . $this->variableName : '') - . (('' . $this->description) ? ' ' . $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + if ($this->variableName) { + $variableName = ($this->variableName ? '$' . $this->variableName : ''); + } else { + $variableName = ''; + } + + $type = (string) $this->type; + + return $type + . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') + . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Return_.php b/src/DocBlock/Tags/Return_.php index 60ba860..546a0ea 100644 --- a/src/DocBlock/Tags/Return_.php +++ b/src/DocBlock/Tags/Return_.php @@ -51,6 +51,14 @@ final class Return_ extends TagWithType implements Factory\StaticMethod public function __toString() : string { - return ($this->type ?: 'mixed') . ' ' . (string) $this->description; + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $type = $this->type ? '' . $this->type : 'mixed'; + + return $type . ($description !== '' ? ($type !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/See.php b/src/DocBlock/Tags/See.php index e71401d..6d7b1b1 100644 --- a/src/DocBlock/Tags/See.php +++ b/src/DocBlock/Tags/See.php @@ -77,6 +77,14 @@ final class See extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->refers . ($this->description ? ' ' . $this->description->render() : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $refers = (string) $this->refers; + + return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Since.php b/src/DocBlock/Tags/Since.php index dc12624..32de527 100644 --- a/src/DocBlock/Tags/Since.php +++ b/src/DocBlock/Tags/Since.php @@ -89,6 +89,14 @@ final class Since extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return (string) $this->version . ($this->description ? ' ' . (string) $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $version = (string) $this->version; + + return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Source.php b/src/DocBlock/Tags/Source.php index 6d3c6cb..78ea7fd 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -96,8 +96,18 @@ final class Source extends BaseTag implements Factory\StaticMethod public function __toString() : string { - return $this->startingLine - . ($this->lineCount !== null ? ' ' . $this->lineCount : '') - . ($this->description ? ' ' . (string) $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $startingLine = (string) $this->startingLine; + + $lineCount = $this->lineCount !== null ? '' . $this->lineCount : ''; + + return $startingLine + . ($lineCount !== '' ? ($startingLine !== '' ? ' ' : '') . $lineCount : '') + . ($description !== '' ? ($startingLine !== '' || $lineCount !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Throws.php b/src/DocBlock/Tags/Throws.php index 13f07ce..d4dc947 100644 --- a/src/DocBlock/Tags/Throws.php +++ b/src/DocBlock/Tags/Throws.php @@ -51,6 +51,14 @@ final class Throws extends TagWithType implements Factory\StaticMethod public function __toString() : string { - return (string) $this->type . ' ' . (string) $this->description; + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $type = (string) $this->type; + + return $type . ($description !== '' ? ($type !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Uses.php b/src/DocBlock/Tags/Uses.php index 57fb290..3ed83b6 100644 --- a/src/DocBlock/Tags/Uses.php +++ b/src/DocBlock/Tags/Uses.php @@ -71,6 +71,14 @@ final class Uses extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return $this->refers . ' ' . (string) $this->description; + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $refers = (string) $this->refers; + + return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index 1683d10..90729a2 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -99,8 +99,22 @@ final class Var_ extends TagWithType implements Factory\StaticMethod */ public function __toString() : string { - return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '') - . ($this->variableName ? '$' . $this->variableName : '') - . (('' . $this->description) ? ' ' . $this->description : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + if ($this->variableName) { + $variableName = ($this->variableName ? '$' . $this->variableName : ''); + } else { + $variableName = ''; + } + + $type = (string) $this->type; + + return $type + . ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '') + . ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Version.php b/src/DocBlock/Tags/Version.php index eaadf4d..460c86d 100644 --- a/src/DocBlock/Tags/Version.php +++ b/src/DocBlock/Tags/Version.php @@ -92,7 +92,14 @@ final class Version extends BaseTag implements Factory\StaticMethod */ public function __toString() : string { - return ((string) $this->version) . - ($this->description instanceof Description ? ' ' . $this->description->render() : ''); + if ($this->description) { + $description = $this->description->render(); + } else { + $description = ''; + } + + $version = (string) $this->version; + + return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : ''); } } diff --git a/tests/unit/DocBlock/Tags/AuthorTest.php b/tests/unit/DocBlock/Tags/AuthorTest.php index 59e4cda..8db3887 100644 --- a/tests/unit/DocBlock/Tags/AuthorTest.php +++ b/tests/unit/DocBlock/Tags/AuthorTest.php @@ -116,6 +116,23 @@ class AuthorTest extends TestCase $fixture = new Author('Mike van Riel', 'mike@phpdoc.org'); $this->assertSame('Mike van Riel ', (string) $fixture); + + // --- + + $fixture = new Author('0', 'zero@foo.bar'); + + $this->assertSame('0 ', (string) $fixture); + } + + /** + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutName() : void + { + $fixture = new Author('', 'mike@phpdoc.org'); + + $this->assertSame('', (string) $fixture); } /** diff --git a/tests/unit/DocBlock/Tags/CoversTest.php b/tests/unit/DocBlock/Tags/CoversTest.php index f4319a9..93ece75 100644 --- a/tests/unit/DocBlock/Tags/CoversTest.php +++ b/tests/unit/DocBlock/Tags/CoversTest.php @@ -151,6 +151,40 @@ class CoversTest extends TestCase $this->assertSame($description, $fixture->getDescription()); } + /** + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Covers(new Fqsen('\\')); + + $this->assertSame('\\', (string) $fixture); + + // --- + + $fixture = new Covers(new Fqsen('\DateTime')); + + $this->assertSame('\DateTime', (string) $fixture); + + // --- + + $fixture = new Covers(new Fqsen('\DateTime'), new Description('')); + + $this->assertSame('\DateTime', (string) $fixture); + } + + /** + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithDescription() : void + { + $fixture = new Covers(new Fqsen('\DateTime'), new Description('My Description')); + + $this->assertSame('\DateTime My Description', (string) $fixture); + } + /** * @covers ::create */ diff --git a/tests/unit/DocBlock/Tags/DeprecatedTest.php b/tests/unit/DocBlock/Tags/DeprecatedTest.php index 9df9a7f..c5ef41a 100644 --- a/tests/unit/DocBlock/Tags/DeprecatedTest.php +++ b/tests/unit/DocBlock/Tags/DeprecatedTest.php @@ -117,6 +117,37 @@ class DeprecatedTest extends TestCase $fixture = new Deprecated('1.0', new Description('Description')); $this->assertSame('1.0 Description', (string) $fixture); + + // --- + + $fixture = new Deprecated(null, new Description('My Description')); + + $this->assertSame('My Description', (string) $fixture); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Deprecated(null, new Description('')); + + $this->assertSame('', (string) $fixture); + + // --- + + $fixture = new Deprecated('1.0', new Description('')); + + $this->assertSame('1.0', (string) $fixture); + + // --- + + $fixture = new Deprecated('1.0'); + + $this->assertSame('1.0', (string) $fixture); } /** diff --git a/tests/unit/DocBlock/Tags/ExampleTest.php b/tests/unit/DocBlock/Tags/ExampleTest.php index 77809d4..e6c123a 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -22,7 +22,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -37,7 +37,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -52,7 +52,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -67,7 +67,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -84,7 +84,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -101,7 +101,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct diff --git a/tests/unit/DocBlock/Tags/GenericTest.php b/tests/unit/DocBlock/Tags/GenericTest.php index a3c8248..14287df 100644 --- a/tests/unit/DocBlock/Tags/GenericTest.php +++ b/tests/unit/DocBlock/Tags/GenericTest.php @@ -107,6 +107,26 @@ class GenericTest extends TestCase $this->assertSame('Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Generic('generic'); + + $this->assertSame('', (string) $fixture); + + // --- + + $fixture = new Generic('generic', new Description('')); + + $this->assertSame('', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/LinkTest.php b/tests/unit/DocBlock/Tags/LinkTest.php index 00c8fe5..6110189 100644 --- a/tests/unit/DocBlock/Tags/LinkTest.php +++ b/tests/unit/DocBlock/Tags/LinkTest.php @@ -119,6 +119,25 @@ class LinkTest extends TestCase $this->assertSame('http://this.is.my/link Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Link('http://this.is.my/link'); + + $this->assertSame('http://this.is.my/link', (string) $fixture); + + // --- + + $fixture = new Link('http://this.is.my/link', new Description('')); + + $this->assertSame('http://this.is.my/link', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Link:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index 8156d07..3b5786e 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -268,6 +268,36 @@ class MethodTest extends TestCase ); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Method::isStatic + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Method('myMethod', [], null, false, new Description('')); + + $this->assertSame( + 'void myMethod()', + (string) $fixture + ); + + // --- + + $arguments = [ + ['name' => 'argument1', 'type' => new String_()], + ['name' => 'argument2', 'type' => new Object_()], + ]; + $fixture = new Method('myMethod', $arguments, new Void_(), true); + + $this->assertSame( + 'static void myMethod(string $argument1, object $argument2)', + (string) $fixture + ); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Method:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/ReturnTest.php b/tests/unit/DocBlock/Tags/ReturnTest.php index aad958c..b93cc1a 100644 --- a/tests/unit/DocBlock/Tags/ReturnTest.php +++ b/tests/unit/DocBlock/Tags/ReturnTest.php @@ -122,6 +122,25 @@ class ReturnTest extends TestCase $this->assertSame('string Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Return_(new String_()); + + $this->assertSame('string', (string) $fixture); + + // --- + + $fixture = new Return_(new String_(), new Description('')); + + $this->assertSame('string', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Return_:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/SeeTest.php b/tests/unit/DocBlock/Tags/SeeTest.php index f9ed04c..891b870 100644 --- a/tests/unit/DocBlock/Tags/SeeTest.php +++ b/tests/unit/DocBlock/Tags/SeeTest.php @@ -135,6 +135,27 @@ class SeeTest extends TestCase $this->assertSame('\DateTime::format() Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen + * @uses \phpDocumentor\Reflection\Fqsen + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new See(new FqsenRef(new Fqsen('\DateTime::format()'))); + + $this->assertSame('\DateTime::format()', (string) $fixture); + + // --- + + $fixture = new See(new FqsenRef(new Fqsen('\DateTime::format()')), new Description('')); + + $this->assertSame('\DateTime::format()', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\See:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/SinceTest.php b/tests/unit/DocBlock/Tags/SinceTest.php index ff926d2..2296ec6 100644 --- a/tests/unit/DocBlock/Tags/SinceTest.php +++ b/tests/unit/DocBlock/Tags/SinceTest.php @@ -119,6 +119,25 @@ class SinceTest extends TestCase $this->assertSame('1.0 Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Since('1.0'); + + $this->assertSame('1.0', (string) $fixture); + + // --- + + $fixture = new Since('1.0', new Description('')); + + $this->assertSame('1.0', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Since:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/SourceTest.php b/tests/unit/DocBlock/Tags/SourceTest.php index 178d16e..26cd46a 100644 --- a/tests/unit/DocBlock/Tags/SourceTest.php +++ b/tests/unit/DocBlock/Tags/SourceTest.php @@ -137,6 +137,32 @@ class SourceTest extends TestCase $this->assertSame('1 10 Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\String_ + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Source(1); + + $this->assertSame('1', (string) $fixture); + + // --- + + $fixture = new Source(1, 0); + + $this->assertSame('1 0', (string) $fixture); + + // --- + + $fixture = new Source(1, 10, new Description('')); + + $this->assertSame('1 10', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Source:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/ThrowsTest.php b/tests/unit/DocBlock/Tags/ThrowsTest.php index ab1f759..ddead29 100644 --- a/tests/unit/DocBlock/Tags/ThrowsTest.php +++ b/tests/unit/DocBlock/Tags/ThrowsTest.php @@ -120,6 +120,31 @@ class ThrowsTest extends TestCase $fixture = new Throws(new String_(), new Description('Description')); $this->assertSame('string Description', (string) $fixture); + + // --- + + $fixture = new Throws(new String_(), new Description('My Description')); + + $this->assertSame('string My Description', (string) $fixture); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Throws(new String_()); + + $this->assertSame('string', (string) $fixture); + + // --- + + $fixture = new Throws(new String_(), new Description('')); + + $this->assertSame('string', (string) $fixture); } /** diff --git a/tests/unit/DocBlock/Tags/UsesTest.php b/tests/unit/DocBlock/Tags/UsesTest.php index 979a675..f45d880 100644 --- a/tests/unit/DocBlock/Tags/UsesTest.php +++ b/tests/unit/DocBlock/Tags/UsesTest.php @@ -121,6 +121,25 @@ class UsesTest extends TestCase $this->assertSame('\DateTime Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Uses(new Fqsen('\DateTime')); + + $this->assertSame('\DateTime', (string) $fixture); + + // --- + + $fixture = new Uses(new Fqsen('\DateTime'), new Description('')); + + $this->assertSame('\DateTime', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/VarTest.php b/tests/unit/DocBlock/Tags/VarTest.php index 63d3b97..176971d 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -154,6 +154,32 @@ class VarTest extends TestCase $this->assertSame('string $myVariable Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\String_ + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Var_('myVariable'); + + $this->assertSame('$myVariable', (string) $fixture); + + // --- + + $fixture = new Var_('myVariable', new String_()); + + $this->assertSame('string $myVariable', (string) $fixture); + + // --- + + $fixture = new Var_('myVariable', new String_(), new Description('')); + + $this->assertSame('string $myVariable', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory diff --git a/tests/unit/DocBlock/Tags/VersionTest.php b/tests/unit/DocBlock/Tags/VersionTest.php index 04ce027..c3c025d 100644 --- a/tests/unit/DocBlock/Tags/VersionTest.php +++ b/tests/unit/DocBlock/Tags/VersionTest.php @@ -119,6 +119,25 @@ class VersionTest extends TestCase $this->assertSame('1.0 Description', (string) $fixture); } + /** + * @uses \phpDocumentor\Reflection\DocBlock\Description + * + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $fixture = new Version('1.0'); + + $this->assertSame('1.0', (string) $fixture); + + // --- + + $fixture = new Version('1.0', new Description('')); + + $this->assertSame('1.0', (string) $fixture); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\Version:: * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory From 3d8d7df9f11d49a64e2ccd0e985f747b31b77762 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 00:35:02 +0200 Subject: [PATCH 06/10] clean-up code comments --- src/DocBlock/Tags/PropertyRead.php | 2 +- src/DocBlock/Tags/PropertyWrite.php | 2 +- src/DocBlock/Tags/Var_.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 674a7b0..fbe387f 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -68,7 +68,7 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod array_unshift($parts, $firstPart); } - // if the next item starts with a $ or ...$ it must be the variable name + // if the next item starts with a $ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); if ($type) { diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index 4962461..42c9207 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -68,7 +68,7 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod array_unshift($parts, $firstPart); } - // if the next item starts with a $ or ...$ it must be the variable name + // if the next item starts with a $ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); if ($type) { diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index 90729a2..a2d4426 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -69,7 +69,7 @@ final class Var_ extends TagWithType implements Factory\StaticMethod array_unshift($parts, $firstPart); } - // if the next item starts with a $ or ...$ it must be the variable name + // if the next item starts with a $ it must be the variable name if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); if ($type) { From 4e6d7ecd03918fe6e44263689ca023b08542513c Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 02:06:24 +0200 Subject: [PATCH 07/10] add more unit tests and normalize the "__toString" methods v2 --- src/DocBlock/Tags/Covers.php | 2 +- src/DocBlock/Tags/Example.php | 25 +++++++++---- tests/unit/DocBlock/Tags/ExampleTest.php | 46 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/DocBlock/Tags/Covers.php b/src/DocBlock/Tags/Covers.php index 096d836..9e52e5e 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -49,7 +49,7 @@ final class Covers extends BaseTag implements Factory\StaticMethod ?FqsenResolver $resolver = null, ?TypeContext $context = null ) : self { - Assert::notEmpty($body); + Assert::stringNotEmpty($body); Assert::notNull($descriptionFactory); Assert::notNull($resolver); diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index c91f75c..4f95c89 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -45,9 +45,14 @@ final class Example implements Tag, Factory\StaticMethod /** @var string|null */ private $content; - public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content) - { - Assert::notEmpty($filePath); + public function __construct( + string $filePath, + bool $isURI, + int $startingLine, + int $lineCount, + ?string $content + ) { + Assert::stringNotEmpty($filePath); Assert::greaterThanEq($startingLine, 1); Assert::greaterThanEq($lineCount, 0); @@ -64,7 +69,7 @@ final class Example implements Tag, Factory\StaticMethod public function getContent() : string { if ($this->content === null || $this->content === '') { - $filePath = '"' . $this->filePath . '"'; + $filePath = $this->filePath; if ($this->isURI) { $filePath = $this->isUriRelative($this->filePath) ? str_replace('%2F', '/', rawurlencode($this->filePath)) @@ -85,7 +90,7 @@ final class Example implements Tag, Factory\StaticMethod public static function create(string $body) : ?Tag { // File component: File path in quotes or File URI / Source information - if (!preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) { + if (!preg_match('/^\s*(?:(\"[^\"]+\")|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) { return null; } @@ -134,7 +139,7 @@ final class Example implements Tag, Factory\StaticMethod */ public function getFilePath() : string { - return $this->filePath; + return trim($this->filePath, '"'); } /** @@ -143,9 +148,15 @@ final class Example implements Tag, Factory\StaticMethod public function __toString() : string { $filePath = (string) $this->filePath; + $isDefaultLine = $this->startingLine === 1 && $this->lineCount === 0; + $startingLine = !$isDefaultLine ? (string) $this->startingLine : ''; + $lineCount = !$isDefaultLine ? (string) $this->lineCount : ''; $content = (string) $this->content; - return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : ''); + return $filePath + . ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '') + . ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '') + . ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : ''); } /** diff --git a/tests/unit/DocBlock/Tags/ExampleTest.php b/tests/unit/DocBlock/Tags/ExampleTest.php index 1d6d159..3bf7aa0 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -93,6 +93,52 @@ class ExampleTest extends TestCase $this->assertEquals(5, $tag->getLineCount()); } + /** + * @covers ::create + * @covers ::__toString + */ + public function testStringRepresentationIsReturned() : void + { + $tag = Example::create('"example1.php" 10 5 test text'); + + $this->assertSame('"example1.php" 10 5 test text', (string) $tag); + + // --- + + $tag = Example::create('file://example1.php'); + + $this->assertSame('file://example1.php', (string) $tag); + + // --- + + $tag = Example::create('0 foo bar'); + + $this->assertSame('0 foo bar', (string) $tag); + + // --- + + $tag = Example::create('$redisCluster->pttl(\'key\');'); + + $this->assertSame('$redisCluster->pttl(\'key\');', (string) $tag); + + // --- + + $tag = Example::create(' "example1.php" 10 5 test text '); + + $this->assertSame('"example1.php" 10 5 test text', (string) $tag); + } + + /** + * @covers ::create + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $tag = Example::create(''); + + $this->assertSame('', (string) $tag); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * From 4438ee7955e384e4cc1a16369f384ca7c4c4a5af Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 02:38:39 +0200 Subject: [PATCH 08/10] add more unit tests and normalize the "__toString" methods -> fix code style + psalm reported errors --- src/DocBlock/Tags/Example.php | 12 +++++++++--- src/DocBlock/Tags/Method.php | 1 + src/DocBlock/Tags/Param.php | 2 +- src/DocBlock/Tags/Property.php | 2 +- src/DocBlock/Tags/PropertyRead.php | 2 +- src/DocBlock/Tags/PropertyWrite.php | 2 +- src/DocBlock/Tags/Source.php | 4 ++-- src/DocBlock/Tags/Var_.php | 2 +- tests/unit/DocBlock/Tags/MethodTest.php | 2 +- 9 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index 4f95c89..3face1e 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -154,9 +154,15 @@ final class Example implements Tag, Factory\StaticMethod $content = (string) $this->content; return $filePath - . ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '') - . ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '') - . ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : ''); + . ($startingLine !== '' + ? ($filePath !== '' ? ' ' : '') . $startingLine + : '') + . ($lineCount !== '' + ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount + : '') + . ($content !== '' + ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content + : ''); } /** diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index e568363..08c0407 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -212,6 +212,7 @@ final class Method extends BaseTag implements Factory\StaticMethod foreach ($this->arguments as $argument) { $arguments[] = $argument['type'] . ' $' . $argument['name']; } + $argumentStr = '(' . implode(', ', $arguments) . ')'; if ($this->description) { diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index 9b5d8b7..83419e9 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -149,7 +149,7 @@ final class Param extends TagWithType implements Factory\StaticMethod $variableName = ''; if ($this->variableName) { $variableName .= ($this->isReference ? '&' : '') . ($this->isVariadic ? '...' : ''); - $variableName .= ($this->variableName ? '$' . $this->variableName : ''); + $variableName .= '$' . $this->variableName; } $type = (string) $this->type; diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index b832156..0389757 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -105,7 +105,7 @@ final class Property extends TagWithType implements Factory\StaticMethod } if ($this->variableName) { - $variableName = ($this->variableName ? '$' . $this->variableName : ''); + $variableName = '$' . $this->variableName; } else { $variableName = ''; } diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index fbe387f..7ff55d5 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -105,7 +105,7 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod } if ($this->variableName) { - $variableName = ($this->variableName ? '$' . $this->variableName : ''); + $variableName = '$' . $this->variableName; } else { $variableName = ''; } diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index 42c9207..cc1e4b6 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -105,7 +105,7 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod } if ($this->variableName) { - $variableName = ($this->variableName ? '$' . $this->variableName : ''); + $variableName = '$' . $this->variableName; } else { $variableName = ''; } diff --git a/src/DocBlock/Tags/Source.php b/src/DocBlock/Tags/Source.php index 78ea7fd..ee12357 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -107,7 +107,7 @@ final class Source extends BaseTag implements Factory\StaticMethod $lineCount = $this->lineCount !== null ? '' . $this->lineCount : ''; return $startingLine - . ($lineCount !== '' ? ($startingLine !== '' ? ' ' : '') . $lineCount : '') - . ($description !== '' ? ($startingLine !== '' || $lineCount !== '' ? ' ' : '') . $description : ''); + . ($lineCount !== '' ? ($startingLine || $startingLine === '0' ? ' ' : '') . $lineCount : '') + . ($description !== '' ? ($startingLine || $startingLine === '0' || $lineCount !== '' ? ' ' : '') . $description : ''); } } diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index a2d4426..762c262 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -106,7 +106,7 @@ final class Var_ extends TagWithType implements Factory\StaticMethod } if ($this->variableName) { - $variableName = ($this->variableName ? '$' . $this->variableName : ''); + $variableName = '$' . $this->variableName; } else { $variableName = ''; } diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index 3b5786e..a07c98a 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -277,7 +277,7 @@ class MethodTest extends TestCase */ public function testStringRepresentationIsReturnedWithoutDescription() : void { - $fixture = new Method('myMethod', [], null, false, new Description('')); + $fixture = new Method('myMethod', [], null, false, new Description('')); $this->assertSame( 'void myMethod()', From 5bb97a97c001ca2c6f661e0a0e86005ad5bedaaa Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 02:51:17 +0200 Subject: [PATCH 09/10] add more unit tests and normalize the "__toString" methods -> fix code style v2 --- src/DocBlock/Tags/Source.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/DocBlock/Tags/Source.php b/src/DocBlock/Tags/Source.php index ee12357..f0c3101 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -107,7 +107,11 @@ final class Source extends BaseTag implements Factory\StaticMethod $lineCount = $this->lineCount !== null ? '' . $this->lineCount : ''; return $startingLine - . ($lineCount !== '' ? ($startingLine || $startingLine === '0' ? ' ' : '') . $lineCount : '') - . ($description !== '' ? ($startingLine || $startingLine === '0' || $lineCount !== '' ? ' ' : '') . $description : ''); + . ($lineCount !== '' + ? ($startingLine || $startingLine === '0' ? ' ' : '') . $lineCount + : '') + . ($description !== '' + ? ($startingLine || $startingLine === '0' || $lineCount !== '' ? ' ' : '') . $description + : ''); } } From 2ef4c3da53b369fbbc1866fc505327d00169233a Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Thu, 3 Sep 2020 02:54:11 +0200 Subject: [PATCH 10/10] add more unit tests and normalize the "__toString" methods -> update the code coverage level --- .github/workflows/push.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index d3419fe..cf2f4db 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -46,7 +46,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Quick check code coverage level - run: php tests/coverage-checker.php 89 + run: php tests/coverage-checker.php 91 phpunit: name: Unit tests for PHP version ${{ matrix.php-versions }} on ${{ matrix.operating-system }}