php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73195 ReflectionClass::getMethod() filter arguments doesn't accept combinations
Submitted: 2016-09-28 09:35 UTC Modified: 2016-09-29 10:13 UTC
From: grgtvs at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Reflection related
PHP Version: Irrelevant OS: GNU/Linux
Private report: No CVE-ID: None
 [2016-09-28 09:35 UTC] grgtvs at gmail dot com
Description:
------------
It seems that when using filters argument with combinations of the constants  ReflectionMethod::IS_STATIC, ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, ReflectionMethod::IS_PRIVATE, ReflectionMethod::IS_ABSTRACT, ReflectionMethod::IS_FINAL - 

e.g `$reflectionClassObj->getMethod(~ReflectionMethod::IS_PUBLIC)`
or  `$reflectionClassObj->getMethod(ReflectionMethod::IS_PUBLIC & ~ReflectionMethod::IS_STATIC)` etc.

the method tends to return every method as it would do without defining any filters.

I've tried this on PHP 5.5.9 as well as at 7.0.8 and the result is the same.

Test script:
---------------
class MyClass {
	final public function iamFinalPublic()
	{
	}
	public function iamPublic()
	{
	}
}
function testReflection()
{
        $refl = new ReflectionClass(MyClass::class);

        $methods = $refl->getMethods(~ReflectionMethod::IS_PUBLIC);
        return count($methods);
}

echo testReflection();

Expected result:
----------------
0

Actual result:
--------------
2

Patches

73195 (last revision 2016-09-28 15:49 UTC by cmb@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-28 10:05 UTC] grgtvs at gmail dot com
-Type: Bug +Type: Documentation Problem
 [2016-09-28 10:05 UTC] grgtvs at gmail dot com
This seems just to be an undocumented issue not a bug
 [2016-09-28 15:48 UTC] cmb@php.net
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: johannes
 [2016-09-28 15:48 UTC] cmb@php.net
The issue is not the logical disjunction (|), but rather the
negation (~) in combination with the fact, that getMethods()
simply checks, whether the logical conjunction (&) of the given
filter value and the stored value is non-zero. Even if there were
no hidden flags (but there are), `~ReflectionMethod::IS_PUBLIC`
would catch final methods, for instance.

I'm not sure whether that is a bug, or just a documentation issue.
Anyhow, the fix appears to be trivial (see the attached
73195.patch).

Johannes, as you're listed as mainter of ext/reflection, what do
you think?
 [2016-09-28 15:49 UTC] cmb@php.net
The following patch has been added/updated:

Patch Name: 73195
Revision:   1475077757
URL:        https://bugs.php.net/patch-display.php?bug=73195&patch=73195&revision=1475077757
 [2016-09-28 16:29 UTC] johannes@php.net
-Assigned To: johannes +Assigned To: cmb
 [2016-09-28 16:29 UTC] johannes@php.net
I believe this was designed to do only "positive" filtering. If we get negation to work I believe we should do this and add an example to the docs. Do you have karma, cmb?
 [2016-09-28 16:37 UTC] cmb@php.net
> Do you have karma, cmb?

Yes. I'll have a closer look at the issue, and check that the
change doesn't break anything else. If all works well, I'll treat
this as a bug, and apply to PHP-5.6+.
 [2016-09-29 09:43 UTC] cmb@php.net
Unfortunately, using the complement (~) can't work without causing
a considerable BC break. For instance, `~IS_PUBLIC` would
basically translate to `IS_STATIC | IS_FINAL | IS_ABSTRACT |
IS_PROTECTED | IS_PRIVATE`, so static public methods would be
returned, what is not desired.

The attached patch would change the semantics of bitwise
disjunction of the filter, so that all given flags would be
required. For instance, `IS_PUBLIC | IS_PROTECTED` would never
match.

So all we can do (at least for now) is to improve the docs.
 [2016-09-29 10:12 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=340255
Log: Fix #73195: ReflectionClass::getMethod() filter arguments doesn't accept combinations
 [2016-09-29 10:13 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-09-29 10:13 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:06 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=992b071c3d5890383eea943186e25b7f94ff39cd
Log: Fix #73195: ReflectionClass::getMethod() filter arguments doesn't accept combinations
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 23:01:30 2024 UTC