Skip to content

How to add static analysis to this tricky code? #10795

Discussion options

You must be logged in to vote

You can achieve that with a custom rule that will check these calls. But personally I'd just do this:

    /**
     * @template T
     * @param ItemsProvider<T> $subject
     * @param callable(T): void $cb
     */
    public function execute(BatchItemsProviderInterface $subject, callable $cb): void
    {
        foreach ($itemsProvider->getItems() as $item) {
            $cb($item);
        }
    }

And call it like this:

(new Executor)->execute($itemsProvider, function (FooDto $item) {
    $this->doSth($item, 'x', 'y', 'z');
});

That's type safe with already present PHPStan capabilities.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@javaDeveloperKid
Comment options

@ondrejmirtes
Comment options

@javaDeveloperKid
Comment options

@ondrejmirtes
Comment options

Answer selected by javaDeveloperKid
@javaDeveloperKid
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants