Open joachim-n opened 2 years ago
Fix your callback to not use a string concatenation with static. There may be a core bug for this though. Try using self
over static
This should be documented already on Drupal.org for PHPStan. I think. PHPStan can't resolve static, especially when using concatenation this way for a callback.
Could it give a general warning about the '#lazy_builder'
part at least, and flag it as something that may need checking?
I used static rather than self as I'd understood it was more desirable in case of a child class overriding.
I'll have to revisit and see. I don't think this code can even know. static::class
comes back as something, and it's in concat... I'll see. There are a few issues floating around.
It works fine if array but not concat string
[static::class, 'callBack']
But Drupal core assumes the callback is always a string, primarily a service.
What I meant is that the analysis could just spot an array key that has the value '#lazy_builder' and say 'Hey this might need checking, but we don't know for sure.'
This should be doing that
Okay, at computer. We have this fixture:
'#pre_render' => [
[self::class, 'preRenderCallback'],
[static::class, 'preRenderCallback'],
self::class . '::preRenderCallback',
static::class . '::preRenderCallback',
[$this, 'preRenderCallback'],
static function(array $element): array {
return $element;
}
]
Gives:
__DIR__ . '/../../fixtures/drupal/modules/pre_render_callback_rule/src/RenderArrayWithPreRenderCallback.php',
[
[
"#pre_render value 'non-empty-string' at key '3' is invalid.",
19,
"Refactor concatenation of `static::class` with method name to an array callback: [static::class, 'preRenderCallback']"
]
]
So it catches #pre_render
but not #lazy_builder
it seems
I wrote a test and pushed a PR: https://github.com/mglaman/phpstan-drupal/pull/426, locally this is working as expected.
This should also be fixed by https://github.com/mglaman/phpstan-drupal/pull/436
@joachim-n can you try this again with the latest release?
I had the following class in a custom module in my Drupal 8.9 codebase. upgrade_status didn't detect that this needed to implement TrustedCallbackInterface for the lazy builder callback.