Skip to content

Relax final+private warning for trait methods with inherited final #17381

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

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 26 additions & 0 deletions Zend/tests/gh17214.phpt
Original file line number Diff line number Diff line change
@@ -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===
2 changes: 0 additions & 2 deletions Zend/tests/traits/gh12854.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading