spaze / phpstan-disallowed-calls

PHPStan rules to detect disallowed method & function calls, constant, namespace, attribute & superglobal usages
MIT License
255 stars 17 forks source link

Add support for disallowing things when used inside class with certain attribute #180

Open ruudk opened 1 year ago

ruudk commented 1 year ago

Example use case, I want to prevent the usage of a certain class / method, inside a class that has #[Entity] attribute.

spaze commented 1 year ago

(I've misunderstood, #179 is about a different thing, reopening)

ruudk commented 1 year ago

Another example:

<?php

final class CreateUserHandler 
{
    public function __construct(private EventRecorder $recorder)
    {
    }

    #[AsCommandHandler]
    public function handle(CreateUserCommand $command)
    {

    }
}

I want to forbid the usage of EventRecorder everywhere, except when it's used in a class that has the AsCommandHandler attribute (somewhere defined).

So in this case, the attribute is put on the handle method (not on the class) and the EventRecorder is used in the __construct. It should therefore be accepted because the attribute is used within or on the class.

spaze commented 9 months ago

Hey @ruudk I'm now considering implementing this next(-ish). Would you still find it useful or you don't need it anymore?

ruudk commented 4 months ago

@spaze Sorry for not getting back to you. That's a bit rude of me.

Yes, I would totally use this. As a matter of fact, today I needed it again.

New use case: I want to disallow RequestStack, but allow it inside a TwigComponent. It's the only way to get the request within a component.

#[AsTwigComponent]
final class Table
{
    public function __construct(
        private RequestStack $requestStack,
    ) {}
}
spaze commented 4 months ago

Ok, got it, let me see what I can do :-)