Skip to content

Commit 32f8966

Browse files
kocsismatenikic
authored andcommitted
Add support for the mixed type
1 parent f33f081 commit 32f8966

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Version 4.4.1-dev
22
-----------------
33

4-
Nothing yet.
4+
### Added
5+
6+
* Added support for the mixed type
57

68
Version 4.4.0 (2020-04-10)
79
--------------------------

lib/PhpParser/BuilderHelpers.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static function normalizeType($type) {
183183
}
184184

185185
$builtinTypes = [
186-
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object'
186+
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed'
187187
];
188188

189189
$lowerType = strtolower($type);
@@ -197,6 +197,10 @@ public static function normalizeType($type) {
197197
throw new \LogicException('void type cannot be nullable');
198198
}
199199

200+
if ($nullable && (string) $type === 'mixed') {
201+
throw new \LogicException('mixed type cannot be nullable');
202+
}
203+
200204
return $nullable ? new NullableType($type) : $type;
201205
}
202206

lib/PhpParser/ParserAbstract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ protected function fixupStartAttributes(Node $to, Node $from) {
648648
}
649649

650650
protected function handleBuiltinTypes(Name $name) {
651-
$scalarTypes = [
651+
$builtinTypes = [
652652
'bool' => true,
653653
'int' => true,
654654
'float' => true,
@@ -658,14 +658,15 @@ protected function handleBuiltinTypes(Name $name) {
658658
'object' => true,
659659
'null' => true,
660660
'false' => true,
661+
'mixed' => true,
661662
];
662663

663664
if (!$name->isUnqualified()) {
664665
return $name;
665666
}
666667

667668
$lowerName = $name->toLowerString();
668-
if (!isset($scalarTypes[$lowerName])) {
669+
if (!isset($builtinTypes[$lowerName])) {
669670
return $name;
670671
}
671672

test/PhpParser/Builder/ParamTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function provideTestTypes() {
113113
['object', new Node\Identifier('object')],
114114
['Array', new Node\Identifier('array')],
115115
['CALLABLE', new Node\Identifier('callable')],
116+
['mixed', new Node\Identifier('mixed')],
116117
['Some\Class', new Node\Name('Some\Class')],
117118
['\Foo', new Node\Name\FullyQualified('Foo')],
118119
['self', new Node\Name('self')],

test/code/parser/stmt/function/builtinTypeDeclarations.test

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Scalar type declarations
22
-----
33
<?php
4-
function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e, object $f) : void {}
4+
function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e, object $f, mixed $g) : void {}
55
-----
66
!!php7
77
array(
@@ -77,6 +77,17 @@ array(
7777
)
7878
default: null
7979
)
80+
6: Param(
81+
type: Identifier(
82+
name: mixed
83+
)
84+
byRef: false
85+
variadic: false
86+
var: Expr_Variable(
87+
name: g
88+
)
89+
default: null
90+
)
8091
)
8192
returnType: Identifier(
8293
name: void

0 commit comments

Comments
 (0)