Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ public function resolve($type, Context $context = null)
$context = new Context('');
}

// split the type string into tokens `|`, `?`, `(`, `)[]`, '<', '>' and type names
$tokens = preg_split('/(\\||\\?|<|>|,|\\(|\\)(?:\\[\\])+)/', $type, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
// split the type string into tokens `|`, `?`, `<`, `>`, `,`, `(`, `)[]`, '<', '>' and type names
$tokens = preg_split('/(\\||\\?|<|>|, ?|\\(|\\)(?:\\[\\])+)/', $type, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

if (false === $tokens) {
throw new \InvalidArgumentException('Unable to split the type string "' . $type . '" into tokens');
}
Expand Down Expand Up @@ -203,7 +204,7 @@ private function parseTypes(\ArrayIterator $tokens, Context $context, $parserCon

$tokens->next();
} elseif ($parserContext === self::PARSER_IN_COLLECTION_EXPRESSION
&& ($token === '>' || $token === ',')
&& ($token === '>' || trim($token) === ',')
) {
break;
} else {
Expand Down Expand Up @@ -412,7 +413,7 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
$valueType = $this->parseTypes($tokens, $context, self::PARSER_IN_COLLECTION_EXPRESSION);
$keyType = null;

if ($tokens->current() === ',') {
if ($tokens->current() !== null && trim($tokens->current()) === ',') {
// if we have a comma, then we just parsed the key type, not the value type
$keyType = $valueType;
if ($isArray) {
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/CollectionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,54 @@ public function testResolvingArrayCollectionWithKey()
$this->assertInstanceOf(Types\Compound::class, $valueType);
}

/**
* @covers ::__construct
* @covers ::resolve
*
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Compound
* @uses \phpDocumentor\Reflection\Types\Collection
* @uses \phpDocumentor\Reflection\Types\String_
*/
public function testResolvingArrayCollectionWithKeyAndWhitespace()
{
$fixture = new TypeResolver();

/** @var Collection $resolvedType */
$resolvedType = $fixture->resolve('array<string, object|array>', new Context(''));

$this->assertInstanceOf(Array_::class, $resolvedType);
$this->assertSame('array<string,object|array>', (string) $resolvedType);

/** @var Array_ $valueType */
$valueType = $resolvedType->getValueType();

/** @var Compound $keyType */
$keyType = $resolvedType->getKeyType();

$this->assertInstanceOf(Types\String_::class, $keyType);
$this->assertInstanceOf(Types\Compound::class, $valueType);
}

/**
* @covers ::__construct
* @covers ::resolve
*
* @expectedException \InvalidArgumentException
*
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Compound
* @uses \phpDocumentor\Reflection\Types\Collection
* @uses \phpDocumentor\Reflection\Types\String_
*/
public function testResolvingArrayCollectionWithKeyAndTooManyWhitespace()
{
$fixture = new TypeResolver();

/** @var Collection $resolvedType */
$resolvedType = $fixture->resolve('array<string, object|array>', new Context(''));
}

/**
* @covers ::__construct
* @covers ::resolve
Expand Down