Skip to content

Throw exception on invalid array start #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2022
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
7 changes: 6 additions & 1 deletion src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use function class_exists;
use function class_implements;
use function count;
use function current;
use function end;
use function in_array;
use function key;
Expand Down Expand Up @@ -273,6 +274,10 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
} elseif ($token === self::OPERATOR_ARRAY) {
end($types);
$last = key($types);
if ($last === null) {
throw new InvalidArgumentException('Unexpected array operator');
}

$lastItem = $types[$last];
if ($lastItem instanceof Expression) {
$lastItem = $lastItem->getValueType();
Expand Down Expand Up @@ -317,7 +322,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
);
}
} elseif (count($types) === 1) {
return $types[0];
return current($types);
}

if ($compoundToken === '|') {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,19 @@ public function testExceptionIsThrownIfTypeIsEmpty(): void
$fixture->resolve(' ', new Context(''));
}

/**
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::__construct
* @covers ::resolve
*/
public function testInvalidArrayOperator(): void
{
$this->expectException('InvalidArgumentException');
$fixture = new TypeResolver();
$fixture->resolve('[]', new Context(''));
}

/**
* Returns a list of keywords and expected classes that are created from them.
*
Expand Down