Generic types bug fix

This commit is contained in:
M@ster
2022-10-19 23:16:04 +02:00
parent 144140cd39
commit 295d2fba38
4 changed files with 87 additions and 16 deletions
@@ -26,7 +26,7 @@ class ReturnTag extends Tag
{ {
/** @var string The raw type component. */ /** @var string The raw type component. */
protected $type = ''; protected $type = '';
/** @var Collection The parsed type component. */ /** @var Collection The parsed type component. */
protected $types = null; protected $types = null;
@@ -49,7 +49,7 @@ class ReturnTag extends Tag
{ {
parent::setContent($content); parent::setContent($content);
$parts = preg_split('/\s+/Su', $this->description, 2); $parts = preg_split('/(?<!,)\s+/Su', $this->description, 2);
// any output is considered a type // any output is considered a type
$this->type = $parts[0]; $this->type = $parts[0];
@@ -112,7 +112,7 @@ class ReturnTag extends Tag
/** /**
* Returns the type collection. * Returns the type collection.
* *
* @return void * @return void
*/ */
protected function getTypesCollection() protected function getTypesCollection()
@@ -106,7 +106,7 @@ class Collection extends \ArrayObject
} }
// separate the type by the OR operator // separate the type by the OR operator
$type_parts = explode(self::OPERATOR_OR, $type); $type_parts = $this->explode($type);
foreach ($type_parts as $part) { foreach ($type_parts as $part) {
$expanded_type = $this->expand($part); $expanded_type = $this->expand($part);
if ($expanded_type) { if ($expanded_type) {
@@ -114,10 +114,10 @@ class Collection extends \ArrayObject
} }
} }
} }
/** /**
* Returns a string representation of the collection. * Returns a string representation of the collection.
* *
* @return string The resolved types across the collection, separated with * @return string The resolved types across the collection, separated with
* {@link self::OPERATOR_OR}. * {@link self::OPERATOR_OR}.
*/ */
@@ -126,6 +126,40 @@ class Collection extends \ArrayObject
return implode(self::OPERATOR_OR, $this->getArrayCopy()); return implode(self::OPERATOR_OR, $this->getArrayCopy());
} }
/**
* Analyzes the given union of types and returns separated by OR operator
* single types.
*
* @param string $type The type or union of types
*
* @return array
*/
protected function explode($type)
{
$type_parts = [];
$curr_type = '';
$nest_level = 0;
foreach (str_split($type) as $char) {
if ($char === self::OPERATOR_OR && $nest_level === 0) {
$type_parts[] = $curr_type;
$curr_type = '';
} else {
if ($char === '<') {
$nest_level++;
} else if ($char === '>') {
$nest_level--;
}
$curr_type .= $char;
}
}
$type_parts[] = $curr_type;
return $type_parts;
}
/** /**
* Analyzes the given type and returns the FQCN variant. * Analyzes the given type and returns the FQCN variant.
* *
@@ -1,7 +1,7 @@
<?php <?php
/** /**
* phpDocumentor Return tag test. * phpDocumentor Return tag test.
* *
* PHP version 5.3 * PHP version 5.3
* *
* @author Mike van Riel <[email protected]> * @author Mike van Riel <[email protected]>
@@ -96,6 +96,27 @@ class ReturnTagTest extends \PHPUnit_Framework_TestCase
'int', 'int',
array('int'), array('int'),
"Number of Bobs" "Number of Bobs"
),
array(
'return',
'array<int, string> Types of Bobs',
'array<int, string>',
array('array<int, string>'),
'Types of Bobs'
),
array(
'return',
'array<int, string>|string Types of Bobs',
'array<int, string>|string',
array('array<int, string>', 'string'),
'Types of Bobs'
),
array(
'return',
'array<int, string|bool>|string Types of Bobs',
'array<int, string|bool>|string',
array('array<int, string|bool>', 'string'),
'Types of Bobs'
) )
); );
} }
@@ -1,7 +1,7 @@
<?php <?php
/** /**
* phpDocumentor Collection Test * phpDocumentor Collection Test
* *
* PHP version 5.3 * PHP version 5.3
* *
* @author Mike van Riel <[email protected]> * @author Mike van Riel <[email protected]>
@@ -16,7 +16,7 @@ use Barryvdh\Reflection\DocBlock\Context;
/** /**
* Test class for \Barryvdh\Reflection\DocBlock\Type\Collection * Test class for \Barryvdh\Reflection\DocBlock\Type\Collection
* *
* @covers Barryvdh\Reflection\DocBlock\Type\Collection * @covers Barryvdh\Reflection\DocBlock\Type\Collection
* *
* @author Mike van Riel <[email protected]> * @author Mike van Riel <[email protected]>
@@ -29,7 +29,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::getContext * @covers Barryvdh\Reflection\DocBlock\Type\Collection::getContext
* *
* @return void * @return void
*/ */
public function testConstruct() public function testConstruct()
@@ -42,7 +42,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
* *
* @return void * @return void
*/ */
public function testConstructWithTypes() public function testConstructWithTypes()
@@ -53,7 +53,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
* *
* @return void * @return void
*/ */
public function testConstructWithNamespace() public function testConstructWithNamespace()
@@ -70,7 +70,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
* *
* @return void * @return void
*/ */
public function testConstructWithNamespaceAliases() public function testConstructWithNamespaceAliases()
@@ -89,7 +89,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
* *
* @dataProvider provideTypesToExpand * @dataProvider provideTypesToExpand
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
* *
* @return void * @return void
*/ */
public function testAdd($fixture, $expected) public function testAdd($fixture, $expected)
@@ -109,7 +109,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
* *
* @dataProvider provideTypesToExpandWithoutNamespace * @dataProvider provideTypesToExpandWithoutNamespace
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
* *
* @return void * @return void
*/ */
public function testAddWithoutNamespace($fixture, $expected) public function testAddWithoutNamespace($fixture, $expected)
@@ -146,7 +146,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
* *
* @return void * @return void
*/ */
public function testAddWithInvalidArgument() public function testAddWithInvalidArgument()
@@ -197,6 +197,22 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
'DocBlock[]|int[]', 'DocBlock[]|int[]',
array($namespace.'DocBlock[]', 'int[]') array($namespace.'DocBlock[]', 'int[]')
), ),
array(
'array<int, string>',
array('array<int, string>')
),
array(
'array<int, string>|string',
array('array<int, string>', 'string')
),
array(
'array<int, float|bool>|string',
array('array<int, float|bool>', 'string')
),
array(
'array<int, string|array<int, bool>>|array<int, float>|string',
array('array<int, string|array<int, bool>>', 'array<int, float>', 'string')
),
array( array(
'LinkDescriptor::setLink()', 'LinkDescriptor::setLink()',
array($namespace.'LinkDescriptor::setLink()') array($namespace.'LinkDescriptor::setLink()')