Skip to content

Commit c4e0557

Browse files
Update based on feedback
* Remove special messages for `never` parameters with default values or on property hooks * Reword error message for method with a body * Simplify some tests * Reduplicate registration of backed enum methods
1 parent 5806761 commit c4e0557

File tree

7 files changed

+21
-30
lines changed

7 files changed

+21
-30
lines changed

Zend/tests/never-parameters/backed_enum.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ function enumMethod(string $class, string $name) {
1515

1616
$type = $param->getType();
1717

18-
if (!($type instanceof ReflectionNamedType)) {
19-
throw new Exception("Should be named...");
20-
}
18+
assert($type instanceof ReflectionNamedType);
2119
echo "Name: " . $type->getName() . "\n";
2220
echo "isBuiltin: " . $type->isBuiltIn() . "\n";
2321
echo "allowsNull: " . (int)$type->allowsNull() . "\n";

Zend/tests/never-parameters/no_implementation.phpt renamed to Zend/tests/never-parameters/no_body.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
`never` parameter types - cannot be used for parameters on methods with implementations
2+
`never` parameter types - cannot be used for parameters on methods with bodies
33
--FILE--
44
<?php
55

@@ -11,4 +11,4 @@ abstract class Demo {
1111

1212
?>
1313
--EXPECTF--
14-
Fatal error: never cannot be used as a parameter type for methods with implementations in %s on line %d
14+
Fatal error: Function Demo::example() containing a body cannot use never as a parameter type in %s on line %d

Zend/tests/never-parameters/no_defaults.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ interface Demo {
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: never cannot be used as a parameter type for parameters with defaults in %s on line %d
12+
Fatal error: Cannot use int as default value for parameter $v of type never in %s on line %d

Zend/tests/never-parameters/no_hooks.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ interface I
1010

1111
?>
1212
--EXPECTF--
13-
Fatal error: never cannot be used as a parameter type for property hooks in %s on line %d
13+
Fatal error: Type of parameter $value of hook I::$both::set must be compatible with property type in %s on line %d

Zend/tests/never-parameters/reflection.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ echo $ref . "\n";
1313

1414
$type = $ref->getType();
1515

16-
if (!($type instanceof ReflectionNamedType)) {
17-
throw new Exception("Should be named...");
18-
}
16+
assert($type instanceof ReflectionNamedType);
1917
echo "Name: " . $type->getName() . "\n";
2018
echo "isBuiltin: " . $type->isBuiltIn() . "\n";
2119
echo "allowsNull: " . (int)$type->allowsNull() . "\n";

Zend/zend_compile.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7716,21 +7716,18 @@ static void zend_compile_params(zend_ast_decl *decl, uint32_t fallback_return_ty
77167716
if (decl->child[2] != NULL) {
77177717
zend_error_noreturn(
77187718
E_COMPILE_ERROR,
7719-
"never cannot be used as a parameter type for methods with implementations"
7720-
);
7721-
}
7722-
if (*default_ast_ptr) {
7723-
zend_error_noreturn(
7724-
E_COMPILE_ERROR,
7725-
"never cannot be used as a parameter type for parameters with defaults"
7726-
);
7727-
}
7728-
if (decl->kind == ZEND_AST_PROPERTY_HOOK) {
7729-
zend_error_noreturn(
7730-
E_COMPILE_ERROR,
7731-
"never cannot be used as a parameter type for property hooks"
7719+
"Function %s::%s() containing a body cannot use never as a parameter type",
7720+
ZSTR_VAL(op_array->scope->name),
7721+
ZSTR_VAL(op_array->function_name)
77327722
);
77337723
}
7724+
/* The restriction on not using `never` parameters for
7725+
* parameters with defaults is implemented by the validation of
7726+
* default values (since no value is valid for a `never` type).
7727+
* The restriction on not using `never` parameters for property
7728+
* hooks is implemented by the validation that the type of
7729+
* parameters accepted by the `set` hook is wider than that of
7730+
* the property itself. */
77347731
}
77357732

77367733
if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable

Zend/zend_enum.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,11 @@ ZEND_API zend_class_entry *zend_register_internal_enum(
564564
ce, unit_enum_methods, &ce->function_table, EG(current_module)->type);
565565
zend_class_implements(ce, 1, zend_ce_unit_enum);
566566
} else {
567-
if (type == IS_LONG) {
568-
zend_register_functions(
569-
ce, int_backed_enum_methods, &ce->function_table, EG(current_module)->type);
570-
} else {
571-
zend_register_functions(
572-
ce, string_backed_enum_methods, &ce->function_table, EG(current_module)->type);
573-
}
567+
zend_function_entry *backed_enum_methods = type == IS_LONG
568+
? int_backed_enum_methods
569+
: string_backed_enum_methods;
570+
zend_register_functions(
571+
ce, backed_enum_methods, &ce->function_table, EG(current_module)->type);
574572
zend_class_implements(ce, 1, zend_ce_backed_enum);
575573
}
576574

0 commit comments

Comments
 (0)