Skip to content

Zend: Deprecate non-canonical cast names #19372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025
Merged
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
4 changes: 2 additions & 2 deletions Zend/tests/type_casts/cast_to_double.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
casting different variables to double
casting different variables to float
--INI--
precision=14
--FILE--
Expand Down Expand Up @@ -32,7 +32,7 @@ $vars = array(
);

foreach ($vars as $var) {
$tmp = (double)$var;
$tmp = (float)$var;
var_dump($tmp);
}

Expand Down
11 changes: 11 additions & 0 deletions Zend/tests/type_casts/non_canonical_binary_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Non canonical (binary) cast
--FILE--
<?php

var_dump((binary) 42);

?>
--EXPECTF--
Deprecated: Non-canonical cast (binary) is deprecated, use the (string) cast instead in %s on line %d
int(42)
11 changes: 11 additions & 0 deletions Zend/tests/type_casts/non_canonical_boolean_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Non canonical (boolean) cast
--FILE--
<?php

var_dump((boolean) 42);

?>
--EXPECTF--
Deprecated: Non-canonical cast (boolean) is deprecated, use the (bool) cast instead in %s on line %d
int(42)
11 changes: 11 additions & 0 deletions Zend/tests/type_casts/non_canonical_double_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Non canonical (double) cast
--FILE--
<?php

var_dump((double) 42);

?>
--EXPECTF--
Deprecated: Non-canonical cast (double) is deprecated, use the (float) cast instead in %s on line %d
int(42)
11 changes: 11 additions & 0 deletions Zend/tests/type_casts/non_canonical_integer_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Non canonical (integer) cast
--FILE--
<?php

var_dump((integer) "42");

?>
--EXPECTF--
Deprecated: Non-canonical cast (integer) is deprecated, use the (int) cast instead in %s on line %d
int(42)
2 changes: 1 addition & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
case IS_NULL: PREFIX_OP("(unset)", 240, 241);
case _IS_BOOL: PREFIX_OP("(bool)", 240, 241);
case IS_LONG: PREFIX_OP("(int)", 240, 241);
case IS_DOUBLE: PREFIX_OP("(double)", 240, 241);
case IS_DOUBLE: PREFIX_OP("(float)", 240, 241);
case IS_STRING: PREFIX_OP("(string)", 240, 241);
case IS_ARRAY: PREFIX_OP("(array)", 240, 241);
case IS_OBJECT: PREFIX_OP("(object)", 240, 241);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%token T_INC "'++'"
%token T_DEC "'--'"
%token T_INT_CAST "'(int)'"
%token T_DOUBLE_CAST "'(double)'"
%token T_DOUBLE_CAST "'(float)'"
%token T_STRING_CAST "'(string)'"
%token T_ARRAY_CAST "'(array)'"
%token T_OBJECT_CAST "'(object)'"
Expand Down
36 changes: 32 additions & 4 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -1629,14 +1629,28 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN_WITH_IDENT(T_VAR);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_INT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"|"float"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("integer"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (integer) is deprecated, use the (int) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("float"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_DOUBLE_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (double) is deprecated, use the (float) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"real"{TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_throw_exception(zend_ce_parse_error, "The (real) cast has been removed, use (float) instead", 0);
Expand All @@ -1645,10 +1659,17 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN(T_DOUBLE_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_STRING_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("binary"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (binary) is deprecated, use the (string) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
RETURN_TOKEN(T_ARRAY_CAST);
}
Expand All @@ -1657,10 +1678,17 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN(T_OBJECT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_BOOL_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("boolean"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (boolean) is deprecated, use the (bool) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}

<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_UNSET_CAST);
}
Expand Down
Loading