1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Relax final+private warning for trait methods with inherited final

Fixes GH-17214
Closes GH-17381
This commit is contained in:
Ilija Tovilo
2025-01-06 15:18:00 +01:00
parent d08a9e0010
commit a6a290d541
4 changed files with 33 additions and 5 deletions

2
NEWS
View File

@@ -7,6 +7,8 @@ PHP NEWS
with 0x0b). (nielsdos)
. Fixed bug GH-16886 (ini_parse_quantity() fails to emit warning for 0x+0).
(nielsdos)
. Fixed bug GH-17214 (Relax final+private warning for trait methods with
inherited final). (ilutov)
- Enchant:
. Fix crashes in enchant when passing null bytes. (nielsdos)

26
Zend/tests/gh17214.phpt Normal file
View File

@@ -0,0 +1,26 @@
--TEST--
GH-17214: Relax final+private warning for trait methods with inherited final
--FILE--
<?php
trait MyTrait
{
final protected function someMethod(): void {}
}
class Test
{
use MyTrait {
someMethod as private anotherMethod;
}
public function __construct()
{
$this->anotherMethod();
}
}
?>
===DONE===
--EXPECT--
===DONE===

View File

@@ -39,8 +39,6 @@ foreach (['pub', 'prot', 'priv', 'final1', 'final2', 'final3'] as $method) {
?>
--EXPECTF--
Warning: Private methods cannot be final as they are never overridden by other classes in %s on line %d
Warning: Private methods cannot be final as they are never overridden by other classes in %s on line %d
--- Method: pub ---
bool(true)

View File

@@ -2048,9 +2048,11 @@ static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /*
static void zend_traits_check_private_final_inheritance(uint32_t original_fn_flags, zend_function *fn_copy, zend_string *name)
{
/* If the function was originally already private+final, then it will have already been warned about.
* If the function became private+final only after applying modifiers, we need to emit the same warning. */
if ((original_fn_flags & (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) != (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
/* If the function was originally already private+final, then it will have
* already been warned about. Only emit this error when the used trait method
* explicitly became final, avoiding errors for `as private` where it was
* already final. */
if (!(original_fn_flags & ZEND_ACC_FINAL)
&& (fn_copy->common.fn_flags & (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) == (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
&& !zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");