From 32f89662f34cf33b4098cadc8a67eb05d9fff9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Thu, 28 May 2020 22:50:32 +0200 Subject: [PATCH] Add support for the mixed type --- CHANGELOG.md | 4 +++- lib/PhpParser/BuilderHelpers.php | 6 +++++- lib/PhpParser/ParserAbstract.php | 5 +++-- test/PhpParser/Builder/ParamTest.php | 1 + .../stmt/function/builtinTypeDeclarations.test | 13 ++++++++++++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f301698..231fbe0 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 b745441..180bf35 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 80dc560..2986643 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 dc2bb2e..781fffa 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 b90fd01..b65904e 100644 --- a/test/code/parser/stmt/function/builtinTypeDeclarations.test +++ b/test/code/parser/stmt/function/builtinTypeDeclarations.test @@ -1,7 +1,7 @@ Scalar type declarations -----