Skip to content

Commit

Permalink
Add support for the mixed type
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate authored and nikic committed May 28, 2020
1 parent f33f081 commit 32f8966
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
--------------------------
Expand Down
6 changes: 5 additions & 1 deletion lib/PhpParser/BuilderHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static function normalizeType($type) {
}

$builtinTypes = [
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object'
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed'
];

$lowerType = strtolower($type);
Expand All @@ -197,6 +197,10 @@ public static function normalizeType($type) {
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;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ protected function fixupStartAttributes(Node $to, Node $from) {
}

protected function handleBuiltinTypes(Name $name) {
$scalarTypes = [
$builtinTypes = [
'bool' => true,
'int' => true,
'float' => true,
Expand All @@ -658,14 +658,15 @@ protected function handleBuiltinTypes(Name $name) {
'object' => true,
'null' => true,
'false' => true,
'mixed' => true,
];

if (!$name->isUnqualified()) {
return $name;
}

$lowerName = $name->toLowerString();
if (!isset($scalarTypes[$lowerName])) {
if (!isset($builtinTypes[$lowerName])) {
return $name;
}

Expand Down
1 change: 1 addition & 0 deletions test/PhpParser/Builder/ParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function provideTestTypes() {
['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')],
Expand Down
13 changes: 12 additions & 1 deletion test/code/parser/stmt/function/builtinTypeDeclarations.test
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32f8966

Please sign in to comment.