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

Assert::interfaceExists($value, $message = '') #69

Closed
bmeynell opened this issue May 25, 2018 · 2 comments
Closed

Assert::interfaceExists($value, $message = '') #69

bmeynell opened this issue May 25, 2018 · 2 comments

Comments

@bmeynell
Copy link

As of PHP 5.02 class_exists,

No longer returns TRUE for defined interfaces. Use interface_exists().

Quick Test:

interface MyInterface {}
class MyClass {}
echo PHP_VERSION; // 7.1.12-3+ubuntu14.04.1+deb.sury.org+1
var_dump(interface_exists(MyInterface::class)); // bool(true)
var_dump(class_exists(MyInterface::class)); // bool(false)
var_dump(interface_exists(MyClass::class)); // bool(false)
var_dump(class_exists(MyClass::class)); // bool(true)

interfaceExists() Proposal:

public static function interfaceExists($value, $message = '')
{
    if (!interface_exists($value)) {
        static::reportInvalidArgument(sprintf(
            $message ?: 'Expected an existing interface name. Got: %s',
            static::valueToString($value)
        ));
    }
}

... which is akin to classExists():

https://github.com/webmozart/assert/blob/23bf61bc8a7cc229d7ce8689b1bf818a9e192cac/src/Assert.php#L817-L825

@Nyholm
Copy link
Collaborator

Nyholm commented May 26, 2018

You are correct. Thank you.

Can you elaborate the use case for Assert::interfaceExists?

@bmeynell
Copy link
Author

bmeynell commented May 26, 2018

Specialized collection object that only allows objects to be added to it that implement a specific interface. The interface is defined in the collection's constructor, which is where check for its existence occurs.

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

No branches or pull requests

2 participants