1
0
mirror of https://github.com/nikic/PHP-Parser.git synced 2025-05-12 00:05:20 +02:00

Add support for the mixed type

This commit is contained in:
Máté Kocsis 2020-05-28 22:50:32 +02:00 committed by Nikita Popov
parent f33f081c8f
commit 32f89662f3
5 changed files with 24 additions and 5 deletions

@ -1,7 +1,9 @@
Version 4.4.1-dev Version 4.4.1-dev
----------------- -----------------
Nothing yet. ### Added
* Added support for the mixed type
Version 4.4.0 (2020-04-10) Version 4.4.0 (2020-04-10)
-------------------------- --------------------------

@ -183,7 +183,7 @@ final class BuilderHelpers
} }
$builtinTypes = [ $builtinTypes = [
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object' 'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed'
]; ];
$lowerType = strtolower($type); $lowerType = strtolower($type);
@ -197,6 +197,10 @@ final class BuilderHelpers
throw new \LogicException('void type cannot be nullable'); throw new \LogicException('void type cannot be nullable');
} }
if ($nullable && (string) $type === 'mixed') {
throw new \LogicException('mixed type cannot be nullable');
}
return $nullable ? new NullableType($type) : $type; return $nullable ? new NullableType($type) : $type;
} }

@ -648,7 +648,7 @@ abstract class ParserAbstract implements Parser
} }
protected function handleBuiltinTypes(Name $name) { protected function handleBuiltinTypes(Name $name) {
$scalarTypes = [ $builtinTypes = [
'bool' => true, 'bool' => true,
'int' => true, 'int' => true,
'float' => true, 'float' => true,
@ -658,6 +658,7 @@ abstract class ParserAbstract implements Parser
'object' => true, 'object' => true,
'null' => true, 'null' => true,
'false' => true, 'false' => true,
'mixed' => true,
]; ];
if (!$name->isUnqualified()) { if (!$name->isUnqualified()) {
@ -665,7 +666,7 @@ abstract class ParserAbstract implements Parser
} }
$lowerName = $name->toLowerString(); $lowerName = $name->toLowerString();
if (!isset($scalarTypes[$lowerName])) { if (!isset($builtinTypes[$lowerName])) {
return $name; return $name;
} }

@ -113,6 +113,7 @@ class ParamTest extends \PHPUnit\Framework\TestCase
['object', new Node\Identifier('object')], ['object', new Node\Identifier('object')],
['Array', new Node\Identifier('array')], ['Array', new Node\Identifier('array')],
['CALLABLE', new Node\Identifier('callable')], ['CALLABLE', new Node\Identifier('callable')],
['mixed', new Node\Identifier('mixed')],
['Some\Class', new Node\Name('Some\Class')], ['Some\Class', new Node\Name('Some\Class')],
['\Foo', new Node\Name\FullyQualified('Foo')], ['\Foo', new Node\Name\FullyQualified('Foo')],
['self', new Node\Name('self')], ['self', new Node\Name('self')],

@ -1,7 +1,7 @@
Scalar type declarations Scalar type declarations
----- -----
<?php <?php
function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e, object $f) : void {} function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e, object $f, mixed $g) : void {}
----- -----
!!php7 !!php7
array( array(
@ -77,6 +77,17 @@ array(
) )
default: null default: null
) )
6: Param(
type: Identifier(
name: mixed
)
byRef: false
variadic: false
var: Expr_Variable(
name: g
)
default: null
)
) )
returnType: Identifier( returnType: Identifier(
name: void name: void