Skip to content

Commit

Permalink
Add support for "never" type in the BuilderHelpers::normalizeType()
Browse files Browse the repository at this point in the history
  • Loading branch information
simivar authored and nikic committed Jun 18, 2021
1 parent e69ebbb commit c35cc4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/PhpParser/BuilderHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static function normalizeType($type) {
}

$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);
Expand All @@ -199,12 +199,11 @@ public static function normalizeType($type) {
$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;
Expand Down
7 changes: 7 additions & 0 deletions test/PhpParser/BuilderHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function testNormalizeType() {
$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));
Expand Down Expand Up @@ -161,6 +162,12 @@ public function testNormalizeTypeNullableMixed() {
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));
Expand Down

0 comments on commit c35cc4b

Please sign in to comment.