How to add static analysis to this tricky code? #10795
Answered
by
ondrejmirtes
javaDeveloperKid
asked this question in
Support
-
This is very tricky for me. Could someone be so kind and help? /**
* @template T
*/
interface ItemsProvider
{
/**
* @return array<T>
*/
public function getItems(): array;
}
class Executor
{
public function execute(ItemsProvider $itemsProvider, callable $cb, array $cbArgs): void
// or maybe should use this ...$cbArgs notation (forgot the this functionality name)?
// public function execute(BatchItemsProviderInterface $subject, callable $cb, ...$cbArgs): void
{
foreach ($itemsProvider->getItems() as $item) {
$cb($item, ...$cbArgs);
}
}
}
class FooDto {}
class SomeClass
{
public function f()
{
/** @var ItemsProvider<FooDto> $itemsProvider */
$itemsProvider = new class() implements ItemsProvider {
public function getItems(): array {
return [new FooDto, new FooDto];
}
};
// HERE! How do I have it covered by PHPStan that doSth will be called with FooDto, x, y, z parameters respectively?
(new Executor)->execute($itemsProvider, $this->doSth(...), ['x', 'y', 'z']);
// or
// (new Executor)->execute($itemsProvider, $this->doSth(...), 'x', 'y', 'z');
}
private function doSth(FooDto $fooDto, string $paramX, string $paramY, string $paramZ): void
{
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
ondrejmirtes
Mar 27, 2024
Replies: 1 comment 5 replies
-
What's the expected output? What error do you want to report? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can achieve that with a custom rule that will check these calls. But personally I'd just do this:
And call it like this:
That's type safe with already present PHPStan capabilities.