Skip to content

Assert::implementsInterface($foo, <Type>) is incorrectly being translated to assert($foo instanceof <Type>) #18

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

Closed
Ocramius opened this issue Dec 2, 2019 · 5 comments · Fixed by #144

Comments

@Ocramius
Copy link

Ocramius commented Dec 2, 2019

Assert::implementsInterface() works on object|string, not just object, so it should be translated to something like assert($foo instanceof $type || in_array($interface, \class_implements($value))) (see https://3v4l.org/VdIbR).

Not sure what the correct translation (understood by PHPStan) would be.

For reference, this is the current implementation:

'implementsInterface' => function (Scope $scope, Arg $expr, Arg $class): ?\PhpParser\Node\Expr {
$classType = $scope->getType($class->value);
if (!$classType instanceof ConstantStringType) {
return null;
}
return new \PhpParser\Node\Expr\Instanceof_(
$expr->value,
new \PhpParser\Node\Name($classType->getValue())
);
},

@ondrejmirtes
Copy link
Member

Can you tell me some real-world examples of implementsInterface usages? I've seen examples where both arguments are literal strings which doesn't seem very useful.

@Ocramius
Copy link
Author

Ocramius commented Dec 2, 2019

Currently finding it in something like:

class MyThingFactory
{
    public function make(string $thing)
    {
        Assert::implementsInterface($thing, SomeDto::class);

        // use $thing static API here
    }
}

@Ocramius
Copy link
Author

Ocramius commented Dec 2, 2019

Note that this is completely removable if I use something like:

class MyThingFactory
{
    /**
     * @template T of SomeDto
     * @param class-string<T> $thing
     */
    public function make(string $thing)
    {
        Assert::implementsInterface($thing, SomeDto::class);

        // use $thing static API here
    }
}

Not sure if that's understood by the tooling yet though

@orklah
Copy link

orklah commented Feb 9, 2020

Can you tell me some real-world examples of implementsInterface usages? I've seen examples where both arguments are literal strings which doesn't seem very useful.

This leads to weird hacks in real projects:
https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/DocBlock/StandardTagFactory.php#L177

@github-actions
Copy link

github-actions bot commented Nov 6, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants