Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with command / event busses? #91

Open
ruudk opened this issue Sep 14, 2024 · 2 comments
Open

How to deal with command / event busses? #91

ruudk opened this issue Sep 14, 2024 · 2 comments

Comments

@ruudk
Copy link

ruudk commented Sep 14, 2024

Let's say you have the following:

class MyHandler {
    public function __construct(private SomeDependency $dep) {}

    public function __invoke(MyCommand $command) {}
}

class MyCommand {
    public function __construct(public string $name) {}
}

class MyController {
    public function __invoke() {
        $this->commandBus->dispatch(new MyCommand('Ruud'));
    }
}

The above should return in no dead classes.

But when MyCommand is not dispatched anywhere, it should mark MyHandler + MyCommand as dead.

Same with an Event and multiple event subscribers to that event. If the event is never dispatched, the event + all the subscribers should be marked as dead.

https://symfony.com/doc/current/components/messenger.html

@janedbal
Copy link
Member

I believe that in order to support this (and #83), we need some HiddenCallProvider which will be based on AST, not based on reflection like EntrypointProvider is.

@janedbal
Copy link
Member

janedbal commented Dec 23, 2024

Since 0.7.0, we can deduce usages from AST, but I'm still unsure if it can solve this problem.

Because when we are analysing $this->commandBus->dispatch, we would need to know which handler have the MyCommand in its __invoke method to be able to mark it as used. This is possible in PHPStan only via collectors, which might be really tricky to inject into dead-code-detector's extension point without breaking its performance (mainly because you MUST NOT use reflection in all CollectedData rules).

Or do you see any option without collector-like logic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants