Skip to content
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

Filter get methods and properties #275

Merged
merged 4 commits into from
May 18, 2017

Conversation

marcosh
Copy link

@marcosh marcosh commented May 17, 2017

solves #103

{
return array_values($this->getMethodsIndexedByName());
if (null === $filter) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use is_null() function ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roukmoute no, please don't do that, unless you aren't using array_map() :-P

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius But I've never understood why you prefer to test null while a function exists.
Can you clarify me on this point?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roukmoute is_null() is a function: while it allows for function composition, calling functions in PHP is the slowest thing ever, and the first/simplest thing you can do to remove O(1) overhead on any codebase.

To clarify:

// good
array_keys(array_filter([null, 1, 2, null], 'is_null'));

// bad
if (is_null($something)) {
    // ...
}

// good
if (null === $something) {
    // ...
}

Copy link

@roukmoute roukmoute May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Ocramius for your great explanation :)

But I've few remarks:

  • is_null() does the job in C, and the comparison is not the same things, is it a mistake? In fact is it not too much to optimize this kind of things? (This is a real question 😄 )
  • You talking about big O notation, is it really necessary to think to this low level ? But I do a benchmark, and finally with PHP 7.1.4, is_null is more fastest. I don't know for previous versions.

Here is a small screencast about that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roukmoute you forgot to namespace it :-)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius Can not agree with you that calling special functions are slow. Because you missing the presence of Optimizer in PHP. So if you will use \is_null($something); then it won't be function call at all, because PHP optimizer will inline it as simple type check.

To prove it, check https://3v4l.org/Ivbid/vld#output to see TYPE_CHECK opcode for that. And it's very fast 😄 But without leading slash it will be slow function call: https://3v4l.org/asV6T/vld#output

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, well aware of the optimisation - still a huge load of noise for nothing IMO

@@ -338,20 +338,33 @@ private function getMethodsIndexedByName() : array
/**
* Fetch an array of all methods for this class.
*
* @param ?int $filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is int|null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More documentation is needed on $filter, with examples, since this is a bitmask.

}

/**
* Get only the methods that this class implements (i.e. do not search
* up parent classes etc.)
*
* @param ?int $filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remarks here

@@ -478,40 +493,47 @@ public function getConstructor() : ReflectionMethod
* Get only the properties for this specific class (i.e. do not search
* up parent classes etc.)
*
* @param ?int $filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

return array_filter(
$this->cachedProperties,
function (ReflectionProperty $property) use ($filter) {
return null === $filter || $filter & $property->getModifiers();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird that in this scenario you went for the "always true" condition in the filter callback

}

/**
* Get the properties for this class.
*
* @param ?int $filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -95,9 +95,10 @@ public static function createFromInstance($object)
/**
* Reflect on runtime properties for the current instance
*
* @param ?int $filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same - maybe we should use @see and point to a single location

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be ok to point directly to http://php.net/manual/en/reflectionclass.getmethods.php where the parameter meaning is explained?

@Ocramius
Copy link
Member

Ocramius commented May 17, 2017 via email

@marcosh
Copy link
Author

marcosh commented May 18, 2017

@Ocramius I updated the PR with the fixes you suggested

@Ocramius Ocramius self-assigned this May 18, 2017
@Ocramius Ocramius added this to the 2.0.0 milestone May 18, 2017
@Ocramius
Copy link
Member

@marcosh awesome work! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants