Skip to content

Commit 0e1fbf9

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Relax final+private warning for trait methods with inherited final
2 parents a06668a + 3c13864 commit 0e1fbf9

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Zend/tests/gh17214.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-17214: Relax final+private warning for trait methods with inherited final
3+
--FILE--
4+
<?php
5+
6+
trait MyTrait
7+
{
8+
final protected function someMethod(): void {}
9+
}
10+
11+
class Test
12+
{
13+
use MyTrait {
14+
someMethod as private anotherMethod;
15+
}
16+
17+
public function __construct()
18+
{
19+
$this->anotherMethod();
20+
}
21+
}
22+
23+
?>
24+
===DONE===
25+
--EXPECT--
26+
===DONE===

Zend/tests/traits/gh12854.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ foreach (['pub', 'prot', 'priv', 'final1', 'final2', 'final3'] as $method) {
3939

4040
?>
4141
--EXPECTF--
42-
Warning: Private methods cannot be final as they are never overridden by other classes in %s on line %d
43-
4442
Warning: Private methods cannot be final as they are never overridden by other classes in %s on line %d
4543
--- Method: pub ---
4644
bool(true)

Zend/zend_inheritance.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,9 +2384,11 @@ static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /*
23842384

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

0 commit comments

Comments
 (0)