php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60165 Overriding unexisting trait should throw/trigger the exception/error
Submitted: 2011-10-28 21:23 UTC Modified: 2011-11-17 21:04 UTC
From: fruit dot dev at gmail dot com Assigned: gron (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0beta2 OS: Fedora 14
Private report: No CVE-ID: None
 [2011-10-28 21:23 UTC] fruit dot dev at gmail dot com
Description:
------------
In case, when user overrides invalid traits method, PHP should check whether specified method belongs to given trait.

The code given below is valid for preprocessing. Meanwhile trait "A" does not have method "getTitle", as well as trait "B" does contains "getSlug" method.

I guess, PHP should trigger error telling about the user is entangled among the three pines.

Test script:
---------------
  trait A
  {
    public function getSlug ()
    {
      return $this->slug;
    }
  }

  trait B
  {
    public function getTitle ()
    {
      return $this->title;
    }
  }

  class Foo
  {
    protected $slug, $title;

    use A, B
    {
      A::getTitle as title;
      B::getSlug as slug;
    }
  }

  $object = new Foo();

Expected result:
----------------
Error/exception should be triggered/thrown

Actual result:
--------------
silence (no errors was shown)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-16 01:16 UTC] felipe@php.net
-Status: Open +Status: Assigned -Package: Class/Object related +Package: Scripting Engine problem -Assigned To: +Assigned To: gron
 [2011-11-16 18:59 UTC] gron@php.net
Thanks for the reminder.
The patch is below.
As soon as I find another half an hour, I will add the necessary tests and 
commit.

Best regards
Stefan

--- Zend/zend_compile.c	(revision 319357)
+++ Zend/zend_compile.c	(working copy)
@@ -4036,6 +4036,8 @@
 	size_t i, j = 0;
 	zend_trait_precedence *cur_precedence;
 	zend_trait_method_reference *cur_method_ref;
+	char *lcname;
+	bool aliased_method_exists;
 
 	/* resolve class references */
 	if (ce->trait_precedences) {
@@ -4064,6 +4066,15 @@
 			if (ce->trait_aliases[i]->trait_method->class_name) {
 				cur_method_ref = ce->trait_aliases[i]-
>trait_method;
 				cur_method_ref->ce = 
zend_fetch_class(cur_method_ref->class_name, cur_method_ref->cname_len, 
ZEND_FETCH_CLASS_TRAIT TSRMLS_CC);
+
+				/** Ensure that this reference is resolvable */
+				lcname = zend_str_tolower_dup(cur_method_ref-
>method_name, cur_method_ref->mname_len);
+				aliased_method_exists = 
zend_hash_exists(&cur_method_ref->ce->function_table, lcname, cur_method_ref-
>mname_len + 1);
+				efree(lcname);
+
+				if (!aliased_method_exists) {
+					zend_error(E_COMPILE_ERROR, "An alias 
was defined for %s::%s but this method does not exist", cur_method_ref->ce-
>name, cur_method_ref->method_name);
+				}
 			}
 			i++;
 		}
 [2011-11-17 21:04 UTC] gron@php.net
Automatic comment from SVN on behalf of gron
Revision: http://svn.php.net/viewvc/?view=revision&revision=319420
Log: Fixed Bug #60165 (Aliasing unexisting trait should throw/trigger the exception/error)

- aliases that are not actually matching anything are treated as errors now. This
  will make sure that all methods that are expected to be in a class are actually
  there, or in case a trait changed for instance, that the code breaks already
  on composition
- Precedence declarations are also checked to ensure that the method
  which is supposed to take precedence actually exists, however,
  the other traits mentioned in the declaration are not regarded.
  We are more lenient here, since this avoids unnecessary fragility.
- fixed another seamingly unrelated test which broke in the progress
  but wasn't clear before either.
 [2011-11-17 21:04 UTC] gron@php.net
-Status: Assigned +Status: Closed
 [2011-11-17 21:04 UTC] gron@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed with SVN rev. 319420.
 [2012-04-18 09:47 UTC] laruence@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=5ef2c328226d2157e2ccab456e4f24e9022936f8
Log: Fixed Bug #60165 (Aliasing unexisting trait should throw/trigger the exception/error)
 [2012-07-24 23:38 UTC] rasmus@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=5ef2c328226d2157e2ccab456e4f24e9022936f8
Log: Fixed Bug #60165 (Aliasing unexisting trait should throw/trigger the exception/error)
 [2013-11-17 09:35 UTC] laruence@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=5ef2c328226d2157e2ccab456e4f24e9022936f8
Log: Fixed Bug #60165 (Aliasing unexisting trait should throw/trigger the exception/error)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 14:01:29 2024 UTC