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 392b4f9e..dea81a7c 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -75,7 +75,7 @@ public static function create( $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 @@ public static function create( } // 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 @@ public function __toString() : string . ($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 dea81a7c..281756f0 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -145,12 +145,7 @@ public function __toString() : string . ($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 281756f0..650506f9 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -85,7 +85,9 @@ public static function create( // 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 @@ public function __toString() : string 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 d8b83b6f..e358d985 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -68,10 +68,12 @@ public static function create( 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 @@ public function __toString() : string { 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 087803c3..6118d32d 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -71,7 +71,9 @@ public static function create( // 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 @@ public function __toString() : string { 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 176b63d7..d1b5a4e5 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -71,7 +71,9 @@ public static function create( // 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 @@ public function __toString() : string { 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 ac1c4380..f939a25d 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -72,7 +72,9 @@ public static function create( // 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 @@ public function getVariableName() : ?string 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 246b57e5..99a84dab 100644 --- a/tests/unit/DocBlock/Tags/ParamTest.php +++ b/tests/unit/DocBlock/Tags/ParamTest.php @@ -16,8 +16,11 @@ 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 @@ public function testFactoryMethodWithVariadicReference() : void $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 901b0907..d2b2ee8c 100644 --- a/tests/unit/DocBlock/Tags/PropertyReadTest.php +++ b/tests/unit/DocBlock/Tags/PropertyReadTest.php @@ -16,8 +16,11 @@ 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 @@ public function testFactoryMethod() : void $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 ec0e7fc3..bf5258b7 100644 --- a/tests/unit/DocBlock/Tags/PropertyTest.php +++ b/tests/unit/DocBlock/Tags/PropertyTest.php @@ -16,8 +16,11 @@ 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 @@ public function testFactoryMethod() : void $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 1c6124c2..e25b360f 100644 --- a/tests/unit/DocBlock/Tags/PropertyWriteTest.php +++ b/tests/unit/DocBlock/Tags/PropertyWriteTest.php @@ -16,8 +16,11 @@ 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 @@ public function testFactoryMethod() : void $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 409263ed..4a8235a3 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -16,8 +16,11 @@ 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 @@ public function testFactoryMethod() : void $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 650506f9..3f272d01 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -140,11 +140,11 @@ public function isReference() : bool */ 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 e358d985..866b919a 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -98,8 +98,8 @@ public function getVariableName() : ?string */ 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 6118d32d..9a10d6ec 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -98,8 +98,8 @@ public function getVariableName() : ?string */ 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 d1b5a4e5..b4f62961 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -98,8 +98,8 @@ public function getVariableName() : ?string */ 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 f939a25d..1683d10a 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -99,8 +99,8 @@ public function getVariableName() : ?string */ 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 4a8235a3..63d3b974 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -237,6 +237,35 @@ public function testFactoryMethodWithType() : void $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 f3c49ad5..d1207571 100644 --- a/src/DocBlock/Tags/Author.php +++ b/src/DocBlock/Tags/Author.php @@ -71,7 +71,15 @@ public function getEmail() : string */ 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 820d5952..04823533 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -72,6 +72,14 @@ public function getReference() : Fqsen */ 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 9b05d22e..68e8f036 100644 --- a/src/DocBlock/Tags/Deprecated.php +++ b/src/DocBlock/Tags/Deprecated.php @@ -95,6 +95,14 @@ public function getVersion() : ?string */ 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 8ccb4fdc..2853673b 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -142,7 +142,10 @@ public function getFilePath() : string */ 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 7509ff1a..a7b423f5 100644 --- a/src/DocBlock/Tags/Generic.php +++ b/src/DocBlock/Tags/Generic.php @@ -64,7 +64,13 @@ public static function create( */ 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 e912e481..226bbe0f 100644 --- a/src/DocBlock/Tags/Link.php +++ b/src/DocBlock/Tags/Link.php @@ -65,6 +65,14 @@ public function getLink() : string */ 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 834f1bde..e5683638 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -212,12 +212,25 @@ public function __toString() : string 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 3f272d01..9b5d8b74 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -140,11 +140,23 @@ public function isReference() : bool */ 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 866b919a..b8321563 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -98,8 +98,22 @@ public function getVariableName() : ?string */ 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 9a10d6ec..674a7b0e 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -98,8 +98,22 @@ public function getVariableName() : ?string */ 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 b4f62961..49624618 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -98,8 +98,22 @@ public function getVariableName() : ?string */ 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 60ba8603..546a0eaa 100644 --- a/src/DocBlock/Tags/Return_.php +++ b/src/DocBlock/Tags/Return_.php @@ -51,6 +51,14 @@ public static function create( 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 e71401d0..6d7b1b10 100644 --- a/src/DocBlock/Tags/See.php +++ b/src/DocBlock/Tags/See.php @@ -77,6 +77,14 @@ public function getReference() : Reference */ 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 dc126240..32de527b 100644 --- a/src/DocBlock/Tags/Since.php +++ b/src/DocBlock/Tags/Since.php @@ -89,6 +89,14 @@ public function getVersion() : ?string */ 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 6d3c6cb7..78ea7fdb 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -96,8 +96,18 @@ public function getLineCount() : ?int 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 13f07cec..d4dc9472 100644 --- a/src/DocBlock/Tags/Throws.php +++ b/src/DocBlock/Tags/Throws.php @@ -51,6 +51,14 @@ public static function create( 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 57fb2908..3ed83b68 100644 --- a/src/DocBlock/Tags/Uses.php +++ b/src/DocBlock/Tags/Uses.php @@ -71,6 +71,14 @@ public function getReference() : Fqsen */ 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 1683d10a..90729a22 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -99,8 +99,22 @@ public function getVariableName() : ?string */ 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 eaadf4d3..460c86d7 100644 --- a/src/DocBlock/Tags/Version.php +++ b/src/DocBlock/Tags/Version.php @@ -92,7 +92,14 @@ public function getVersion() : ?string */ 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 59e4cdad..8db3887d 100644 --- a/tests/unit/DocBlock/Tags/AuthorTest.php +++ b/tests/unit/DocBlock/Tags/AuthorTest.php @@ -116,6 +116,23 @@ public function testStringRepresentationIsReturned() : void $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 f4319a91..93ece759 100644 --- a/tests/unit/DocBlock/Tags/CoversTest.php +++ b/tests/unit/DocBlock/Tags/CoversTest.php @@ -151,6 +151,40 @@ public function testFactoryMethod() : void $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 9df9a7f7..c5ef41a2 100644 --- a/tests/unit/DocBlock/Tags/DeprecatedTest.php +++ b/tests/unit/DocBlock/Tags/DeprecatedTest.php @@ -117,6 +117,37 @@ public function testStringRepresentationIsReturned() : void $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 77809d49..e6c123a2 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -22,7 +22,7 @@ public function tearDown() : void } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -37,7 +37,7 @@ public function testExampleWithoutContent() : void } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -52,7 +52,7 @@ public function testWithDescription() : void } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -67,7 +67,7 @@ public function testStartlineIsParsed() : void } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -84,7 +84,7 @@ public function testAllowOmitingLineCount() : void } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -101,7 +101,7 @@ public function testLengthIsParsed() : void } /** - * @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 a3c82488..14287dff 100644 --- a/tests/unit/DocBlock/Tags/GenericTest.php +++ b/tests/unit/DocBlock/Tags/GenericTest.php @@ -107,6 +107,26 @@ public function testStringRepresentationIsReturned() : void $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 00c8fe5c..6110189b 100644 --- a/tests/unit/DocBlock/Tags/LinkTest.php +++ b/tests/unit/DocBlock/Tags/LinkTest.php @@ -119,6 +119,25 @@ public function testStringRepresentationIsReturned() : void $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 8156d07e..3b5786ef 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -268,6 +268,36 @@ public function testStringRepresentationIsReturned() : void ); } + /** + * @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 aad958ce..b93cc1a2 100644 --- a/tests/unit/DocBlock/Tags/ReturnTest.php +++ b/tests/unit/DocBlock/Tags/ReturnTest.php @@ -122,6 +122,25 @@ public function testStringRepresentationIsReturned() : void $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 f9ed04c0..891b8702 100644 --- a/tests/unit/DocBlock/Tags/SeeTest.php +++ b/tests/unit/DocBlock/Tags/SeeTest.php @@ -135,6 +135,27 @@ public function testStringRepresentationIsReturned() : void $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 ff926d2c..2296ec67 100644 --- a/tests/unit/DocBlock/Tags/SinceTest.php +++ b/tests/unit/DocBlock/Tags/SinceTest.php @@ -119,6 +119,25 @@ public function testStringRepresentationIsReturned() : void $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 178d16e0..26cd46aa 100644 --- a/tests/unit/DocBlock/Tags/SourceTest.php +++ b/tests/unit/DocBlock/Tags/SourceTest.php @@ -137,6 +137,32 @@ public function testStringRepresentationIsReturned() : void $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 ab1f759a..ddead29f 100644 --- a/tests/unit/DocBlock/Tags/ThrowsTest.php +++ b/tests/unit/DocBlock/Tags/ThrowsTest.php @@ -120,6 +120,31 @@ public function testStringRepresentationIsReturned() : void $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 979a6751..f45d880f 100644 --- a/tests/unit/DocBlock/Tags/UsesTest.php +++ b/tests/unit/DocBlock/Tags/UsesTest.php @@ -121,6 +121,25 @@ public function testStringRepresentationIsReturned() : void $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 63d3b974..176971d3 100644 --- a/tests/unit/DocBlock/Tags/VarTest.php +++ b/tests/unit/DocBlock/Tags/VarTest.php @@ -154,6 +154,32 @@ public function testStringRepresentationIsReturned() : void $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 04ce027e..c3c025d1 100644 --- a/tests/unit/DocBlock/Tags/VersionTest.php +++ b/tests/unit/DocBlock/Tags/VersionTest.php @@ -119,6 +119,25 @@ public function testStringRepresentationIsReturned() : void $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 674a7b0e..fbe387f6 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -68,7 +68,7 @@ public static function create( 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 49624618..42c9207d 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -68,7 +68,7 @@ public static function create( 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 90729a22..a2d44268 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -69,7 +69,7 @@ public static function create( 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 096d8364..9e52e5e4 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -49,7 +49,7 @@ public static function create( ?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 c91f75c5..4f95c894 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 @@ public function __construct(string $filePath, bool $isURI, int $startingLine, in 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 @@ public function getDescription() : ?string 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 @@ public static function create(string $body) : ?Tag */ public function getFilePath() : string { - return $this->filePath; + return trim($this->filePath, '"'); } /** @@ -143,9 +148,15 @@ public function getFilePath() : string 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 1d6d159f..3bf7aa04 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -93,6 +93,52 @@ public function testLengthIsParsed() : void $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 4f95c894..3face1ef 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -154,9 +154,15 @@ public function __toString() : string $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 e5683638..08c0407e 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -212,6 +212,7 @@ public function __toString() : string 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 9b5d8b74..83419e9c 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -149,7 +149,7 @@ public function __toString() : string $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 b8321563..03897578 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -105,7 +105,7 @@ public function __toString() : string } 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 fbe387f6..7ff55d50 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -105,7 +105,7 @@ public function __toString() : string } 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 42c9207d..cc1e4b61 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -105,7 +105,7 @@ public function __toString() : string } 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 78ea7fdb..ee123575 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -107,7 +107,7 @@ public function __toString() : string $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 a2d44268..762c262f 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -106,7 +106,7 @@ public function __toString() : string } 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 3b5786ef..a07c98a3 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -277,7 +277,7 @@ public function testStringRepresentationIsReturned() : void */ 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 ee123575..f0c31014 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -107,7 +107,11 @@ public function __toString() : string $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 d3419fe5..cf2f4dba 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 }}