Skip to content

Commit bb6f5c9

Browse files
committed
Add support for the mixed type
1 parent 51a305d commit bb6f5c9

19 files changed

+299
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Test that a mixed casting is not supported
3+
--FILE--
4+
<?php
5+
6+
$foo = (mixed) 12;
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected '12' (T_LNUMBER) in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test that the mixed parameter type is not valid with union types
3+
--FILE--
4+
<?php
5+
6+
function foo(mixed|int|null $a)
7+
{
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Duplicate type int is redundant in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Test that mixed is a valid parameter type
3+
--FILE--
4+
<?php
5+
6+
function foo(mixed $a)
7+
{
8+
}
9+
10+
?>
11+
--EXPECT--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Test that the mixed return type is not valid with union types
3+
--FILE--
4+
<?php
5+
6+
function foo(): mixed|string|null
7+
{
8+
return null;
9+
}
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Duplicate type string is redundant in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test that mixed is a valid return type
3+
--FILE--
4+
<?php
5+
6+
function foo(): mixed
7+
{
8+
return null;
9+
}
10+
11+
?>
12+
--EXPECT--
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Test that the mixed parameter type accepts any kind of arguments in strict mode
3+
--FILE--
4+
<?php
5+
declare(strict_types=1);
6+
7+
function foo(mixed $a)
8+
{
9+
}
10+
11+
foo(null);
12+
foo(false);
13+
foo(1);
14+
foo(3.14);
15+
foo("");
16+
foo([]);
17+
foo(new stdClass());
18+
19+
?>
20+
--EXPECT--
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test that the mixed parameter type accepts any kind of arguments in weak mode
3+
--FILE--
4+
<?php
5+
6+
function foo(mixed $a)
7+
{
8+
}
9+
10+
foo(null);
11+
foo(false);
12+
foo(1);
13+
foo(3.14);
14+
foo("");
15+
foo([]);
16+
foo(new stdClass());
17+
18+
?>
19+
--EXPECT--
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test that the mixed property type accepts any kind of value in strict mode
3+
--FILE--
4+
<?php
5+
declare(strict_types=1);
6+
7+
class Foo
8+
{
9+
public mixed $property1;
10+
public mixed $property2 = null;
11+
public mixed $property3 = false;
12+
public mixed $property4 = true;
13+
public mixed $property5 = 1;
14+
public mixed $property6 = 3.14;
15+
public mixed $property7 = "foo";
16+
public mixed $property8 = [];
17+
public mixed $property9;
18+
19+
public function __construct()
20+
{
21+
$this->property9 = fopen(__FILE__, "r");
22+
$this->property9 = new stdClass();
23+
}
24+
}
25+
26+
$foo = new Foo();
27+
28+
try {
29+
$foo->property1;
30+
} catch (Error $exception) {
31+
echo $exception->getMessage() . "\n";
32+
}
33+
34+
?>
35+
--EXPECT--
36+
Typed property Foo::$property1 must not be accessed before initialization
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test that the mixed property type accepts any kind of value
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
public mixed $property2 = null;
10+
public mixed $property3 = false;
11+
public mixed $property4 = true;
12+
public mixed $property5 = 1;
13+
public mixed $property6 = 3.14;
14+
public mixed $property7 = "foo";
15+
public mixed $property8 = [];
16+
public mixed $property9;
17+
18+
public function __construct()
19+
{
20+
$this->property9 = fopen(__FILE__, "r");
21+
$this->property9 = new stdClass();
22+
}
23+
}
24+
25+
$foo = new Foo();
26+
27+
try {
28+
$foo->property1;
29+
} catch (Error $exception) {
30+
echo $exception->getMessage() . "\n";
31+
}
32+
33+
?>
34+
--EXPECT--
35+
Typed property Foo::$property1 must not be accessed before initialization
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test that the mixed property type accepts any kind of value in weak mode
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
public mixed $property2 = null;
10+
public mixed $property3 = false;
11+
public mixed $property4 = true;
12+
public mixed $property5 = 1;
13+
public mixed $property6 = 3.14;
14+
public mixed $property7 = "foo";
15+
public mixed $property8 = [];
16+
public mixed $property9;
17+
18+
public function __construct()
19+
{
20+
$this->property9 = fopen(__FILE__, "r");
21+
$this->property9 = new stdClass();
22+
}
23+
}
24+
25+
$foo = new Foo();
26+
27+
try {
28+
$foo->property1;
29+
} catch (Error $exception) {
30+
echo $exception->getMessage() . "\n";
31+
}
32+
33+
?>
34+
--EXPECT--
35+
Typed property Foo::$property1 must not be accessed before initialization

0 commit comments

Comments
 (0)