phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

`RenderCallbackRule` ignores `::trustedCallbacks()`

Open Boegie opened this issue 1 year ago • 5 comments

Bug report

(Now that all Entity Query problems seem to be solved, I looked a bit closer at the Trusted Callbacks, so expect some more issues on that)

Currently \mglaman\PHPStanDrupal\Rules\Drupal\RenderCallbackRule only checks if the class implements TrustedCallbackInterface. However the callbacks should also be in the return array from trustedCallbacks() to be trusted.

Now I highly doubt that people "in the wild" go through the trouble of creating callbacks and making sure the containing class implements TrustedCallbackInterface and finally don't add the callback to trustedCallbacks(), so the impact seems low.

However what we're currently doing seems incorrect.

Code snippet that reproduces the problem

See testcases in (extended) tests/src/Rules/data/bug-543.php

Boegie avatar May 12 '23 15:05 Boegie

Yep, this is a big gap. I thought I had documented it in the code with a @todo but I did not!

mglaman avatar May 12 '23 16:05 mglaman

// @todo: Write @todo...

Boegie avatar May 12 '23 16:05 Boegie

Problem: we would need to call the method to get its values. I think. Even though the method is static, I don't believe it's return values can be parsed that way.

mglaman avatar May 17 '23 21:05 mglaman

Ideas:

                preg_match('#^([a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*)::([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\z#', $constantStringType->getValue(), $matches);
$foo = (new ObjectType($matches[1]))->getClassReflection()
                        ->getNativeReflection()->getMethod($matches[2])

We can then do \ReflectionMethod::invoke with null for first param.

So:

$foo = (new ObjectType($matches[1]))->getClassReflection()
    ->getNativeReflection()
    ->getMethod($matches[2])
    ->invoke(null)

$foo should have the method names, then.

mglaman avatar May 17 '23 21:05 mglaman

The PR is almost ready, but it crashes on Drupal core due to the way a class fixture is defined.

mglaman avatar Jun 28 '23 21:06 mglaman