diff --git a/CHANGELOG.md b/CHANGELOG.md index f3016984..231fbe08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ Version 4.4.1-dev ----------------- -Nothing yet. +### Added + +* Added support for the mixed type Version 4.4.0 (2020-04-10) -------------------------- diff --git a/lib/PhpParser/BuilderHelpers.php b/lib/PhpParser/BuilderHelpers.php index b745441e..180bf35d 100644 --- a/lib/PhpParser/BuilderHelpers.php +++ b/lib/PhpParser/BuilderHelpers.php @@ -183,7 +183,7 @@ final class BuilderHelpers } $builtinTypes = [ - 'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object' + 'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed' ]; $lowerType = strtolower($type); @@ -197,6 +197,10 @@ final class BuilderHelpers 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; } diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index 80dc560f..29866430 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -648,7 +648,7 @@ abstract class ParserAbstract implements Parser } protected function handleBuiltinTypes(Name $name) { - $scalarTypes = [ + $builtinTypes = [ 'bool' => true, 'int' => true, 'float' => true, @@ -658,6 +658,7 @@ abstract class ParserAbstract implements Parser 'object' => true, 'null' => true, 'false' => true, + 'mixed' => true, ]; if (!$name->isUnqualified()) { @@ -665,7 +666,7 @@ abstract class ParserAbstract implements Parser } $lowerName = $name->toLowerString(); - if (!isset($scalarTypes[$lowerName])) { + if (!isset($builtinTypes[$lowerName])) { return $name; } diff --git a/test/PhpParser/Builder/ParamTest.php b/test/PhpParser/Builder/ParamTest.php index dc2bb2e9..781fffa6 100644 --- a/test/PhpParser/Builder/ParamTest.php +++ b/test/PhpParser/Builder/ParamTest.php @@ -113,6 +113,7 @@ class ParamTest extends \PHPUnit\Framework\TestCase ['object', new Node\Identifier('object')], ['Array', new Node\Identifier('array')], ['CALLABLE', new Node\Identifier('callable')], + ['mixed', new Node\Identifier('mixed')], ['Some\Class', new Node\Name('Some\Class')], ['\Foo', new Node\Name\FullyQualified('Foo')], ['self', new Node\Name('self')], diff --git a/test/code/parser/stmt/function/builtinTypeDeclarations.test b/test/code/parser/stmt/function/builtinTypeDeclarations.test index b90fd019..b65904e5 100644 --- a/test/code/parser/stmt/function/builtinTypeDeclarations.test +++ b/test/code/parser/stmt/function/builtinTypeDeclarations.test @@ -1,7 +1,7 @@ Scalar type declarations ----- <?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 array( @@ -77,6 +77,17 @@ array( ) default: null ) + 6: Param( + type: Identifier( + name: mixed + ) + byRef: false + variadic: false + var: Expr_Variable( + name: g + ) + default: null + ) ) returnType: Identifier( name: void