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

instanceof checks in ExpressionVisitor #294

Open
rela589n opened this issue Jan 29, 2022 · 0 comments
Open

instanceof checks in ExpressionVisitor #294

rela589n opened this issue Jan 29, 2022 · 0 comments

Comments

@rela589n
Copy link

Hello. I am wondering why does \Doctrine\Common\Collections\Expr\ExpressionVisitor::dispatch restricts $expr to be instance of predefined classes.

Doesn't it violate key principles of visitor pattern which leverage polymorphism and double dispatch in order to avoid such hard-coding of instanceof checks.

public function dispatch(Expression $expr)
{
    switch (true) {
        case $expr instanceof Comparison:
            return $this->walkComparison($expr);

        case $expr instanceof Value:
            return $this->walkValue($expr);

        case $expr instanceof CompositeExpression:
            return $this->walkCompositeExpression($expr);

        default:
            throw new RuntimeException('Unknown Expression ' . get_class($expr));
    }
}

CMIIW, foregoing code can be replaced with single line like this:

public function dispatch(Expression $expr)
{
    return $expr->visit($this);
}

Moreover, this method seems useless to me as we can (and should) call visit directly in the client code. If not, then what is the reason of Expression interface and implementations of visit method if those are not used at all?

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

1 participant