diff --git a/lib/PhpParser/BuilderHelpers.php b/lib/PhpParser/BuilderHelpers.php index f5bfae9ee5..c6d8f16136 100644 --- a/lib/PhpParser/BuilderHelpers.php +++ b/lib/PhpParser/BuilderHelpers.php @@ -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); @@ -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; diff --git a/test/PhpParser/BuilderHelpersTest.php b/test/PhpParser/BuilderHelpersTest.php index 9376cda461..da6348f6db 100644 --- a/test/PhpParser/BuilderHelpersTest.php +++ b/test/PhpParser/BuilderHelpersTest.php @@ -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)); @@ -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));