Add support for "never" type in the BuilderHelpers::normalizeType()

This commit is contained in:
simivar 2021-06-18 17:04:21 +02:00 committed by Nikita Popov
parent e69ebbbfd9
commit c35cc4b2cb
2 changed files with 13 additions and 7 deletions

View File

@ -189,7 +189,7 @@ final class BuilderHelpers
}
$builtinTypes = [
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed'
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed', 'never',
];
$lowerType = strtolower($type);
@ -199,12 +199,11 @@ final class BuilderHelpers
$type = self::normalizeName($type);
}
if ($nullable && (string) $type === 'void') {
throw new \LogicException('void type cannot be nullable');
}
if ($nullable && (string) $type === 'mixed') {
throw new \LogicException('mixed type cannot be nullable');
$notNullableTypes = [
'void', 'mixed', 'never',
];
if ($nullable && in_array((string) $type, $notNullableTypes)) {
throw new \LogicException(sprintf('%s type cannot be nullable', $type));
}
return $nullable ? new NullableType($type) : $type;

View File

@ -126,6 +126,7 @@ class BuilderHelpersTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(new Node\Identifier('void'), BuilderHelpers::normalizeType('void'));
$this->assertEquals(new Node\Identifier('object'), BuilderHelpers::normalizeType('object'));
$this->assertEquals(new Node\Identifier('mixed'), BuilderHelpers::normalizeType('mixed'));
$this->assertEquals(new Node\Identifier('never'), BuilderHelpers::normalizeType('never'));
$intIdentifier = new Node\Identifier('int');
$this->assertSame($intIdentifier, BuilderHelpers::normalizeType($intIdentifier));
@ -161,6 +162,12 @@ class BuilderHelpersTest extends \PHPUnit\Framework\TestCase
BuilderHelpers::normalizeType('?mixed');
}
public function testNormalizeTypeNullableNever() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('never type cannot be nullable');
BuilderHelpers::normalizeType('?never');
}
public function testNormalizeValue() {
$expression = new Scalar\LNumber(1);
$this->assertSame($expression, BuilderHelpers::normalizeValue($expression));