Skip to content

Fix class_uses polyfill #31

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

Merged
merged 1 commit into from
Feb 25, 2016
Merged

Fix class_uses polyfill #31

merged 1 commit into from
Feb 25, 2016

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Jan 25, 2016

class_uses accepts both the class name and object instance as first argument, class_exists only accepts a class name. If an object instance is giving, the class is already autoloaded, so the other logic can be skipped.

@nicolas-grekas
Copy link
Member

Could you add a test please ?

@wouterj
Copy link
Member Author

wouterj commented Jan 25, 2016

@nicolas-grekas done

@wouterj
Copy link
Member Author

wouterj commented Jan 25, 2016

I've completely changed the function now btw, as it had to return an array and not a boolean as it did before.

@nicolas-grekas
Copy link
Member

Great! Some comments:

  • class_uses with a non-existent class name returns false,
  • we miss a test with a class instance,
  • I'm not sure using spl_autoload_register provides more behavior coverage, does it? If yes, then I'd suggest unregistering the autoloader to leave the global state clean after the test has run.

@wouterj wouterj force-pushed the patch-2 branch 2 times, most recently from 831f72a to 1224378 Compare January 25, 2016 14:00
@wouterj
Copy link
Member Author

wouterj commented Jan 25, 2016

I've fixed the first 2 comments.

The test with spl_autoload_register was there to test the second argument of class_uses: $autoload. However, I couldn't get things to work probably (unregistering didn't work for me for some reason), so I've removed the tests for now.

function class_uses($class, $autoload = true) { return $autoload && class_exists($class, $autoload) && false; }
function class_uses($class, $autoload = true)
{
if (!is_object($class) && $autoload && !class_exists($class, true)) {
Copy link
Member

Choose a reason for hiding this comment

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

when autoload === false, we should still return false when the class does not exist. But when $class is an interface, we shoudld return array()... Could be done with:

if (is_object($class) || class_exists($class, $autoload) || interface_exists($class, false)) {
    return array();
}

return false;

@wouterj
Copy link
Member Author

wouterj commented Jan 25, 2016

Updated again

function class_uses($class, $autoload = true) { return $autoload && class_exists($class, $autoload) && false; }
function class_uses($class, $autoload = true)
{
if (is_object($class) || class_exists($class, $autoload) || interface_exists($class, $autoload)) {
Copy link
Member

Choose a reason for hiding this comment

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

interface_exists($class, false) because the autoloader has already been triggered, if needed, by class_exists

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@nicolas-grekas
Copy link
Member

👍 (the missing warning is a bonus to me, nobody should rely on it)

@xabbuh
Copy link
Member

xabbuh commented Jan 30, 2016

👍 (if you wanted to add the warning too that would be nice though)

@wouterj
Copy link
Member Author

wouterj commented Feb 25, 2016

Can we please merge this?

@fabpot
Copy link
Member

fabpot commented Feb 25, 2016

Thank you @wouterj.

@fabpot fabpot merged commit b2c4e67 into symfony:master Feb 25, 2016
fabpot added a commit that referenced this pull request Feb 25, 2016
This PR was merged into the 1.1-dev branch.

Discussion
----------

Fix class_uses polyfill

`class_uses` accepts both the class name and object instance as first argument, `class_exists` only accepts a class name. If an object instance is giving, the class is already autoloaded, so the other logic can be skipped.

Commits
-------

b2c4e67 Fix class_uses polyfill
@nicolas-grekas nicolas-grekas mentioned this pull request Mar 10, 2016
fabpot added a commit that referenced this pull request Mar 10, 2016
This PR was merged into the 1.1-dev branch.

Discussion
----------

Add CHANGELOG

Fixes #32

* v1.1.1

 * bug #40 [Apcu] Load APCUIterator only when APCIterator exists (nicolas-grekas)
 * bug #37 [Iconv] Fix wrong use in bootstrap.php (tucksaun)
 * bug #31 Fix class_uses polyfill (WouterJ)

* v1.1.0

 * feature #22 [APCu] A new polyfill for the legacy APC users (nicolas-grekas)
 * bug #28 [Php70] Workaround https://bugs.php.net/63206 (nicolas-grekas)

* v1.0.1

 * bug #14 ldap_escape does not encode leading/trailing spaces. (ChadSikorra)
 * bug #17 Fix #16 - gzopen() / gzopen64() - 32 bit builds of Ubuntu 14.04 (fisharebest)

* v1.0.0

 * Hello symfony/polyfill

Commits
-------

1781007 Add CHANGELOG
@wouterj wouterj deleted the patch-2 branch June 1, 2016 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants