Skip to content

Commit 300ccee

Browse files
committed
Merge branch 'ignore-whitespaces' of https://github.com/popy-dev/TypeResolver into popy-dev-ignore-whitespaces
2 parents 70fdb32 + bba8c4b commit 300ccee

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/TypeResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public function resolve(string $type, Context $context = null): Type
108108
$context = new Context('');
109109
}
110110

111-
// split the type string into tokens `|`, `?`, `(`, `)[]`, '<', '>' and type names
111+
// split the type string into tokens `|`, `?`, `<`, `>`, `,`, `(`, `)[]`, '<', '>' and type names
112112
$tokens = preg_split(
113-
'/(\\||\\?|<|>|,|\\(|\\)(?:\\[\\])+)/',
113+
'/(\\||\\?|<|>|, ?|\\(|\\)(?:\\[\\])+)/',
114114
$type,
115115
-1,
116116
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
@@ -206,7 +206,7 @@ private function parseTypes(\ArrayIterator $tokens, Context $context, $parserCon
206206

207207
$tokens->next();
208208
} elseif ($parserContext === self::PARSER_IN_COLLECTION_EXPRESSION
209-
&& ($token === '>' || $token === ',')
209+
&& ($token === '>' || trim($token) === ',')
210210
) {
211211
break;
212212
} else {
@@ -411,7 +411,7 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
411411
$valueType = $this->parseTypes($tokens, $context, self::PARSER_IN_COLLECTION_EXPRESSION);
412412
$keyType = null;
413413

414-
if ($tokens->current() === ',') {
414+
if ($tokens->current() !== null && trim($tokens->current()) === ',') {
415415
// if we have a comma, then we just parsed the key type, not the value type
416416
$keyType = $valueType;
417417
if ($isArray) {

tests/unit/CollectionResolverTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,54 @@ public function testResolvingArrayCollectionWithKey()
147147
$this->assertInstanceOf(Types\Compound::class, $valueType);
148148
}
149149

150+
/**
151+
* @covers ::__construct
152+
* @covers ::resolve
153+
*
154+
* @uses \phpDocumentor\Reflection\Types\Context
155+
* @uses \phpDocumentor\Reflection\Types\Compound
156+
* @uses \phpDocumentor\Reflection\Types\Collection
157+
* @uses \phpDocumentor\Reflection\Types\String_
158+
*/
159+
public function testResolvingArrayCollectionWithKeyAndWhitespace()
160+
{
161+
$fixture = new TypeResolver();
162+
163+
/** @var Collection $resolvedType */
164+
$resolvedType = $fixture->resolve('array<string, object|array>', new Context(''));
165+
166+
$this->assertInstanceOf(Array_::class, $resolvedType);
167+
$this->assertSame('array<string,object|array>', (string) $resolvedType);
168+
169+
/** @var Array_ $valueType */
170+
$valueType = $resolvedType->getValueType();
171+
172+
/** @var Compound $keyType */
173+
$keyType = $resolvedType->getKeyType();
174+
175+
$this->assertInstanceOf(Types\String_::class, $keyType);
176+
$this->assertInstanceOf(Types\Compound::class, $valueType);
177+
}
178+
179+
/**
180+
* @covers ::__construct
181+
* @covers ::resolve
182+
*
183+
* @expectedException \InvalidArgumentException
184+
*
185+
* @uses \phpDocumentor\Reflection\Types\Context
186+
* @uses \phpDocumentor\Reflection\Types\Compound
187+
* @uses \phpDocumentor\Reflection\Types\Collection
188+
* @uses \phpDocumentor\Reflection\Types\String_
189+
*/
190+
public function testResolvingArrayCollectionWithKeyAndTooManyWhitespace()
191+
{
192+
$fixture = new TypeResolver();
193+
194+
/** @var Collection $resolvedType */
195+
$resolvedType = $fixture->resolve('array<string, object|array>', new Context(''));
196+
}
197+
150198
/**
151199
* @covers ::__construct
152200
* @covers ::resolve

0 commit comments

Comments
 (0)