Skip to content

Commit aec4c0f

Browse files
kocsismateDanack
andcommitted
Add support for the mixed type
RFC: https://wiki.php.net/rfc/mixed_type_v2 Closes GH-5313 Co-authored-by: Dan Ackroyd <danack@basereality.com>
1 parent 4bc1d83 commit aec4c0f

File tree

55 files changed

+927
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+927
-15
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a mixed parameter type can't be overridden by a built-in type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(bool $a) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Declaration of Bar::method(bool $a) must be compatible with Foo::method(mixed $a) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a mixed parameter type can't be overridden by a nullable built-in type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(?int $a) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Declaration of Bar::method(?int $a) must be compatible with Foo::method(mixed $a) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a mixed parameter type can't be overridden by a union of all built-in types
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(bool|int|float|string|array|object|null $a) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Declaration of Bar::method(object|array|string|int|float|bool|null $a) must be compatible with Foo::method(mixed $a) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a mixed parameter type can't be overridden by a union type of classes
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(stdClass|Foo $a) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Declaration of Bar::method(stdClass|Foo $a) must be compatible with Foo::method(mixed $a) in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a mixed parameter type supports invariance
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a mixed parameter type can be overridden by no type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(mixed $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method($a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a parameter of no type can be overridden by the mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method($a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a parameter of a built-in type can be overridden by the mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(int $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a parameter of a nullable built-in type can be overridden by the mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(?int $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a parameter of a union of all built-in types can be overridden by the mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(bool|int|float|string|array|object|null $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a parameter of a union type of classes can be overridden by the mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public function method(stdClass|Foo $a) {}
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public function method(mixed $a) {}
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of mixed type can't be overridden by a property of a built-in type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public int $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be mixed (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of mixed type can't be overridden by a property of class type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public stdClass $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be mixed (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of mixed type can't be overridden by an untyped property
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be mixed (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of mixed type can't be overridden by a property of a union type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public bool|int|float|string|array|object|null $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be mixed (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of a built-in type can't be overridden by a property of mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public int $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public mixed $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be int (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of class type can't be overridden by a property of mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public stdClass $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public mixed $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be stdClass (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that an untyped property can't be overridden by a property of mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public mixed $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must not be defined (as in class Foo) in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a property of a union type can't be overridden by a property of mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public bool|int|float|string|array|object|null $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public mixed $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Type of Bar::$property1 must be object|array|string|int|float|bool|null (as in class Foo) in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a property of mixed property type can be overridden by a property of mixed type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
public mixed $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
public mixed $property1;
14+
}
15+
16+
?>
17+
--EXPECT--

0 commit comments

Comments
 (0)