Skip to content

Commit 6ba55c8

Browse files
iluuu1994charmitro
authored andcommitted
Relax final+private warning for trait methods with inherited final
Fixes phpGH-17214 Closes phpGH-17381
1 parent 4e8fc6c commit 6ba55c8

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
with 0x0b). (nielsdos)
88
. Fixed bug GH-16886 (ini_parse_quantity() fails to emit warning for 0x+0).
99
(nielsdos)
10+
. Fixed bug GH-17214 (Relax final+private warning for trait methods with
11+
inherited final). (ilutov)
1012

1113
- Enchant:
1214
. Fix crashes in enchant when passing null bytes. (nielsdos)

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
@@ -2358,9 +2358,11 @@ static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /*
23582358

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

0 commit comments

Comments
 (0)