Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Zend/tests/type_aliases/basic_type_alias.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Basic type alias functionality
--FILE--
<?php
use type int|float as Number;
use type string as Text;
use type ?int as OptionalInt;
use type Number|string as Scalar;

function add(Number $a, Number $b): Number {
return $a + $b;
}

function greet(Text $name): Text {
return "Hello, " . $name;
}

function maybeInt(OptionalInt $value): OptionalInt {
return $value;
}

function identity(Scalar $v): Scalar {
return $v;
}

// Test int|float alias
var_dump(add(1, 2));
var_dump(add(1.5, 2.5));
var_dump(add(1, 2.5));

// Test string alias
var_dump(greet("World"));

// Test nullable alias
var_dump(maybeInt(42));
var_dump(maybeInt(null));

// Test nested alias (Scalar = Number|string = int|float|string)
var_dump(identity(42));
var_dump(identity(3.14));
var_dump(identity("hello"));

echo "Done\n";
?>
--EXPECT--
int(3)
float(4)
float(3.5)
string(12) "Hello, World"
int(42)
NULL
int(42)
float(3.14)
string(5) "hello"
Done
9 changes: 9 additions & 0 deletions Zend/tests/type_aliases/error_duplicate_alias.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Type alias name cannot be used twice
--FILE--
<?php
use type int as Number;
use type float as Number;
?>
--EXPECTF--
Fatal error: Cannot use type alias 'Number' because the name is already in use in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_reserved_type_name_int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot use reserved type name (int)
--FILE--
<?php
use type int|float as int;
?>
--EXPECTF--
Fatal error: Cannot use 'int' as type alias name as it is reserved in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_reserved_type_name_mixed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot use reserved type name (mixed)
--FILE--
<?php
use type int|string as mixed;
?>
--EXPECTF--
Fatal error: Cannot use 'mixed' as type alias name as it is reserved in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_reserved_type_name_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot use reserved type name (string)
--FILE--
<?php
use type int as string;
?>
--EXPECTF--
Fatal error: Cannot use 'string' as type alias name as it is reserved in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_reserved_type_name_void.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot use reserved type name (void)
--FILE--
<?php
use type int as void;
?>
--EXPECTF--
Fatal error: Cannot use 'void' as type alias name as it is reserved in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_self_referential.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot reference itself
--FILE--
<?php
use type Foo|int as Foo;
?>
--EXPECTF--
Fatal error: Type alias 'Foo' cannot reference itself in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_self_referential_dnf.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot reference itself in DNF type
--FILE--
<?php
use type Bar|(Foo&Baz) as Foo;
?>
--EXPECTF--
Fatal error: Type alias 'Foo' cannot reference itself in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/error_self_referential_union.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Type alias cannot reference itself in union type
--FILE--
<?php
use type int|string|MyType as MyType;
?>
--EXPECTF--
Fatal error: Type alias 'MyType' cannot reference itself in %s on line %d
42 changes: 42 additions & 0 deletions Zend/tests/type_aliases/include_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
include types functionality
--FILE--
<?php
include types 'types.php';

function add(Number $a, Number $b): Number {
return $a + $b;
}

function identity(Scalar $v): Scalar {
return $v;
}

function greet(OptionalString $name): OptionalString {
return $name ? "Hello, " . $name : null;
}

// Test Number alias (int|float)
var_dump(add(1, 2));
var_dump(add(1.5, 2.5));

// Test Scalar alias (Number|string = int|float|string)
var_dump(identity(42));
var_dump(identity(3.14));
var_dump(identity("hello"));

// Test OptionalString alias (?string)
var_dump(greet("World"));
var_dump(greet(null));

echo "Done\n";
?>
--EXPECT--
int(3)
float(4)
int(42)
float(3.14)
string(5) "hello"
string(12) "Hello, World"
NULL
Done
8 changes: 8 additions & 0 deletions Zend/tests/type_aliases/include_types_invalid_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
include types with invalid file (contains non-type-alias code)
--FILE--
<?php
include types 'invalid_types.php';
?>
--EXPECTF--
Fatal error: include types: Types file 'invalid_types.php' must only contain type aliases (use type ... as ...;) in %s on line %d
2 changes: 2 additions & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ enum _zend_ast_kind {
ZEND_AST_LABEL,
ZEND_AST_REF,
ZEND_AST_HALT_COMPILER,
ZEND_AST_INCLUDE_TYPES,
ZEND_AST_ECHO,
ZEND_AST_THROW,
ZEND_AST_GOTO,
Expand Down Expand Up @@ -146,6 +147,7 @@ enum _zend_ast_kind {
ZEND_AST_METHOD_REFERENCE,
ZEND_AST_NAMESPACE,
ZEND_AST_USE_ELEM,
ZEND_AST_TYPE_ALIAS,
ZEND_AST_TRAIT_ALIAS,
ZEND_AST_GROUP_USE,
ZEND_AST_ATTRIBUTE,
Expand Down
Loading
Loading