Skip to content

Comments

Add checks for existing callable at compilation#3451

Closed
ghost wants to merge 2 commits into3.xfrom
unknown repository
Closed

Add checks for existing callable at compilation#3451
ghost wants to merge 2 commits into3.xfrom
unknown repository

Conversation

@ghost
Copy link

@ghost ghost commented Dec 1, 2020

Fixes #3450

Currently Twig does not check if the callable passed to CallExpression is actually a callable.
This pull request adds does checks.

If it is a function it check if it exists. If it is an array indicating a class method, then it also checks for the presence of __call and __callStatic methods.
If the callable does not exist a \LogicException is thrown. (I don't know if this is the appropriate Exception).

I think Twig should check for the existence of callables if it can otherwise it can be quite cumbersome to debug when using caching, since it is possible a wrong path will be compiled. (This happened to me.)

Some unrelated Function/Filter tests broke because they were passing undefined callables so I fixed them. However, the intent was clear to pass an existing callable.

$class = substr($callable, 0, $pos);
$method = substr($callable, $pos + 2);
if (!method_exists($class, $method)) {
if (!method_exists($class, '__callStatic')) {
Copy link
Author

Choose a reason for hiding this comment

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

Should this also check for __call? We allow [FQCN, 'nonStaticMethod'] for instance methods even though that is not a legal callable. Is 'FQCN::nonStaticMethod' also legal in context of Twig?

Copy link
Author

Choose a reason for hiding this comment

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

@stof What do you think?

@ghost ghost requested a review from stof December 2, 2020 09:53
@ghost
Copy link
Author

ghost commented Dec 14, 2020

@fabpot do you have an opinion on this pull request?

@ghost
Copy link
Author

ghost commented Dec 20, 2020

Sorry for pinging again. Is there anything else I have to do to get this PR merged? Or are these checks not desired? @stof @fabpot

@ghost
Copy link
Author

ghost commented Jan 14, 2021

@stof @fabpot do I have to do anything else?

@ghost ghost closed this Jan 15, 2021
@ghost ghost reopened this Jan 15, 2021
@fabpot fabpot closed this Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Twig assumes static callable if it does not exist when registering a filter

3 participants